import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if (n % 2 == 0) {
            System.out.println((n - 4) + " " + (n - (n - 4)));
        } else {
            System.out.println((n - 9) + " " + (n - (n - 9)));
        }
    }

}


import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Anirudh Rayabharam (anirudhrb@yandex.com)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        if (n % 2 == 0) {
            out.println("4 " + (n - 4));
        } else {
            out.println("9 " + (n - 9));
        }
    }
}

class InputReader {
    public BufferedReader reader;
    private int tokenCount, nextTokenIndex;
    private String[] tokens;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenCount = nextTokenIndex = 0;
    }

    public String next() {
        String nextLine;
        if (nextTokenIndex == tokenCount) {
            try {
                nextLine = reader.readLine();
                nextTokenIndex = 0;
                tokens = nextLine.split("\\s");
                tokenCount = tokens.length;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return tokens[nextTokenIndex++];
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}


import java.io.IOException;
import java.util.Scanner;

/**
 * TODO: describe
 *
 * @author keks
 */
public class A {
    public static void main(String[] args) throws IOException {
        final Scanner sc = new Scanner(System.in);
        final int n = sc.nextInt();
        if (n % 2 == 0) {
            System.out.println(4 + " " + (n - 4));
        } else {
            System.out.println(9 + " " + (n - 9));
        }
    }
}


import java.util.*;
import java.io.*;
public class Cf270a {
    public static void main(String[] args) throws IOException {
        InputStreamReader fin = new InputStreamReader(System.in);
        Scanner scr = new Scanner(fin);
        int n = scr.nextInt();
        int x = 0;
        int y = 0;
        if (n%2 == 0) {
            x = 4;
            y = n - x;
        } else {
            x = 9;
            y = n - x;
        }
        PrintWriter fout = new PrintWriter(System.out);
        fout.print(x+" "+y);
        fout.flush();
        fout.close();
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class TA {

	public void solve(long n) {
		long a = 0, b = 0;
		if (n % 2 == 0) {
			if (n % 4 == 0) {
				a = n / 2;
				b = n/ 2;
			} else {
				a = n / 2 - 1;
				b = n / 2 + 1;
			}
		} else {
			a = 4;
			b = n - a;
			while (b > 0 && (b % 3 != 0)) {
				a += 2;
				b = n - a;
			}

		}
		System.out.println(a + " " + b);
	}
	public static void main(String[] args) {
		FastScanner in = new FastScanner();

		new TA().solve(in.nextLong());
	}
	static class FastScanner {

		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}

	}

}


import java.util.Scanner;

public class J472A {
	private static Scanner scan = new Scanner(System.in);
	public static void main(String[] args) {
		int a = scan.nextInt();
		if(a % 2 == 0) {
			System.out.println(4 + " " + (a - 4));//ż���4
		} else {
			System.out.println(9 + " " + (a - 9));//������
		}
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a,b;
		if (n%2==0) {
			a = 4;
		}else{
			a = 9;
		}
		b = n - a;
		System.out.println(a + " " + b);
	}

}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();

        int a = n/2;
        int b = (n/2) + (n%2);

        if ((a%2!=0 && a%3!=0) || (b%2!=0 && b%3!=0)) {
            a--;
            b++;
        }

        if ((a%2!=0 && a%3!=0) || (b%2!=0 && b%3!=0)) {
            a--;
            b++;
        }

        System.out.println(a + " " + b);
    }
}


import java.util.Scanner;

public class composite {

    public static void main(String[] args) {
        int b;
        Scanner s3=new Scanner(System.in);
         b=s3.nextInt();

          if(b%2==0)
          {
              b=b-4;
              System.out.println(4+" "+b);
          }
          else
          {
              b=b-9;
              System.out.println(9+" "+b);
          }

}
}


import java.io.*;
import java.util.*;

public class A {

	BufferedReader in;
	StringTokenizer st;
	PrintWriter out;

	String next() throws IOException {
		while (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}

	int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	long nextLong() throws Exception {
		return Long.parseLong(next());
	}

	double nextDouble() throws Exception {
		return Double.parseDouble(next());
	}

	void solve() throws Exception {
		int n = nextInt();
		if(n%2==0)
			out.println("4 "+(n-4));
		else
			out.println("9 "+(n-9));
	}

	void run() {
		try {
			Locale.setDefault(Locale.US);
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public static void main(String[] args) {
		new A().run();
	}

}


import java.io.*;
import java.util.*;

public class Main {
  // main
  public static void main(String [] args) throws IOException {
    // makes the reader and writer
    BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    // read in N
    int N = Integer.parseInt(f.readLine());

    // write to out
    if (N%2==0) out.println("4 "+(N-4));
    if (N%2==1) out.println("9 "+(N-9));

    // cleanup
    out.close();
    System.exit(0);
  }
}


import java.util.Scanner;

public class A470 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	Scanner sc = new Scanner(System.in);

	int n=sc.nextInt();

	int start=4;

	while(true){

		if((start%2==0||start%3==0)&&((n-start)%2==0||(n-start)%3==0))
		{
			System.out.println(start+" "+(n-start));
			return;
		}
		else
		start++;

	}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class A {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tb;
		int n = Integer.parseInt(br.readLine());
		int x = 0,y=0;
		if(n%2==0){
			x = n-4;
			y = 4;
		}else{
			x = n-9;
			y = 9;
		}
		System.out.println(x+" "+y);
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author bdepwgjqet
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		A solver = new A();
		solver.solve(1, in, out);
		out.close();
	}
}

class A {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int number = in.nextInt();
        if((number & 1) == 0) {
            out.println(4+" "+(number-4));
        } else {
            out.println(9+" "+(number-9));
        }
    }
}

class InputReader {

    private final BufferedReader bufferedReader;
    private StringTokenizer stringTokenizer;

    public InputReader(InputStream in) {
        bufferedReader = new BufferedReader(new InputStreamReader(in));
        stringTokenizer = null;
    }

    public String nextLine() {
        try {
            return bufferedReader.readLine();
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String nextBlock() {
        while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(nextLine());
        }
        return stringTokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextBlock());
    }

    public double nextDouble() {
        return Double.parseDouble(nextBlock());
    }

    public long nextLong() {
        return Long.parseLong(nextBlock());
    }
}


import java.io.*;
import java.util.*;
import java.math.*;

public class p472a {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 2 == 0) {
            System.out.println("8 " + (n - 8));
        } else {
            System.out.println("9 " + (n - 9));
        }
    }
}


import java.util.*;
public class A
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);

		int n = in.nextInt();

		if(n % 2 == 1)
		{
			System.out.println(9 + " " + (n - 9));
		}
		else
		{
			System.out.println(4 + " " + (n - 4));
		}
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;

public class Problem1 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        long n=Long.parseLong(br.readLine());
        if(n%2==0){
            System.out.println(4+" "+(n-4));
        }
        else{
            System.out.println(9+" "+(n-9));
        }

    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Test {
public static void main(String[] args) throws IOException{
	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	//StringTokenizer st=new StringTokenizer(bf.readLine());
	int n=Integer.parseInt(bf.readLine());

	if(n%2==0) System.out.println(4+" "+(n-4));
	else System.out.println(9+" "+(n-9));

}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

    public static boolean isPrime(long num){
        int divisor = 2;
        boolean bandera = true;
        while(bandera && divisor<num) //ponemos el while con su condicion en este caso si bandera
        {
        if (num%divisor==0) {
            bandera=false;
            break;
        }else{
            divisor++; //igual si el divisor llego al numero q se capturo este sera primo
        }
        }
        return bandera;
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int uno = 4;
        int dos = n-4;

        while(isPrime(dos) || isPrime(uno)){
            dos--;
            uno++;
        }

        System.out.println(uno+" "+dos);

    }

}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
     int n,a,b;
     Scanner obj=new Scanner(System.in);

     n=obj.nextInt();

     if(n%4==0){a=n/2;b=n/2;System.out.println(a+" "+b);}
     else if(n%2==0 && n%4!=0)
     {a=n/2-1;b=n/2+1;System.out.println(a+" "+b);}

     else if(n%2!=0)
     {  a=4;b=0;
       while(b!=1)
       { b=n-a;
         if(b%3==0){ System.out.println(a+" "+b);break; }
         else{a=a+2;}
       }
     }
    }
}


import java.util.*;
import static java.lang.Math.*;
import java.io.*;

public class SolutionB {
      public static void main(String args[])throws IOException{
              Scanner sc = new Scanner(System.in);
              int a[] = new int[1501];
              for(int i = 0; i < 3; i++){
                     a[sc.nextInt()]++;
              }
              if(a[1] > 0 || a[2] > 1 || a[3] > 2 || (a[4] == 2 && a[2] == 1)){
                     System.out.println("YES");
              }else{
                     System.out.println("NO");
              }
      }
}


import java.io.*;
import java.math.*;
import java.util.*;

// author @mdazmat9
public  class codeforces{
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int test = 1;
        for (int ind = 0; ind < test; ind++) {
          int [] a=new int[3];
          a[0]=sc.nextInt();
          a[1]=sc.nextInt();
          a[2]=sc.nextInt();
          Arrays.sort(a);
          int k1=a[0];
          int k2=a[1];
          int k3=a[2];
          if(k1==1 || k2==1 || k3==1){
              out.println("YES");
          }
          else if((k1==2 && k2==2)||(k2==2 && k3==2)){
              out.println("YES");
            }
            else if(k1==3 && k2==3 && k3==3){
              out.println("YES");
          }
          else if(k1==2 && k2==4 && k3==4){
              out.println("YES");
          }
          else
              out.println("NO");

        }
        out.flush();
    }

    static   void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }
    static long gcd(long a , long b)
    {
        if(b == 0)
            return a;
        return gcd(b , a % b);
    }

}
class Scanner {
    public BufferedReader reader;
    public StringTokenizer st;

    public Scanner(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        st = null;
    }

    public String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = reader.readLine();
                if (line == null) return null;
                st = new StringTokenizer(line);
            } catch (Exception e) {
                throw (new RuntimeException());
            }
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}

class OutputWriter {
    BufferedWriter writer;

    public OutputWriter(OutputStream stream) {
        writer = new BufferedWriter(new OutputStreamWriter(stream));
    }

    public void print(int i) throws IOException {
        writer.write(i);
    }

    public void print(String s) throws IOException {
        writer.write(s);
    }

    public void print(char[] c) throws IOException {
        writer.write(c);
    }

    public void close() throws IOException {
        writer.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test3 {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int x=Integer.parseInt(br.readLine());
		int y=Integer.parseInt(br.readLine());
		System.out.print((int)(y%(Math.pow(2, x))));
	}
}


/**
 *   Author: Ridam Nagar
 *   Date: 27 February 2019
 *   Time: 01:17:36
**/
/*
package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
  static String reverse(String s){
    String reverse="";
    for(int i=s.length()-1;i>=0;i--){
        reverse=reverse + s.charAt(i);
    }
    return reverse;
  }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
         int n=sc.nextInt();
         int m=sc.nextInt();
         int x=m%(int)Math.pow(2,n);
         System.out.println(x);

}
}


import java.math.BigInteger;
import java.util.Scanner;

public class RENAMETHISBITCH {

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {

			int n = sc.nextInt();
			BigInteger m = sc.nextBigInteger();

			System.out.println(m.mod(BigInteger.valueOf(2).pow(n)));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}


import java.util.*;
import java.io.*;

public class aaaaaaaaaaaaaaaa {
	public void run() throws Exception {
		Scanner file = new Scanner(System.in);
		int a = file.nextInt(), b= file.nextInt(), c = file.nextInt(), n = file.nextInt();
		a -= c;
		b -= c;
		if (a < 0 || b < 0) System.out.println(-1);
		else {
			int x = a + b + c;
			if (x >= n) System.out.println(-1);
			else System.out.println(n - x);
		}
	}

	public static void main(String[] args) throws Exception {
		new aaaaaaaaaaaaaaaa().run();
	}

}


import java.util.*;

public class helloWorld
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int n = in.nextInt();

		int ans = n - (a + b - c);
		if(ans < 1 || a >= n || b >= n || c > a || c > b)
			ans = -1;

		System.out.println(ans);

		in.close();
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {

	static class Task {

		int NN = 500005;
		int MOD = 1000000007;
		int INF = 2000000000;
		long INFINITY = 2000000000000000000L;

		public void solve(InputReader in, PrintWriter out) {
			int t = in.nextInt();
			while(t-->0) {
				long n =in.nextLong();
				long m = in.nextLong();
				long x1 = in.nextLong();
				long y1 = in.nextLong();
				long x2 = in.nextLong();
				long y2 = in.nextLong();
				long x3 = in.nextLong();
				long y3 = in.nextLong();
				long x4 = in.nextLong();
				long y4 = in.nextLong();
				long w = white(1, 1, m, n);
				long b = black(1, 1, m, n);
				long whited = 0;
				if(x3 > x2 || x4 < x1 || y3 > y2 || y4 < y1) {
					whited = black(x1, y1, x2, y2);
				} else {
					whited = black(x1, y1, x2, y2);
					long xm1 = Math.max(x1, x3);
					long ym1 = Math.max(y1, y3);
					long xm2 = Math.min(x2, x4);
					long ym2 = Math.min(y2, y4);
					whited -= black(xm1, ym1, xm2, ym2);
				}
				b -= whited;w += whited;
				long blacked = white(x3, y3, x4, y4);
				w-= blacked;b += blacked;
				out.println(w + " " + b);
			}
		}

		long black(long x1, long y1, long x2, long y2) {
			long dx = (x2 - x1) + 1;
			long dy = (y2 - y1) + 1;
			if((x1+y1)%2!=0) {
				return ((dy+1)/2)*((dx+1)/2)+(dy/2)*(dx/2);
			}
			return ((dy+1)/2)*((dx)/2)+(dy/2)*((dx+1)/2);
		}

		long white(long x1, long y1, long x2, long y2) {
			long dx = (x2 - x1) + 1;
			long dy = (y2 - y1) + 1;
			if((x1+y1)%2==0) {
				return ((dy+1)/2)*((dx+1)/2)+(dy/2)*(dx/2);
			}
			return ((dy+1)/2)*(dx/2)+(dy/2)*((dx+1)/2);
		}

	}

	static void prepareIO(boolean isFileIO) {
		//long t1 = System.currentTimeMillis();
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) {
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        fout.close();
		}
	}

	public static void main(String[] args) {
        prepareIO(false);
	}

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }

        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
        	return Double.parseDouble(next());
        }

    }

}


import java.io.*;
import java.util.*;

public class A {

	FastScanner in;
	PrintWriter out;

	void solve() {
		long a = in.nextLong(), b = in.nextLong();

		long ans = 0;
		while (true) {
			long div = a / b;

			ans += div;
			a -= div * b;

			if (a == 0) {
				break;
			}

			long tmp = a;
			a = b;
			b = tmp;
		}

		out.println(ans);
	}

	void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new A().run();
	}
}


import java.util.Scanner;

public class A {
	public static final boolean DEBUG = false;
	Scanner sc;

	public void debug(Object o) {
		if (DEBUG) {
			int ln = Thread.currentThread().getStackTrace()[2].getLineNumber();
			String fn = Thread.currentThread().getStackTrace()[2].getFileName();
			System.out.println("(" + fn + ":" + ln+ "): " + o);
		}
	}

	public void pln(Object o) {
		System.out.println(o);
	}

	public void run() {
		sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long nr = 0;
		if (a < b) {
			long aux = a;
			a = b;
			b = aux;
		}
		while (a != 0 && b != 0) {
			nr += a / b;
			long c = a % b;
			a = b;
			b = c;
		}

		pln(nr);
		return;

	}
	public static void main(String[] args) {
		A t = new A();
		t.run();
	}
}


import java.io.*;
import java.util.*;

public class A {
  public static void main(String[] args){
    FastScanner sc = new FastScanner();
    long a = sc.nextLong();
    long b = sc.nextLong();
    long result = 0L;

    while(a != 0 && b != 0) {
      if(a > b) {
        result += a/b;
        a = a % b;
      } else {
        result += b/a;
        b = b % a;
      }

      long gcd = gcd(a, b);
      a /= gcd;
      b /= gcd;
    }

      System.out.println(result);
  }

  private static long gcd(long a, long b) {
    while(a != 0 && b != 0) {
      if(a < b) {
        long tmp = a;
        a = b;
        b = tmp;
      }
      a%=b;
    }
    return a + b;
  }

  public static class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    public FastScanner(String s) {
      try {
        br = new BufferedReader(new FileReader(s));
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      }
    }

    public FastScanner() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String nextToken() {
      while (st == null || !st.hasMoreElements()) try {
        st = new StringTokenizer(br.readLine());
      } catch (IOException e) {
        e.printStackTrace();
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(nextToken());
    }

    long nextLong() {
      return Long.parseLong(nextToken());
    }

    double nextDouble() {
      return Double.parseDouble(nextToken());
    }
  }
}


import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    long solve(long a, long b) {
        return b == 0 ? 0 : a / b + solve(b, a % b);
    }

    public void run() {
        try {
            long a = reader.nextLong();
            long b = reader.nextLong();
            writer.println(solve(a, b));
        } catch (IOException ex) {
        }
        writer.close();
    }

    InputReader reader;
    PrintWriter writer;

    Main() {
        reader = new InputReader();
        writer = new PrintWriter(System.out);
    }

    public static void main(String[] args) {
        new Main().run();
    }

}

class InputReader {
    BufferedReader reader;
    StringTokenizer tokenizer;

    InputReader() {
        reader = new BufferedReader(new InputStreamReader(System.in));
        tokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    Integer nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    Long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    long solve(long a, long b) {
        if (a == 0) {
            return 0;
        }
        long answer = a / b;
        a %= b;
        if (a != 0) {
            answer += 1 + solve(b - a, a);
        }
        return answer;
    }

    public void run() {
        try {
            long a = reader.nextLong();
            long b = reader.nextLong();
            writer.println(solve(a, b));
        } catch (IOException ex) {
        }
        writer.close();
    }

    InputReader reader;
    PrintWriter writer;

    Main() {
        reader = new InputReader();
        writer = new PrintWriter(System.out);
    }

    public static void main(String[] args) {
        new Main().run();
    }

}

class InputReader {
    BufferedReader reader;
    StringTokenizer tokenizer;

    InputReader() {
        reader = new BufferedReader(new InputStreamReader(System.in));
        tokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    Integer nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    Long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Parser in = new Parser(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskA {
    public void solve(int testNumber, Parser in, OutputWriter out) {
        long a = in.nextLong();
        long b = in.nextLong();

        long cnt = 0;
        while (a != 0 && b != 0) {
            if (a > b) {
                cnt += a / b;
                a %= b;
            }
            else {
                cnt += b / a;
                b = b % a;
            }
        }

        out.println(cnt);
    }
}

class Parser
{
    private BufferedReader din;
    private StringTokenizer tokenizer;

    public Parser(InputStream in)
    {
        din = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(din.readLine());
            } catch (Exception e) {
                throw new UnknownError();
            }
        }
        return tokenizer.nextToken();
    }

    public long nextLong()
    {
        return Long.parseLong(next());
    }

    }

class OutputWriter extends PrintWriter {
    public OutputWriter(Writer out) {
        super(out);
    }

    public OutputWriter(OutputStream out) {
        super(out);
    }

    }


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long a = in.nextLong();
        long b = in.nextLong();

        long res = 0;

        while(a > 1 && b > 1) {
            if(a < b) {
                res += b / a;
                b %= a;
            }
            else {
                res += a / b;
                a %= b;
            }
        }

        if(a == 1)
            res += b;
        else
            res += a;

        out.println(res);
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public long nextLong() {
        return Long.parseLong(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));

        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    }


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class A
{
	public static void main(String [] args) throws IOException
	{
		Scanner in = new Scanner(System.in);

		System.out.println(rec(in.nextLong(), in.nextLong()));

	}

	private static long rec(long a, long b)
	{
		return b == 0 ? 0 : a/b + rec(b, a%b);
	}

}


import java.io.*;
import java.util.StringTokenizer;

public class Contest200C {
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		//BufferedReader in = new BufferedReader(new FileReader("200C.in"));
		StringTokenizer st = new StringTokenizer(in.readLine());
		long a = Long.parseLong(st.nextToken());
		long b = Long.parseLong(st.nextToken());

		System.out.println(min(a,b));
	}

	static long min(long a, long b) {
		if (a <= 0 || b <= 0) return 0;
		return a/b+min(b,a%b);
	}
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        long a=s.nextLong(), b=s.nextLong();
        long c=0;
        while(true)  {
            if(a==b ){
                System.out.println(c+a);
                return ;
            } else if(b==a+1){
                c+=1;
                b=a;
            } else if(b<a){

                long h = a/b-1;
                if(h<=0){
                    a-=b;c++;continue;
                }
                a-=b*h;
                c+=h;
            } else{
                if(a==1){
                    long t = b-a;
                    b = t;
                    c+=t;
                    b = a;
                    continue;
                }
                long t = b-a;
                long h = b/a - 1 ;
                if(h<=0){
                    b = t;
                    c+=1;continue;
                }
                c+=h;b-=h*a;
//                b = a;
            }
        }

//        System.out.println(c);
    }
}


import java.io.*;
import java.util.*;

public class A {

  public static void main(String[] args) throws Throwable {
    new A().go();
  }

  void p(String s) {
    System.out.println(s);
  }

  void go() throws Throwable {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] t = in.readLine().split(" ");
    long a = Long.parseLong(t[0]), b = Long.parseLong(t[1]);
    long mv = 0;
    while (a > 1 && b > 1) {
      if (a > b) {
        long tt = a;
        a = b;
        b = tt;
      }
      mv += b / a;
      b %= a;
    }
    System.out.println(mv + Math.max(a, b));
  }
}


import java.util.*;

public class A {
	private static Scanner in;

	public void run() {
		long a = in.nextLong();
		long b = in.nextLong();
		long ans = 0;
		while (a > 0 && b > 0) {
			if (a >= b) {
				ans += a / b;
				a %= b;
				continue;
			}
			ans += b / a;
			b %= a;
		}
		System.out.println(ans);
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		in = new Scanner(System.in);
		new A().run();
		in.close();
	}
}


import java.util.*;

public class NewEmpty
{
    public static void main(String[] args)
    {
        Scanner blabla=new Scanner(System.in);
        long a,b,c=0,d;
        a=blabla.nextLong();
        b=blabla.nextLong();
        while (b!=0){
            c+=(a/b);
            a=a%b;
            d=a;
            a=b;
            b=d;
        }
        System.out.println(c);
    }
}


import java.io.*;
import java.util.*;

public class Main {
	boolean eof;

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	public String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "-1";
			}
		}
		return st.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(nextToken());
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	void run() throws IOException {
		InputStream input = System.in;
		PrintStream output = System.out;
		try {
			File f = new File("trips.in");
			if (f.exists() && f.canRead()) {
				input = new FileInputStream(f);
				output = new PrintStream("trips.out");
			}
		} catch (Throwable e) {
		}
		br = new BufferedReader(new InputStreamReader(input));
		out = new PrintWriter(output);
		solve();
		br.close();
		out.close();
	}

	class Pair implements Comparable<Pair> {
		int x, y;

		Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(Pair p) {
			if (x > p.x) {
				return 1;
			} else if (x < p.x) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	long ans;

	void nod(long a, long b){
		if (a == 0 || b == 0){

		} else if (a > b){
			ans += a / b;
			nod(a % b, b);
		} else {
			ans += b / a;
			nod(a, b % a);
		}
	}

	void solve() {
		long a = nextLong(), b = nextLong();
		ans = 0;
		nod(a, b);
		out.println(ans);
	}

}


import java.io.*;
import java.util.*;

public class cf343a {
  static FastIO in = new FastIO(), out = in;

  public static void main(String[] args) {
    out.println(go(in.nextLong(),in.nextLong()));
    out.close();
  }
  static long go(long a, long b) {
    return b==0?0:(go(b,a%b) + a/b);
  }

  static class FastIO extends PrintWriter {
    BufferedReader br;
    StringTokenizer st;

    public FastIO() {
      this(System.in, System.out);
    }

    public FastIO(InputStream in, OutputStream out) {
      super(new BufferedWriter(new OutputStreamWriter(out)));
      br = new BufferedReader(new InputStreamReader(in));
      scanLine();
    }

    public void scanLine() {
      try {
        st = new StringTokenizer(br.readLine().trim());
      } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
      }
    }

    public int numTokens() {
      if (!st.hasMoreTokens()) {
        scanLine();
        return numTokens();
      }
      return st.countTokens();
    }

    public String next() {
      if (!st.hasMoreTokens()) {
        scanLine();
        return next();
      }
      return st.nextToken();
    }

    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }
  }
}


import java.util.Scanner;

public class RationalResistance {
	static long n = 0;

	static void R(long a, long b) {
		n += a / b;
		a %= b;
		if (a == 0) {
			return;
		}
		R(b, a);
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		long a = cin.nextLong();
		long b = cin.nextLong();
		cin.close();
		R(a, b);
		System.out.println(n);
	}
}


import java.io.*;
import java.util.*;

public class taskA {

	void solve() throws IOException {
		long a = nextLong();
		long b = nextLong();

		long ans = 0;
		while (a  != 0 && b != 0) {
			if (a > b) {
				ans += a / b;
				a %= b;
			} else {
				long c = b % a;
				ans += b / a;
				b = a;
				a = c;
			}
		}
		out.println(ans);
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			// br = new BufferedReader(new FileReader(new File("taskA.in")));
			// out = new PrintWriter("taskA.out");
			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new taskA().run();
	}

	String nextToken() throws IOException {
		while ((st == null) || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}
}


import java.io.*;
import java.util.*;

public class p343a
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println();
        long a = sc.nextLong();
        long b = sc.nextLong();
        if(a==b) System.out.println("1");
        else if(b==1) System.out.println(a);
        else if(a==1) System.out.println(b);
        else if(a>b) System.out.println(count(a,b));
        else System.out.println(count(b,a));
    }

    public static long count(long a,long b)
    {
        long count = 0;
        while(b!=1)
        {
            long c = a/b;
            count += c;
            long d = a-(c*b);
            a = b;
            b = d;
            if(b==1)
            {
                count += a;
                break;
            }
        }
        return count;
    }
}


import java.io.*;
import java.util.*;

public class A {

	static void solve() throws IOException {
		long a = nextLong(), b = nextLong();
		long answer = get(a, b);
		out.println(answer);
	}

	private static long get(long p, long q) {
		if (p == 0) {
			return 0;
		}
		if (q == 1) {
			return p;
		}
		if (p == 1) {
			return q;
		}
		if (p >= q) {
			return p / q + get(p % q, q);
		}
		return q / p + get(p, q % p);
	}

	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter out;

	public static void main(String[] args) throws IOException {
		InputStream input = System.in;
		PrintStream output = System.out;
		File file = new File("a.in");
		if (file.exists() && file.canRead()) {
			input = new FileInputStream(file);
		}
		br = new BufferedReader(new InputStreamReader(input));
		out = new PrintWriter(output);
		solve();
		out.close();
	}

	static long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	static String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = br.readLine();
			if (line == null) {
				return null;
			}
			st = new StringTokenizer(line);
		}
		return st.nextToken();
	}
}


//package CF;

// Div One
import java.util.*;

public class CFA_200 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long x = sc.nextLong();
		long y = sc.nextLong();

		System.out.println(Wilf_tree(x, y));

		sc.close();
	}

	static long Wilf_tree(long a,long b)
	{
		if(a==0||b==0)
			return 0;
		if(a>=b)
			return a/b+Wilf_tree(a%b, b);
		else
			return b/a+Wilf_tree(a, b%a);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Soal3 {
    public static void main(String[] args) throws IOException{
        BufferedReader baca = new BufferedReader(new InputStreamReader(System.in));
        String[] masukan = baca.readLine().split(" ");
        BigInteger a = new BigInteger(masukan[0]);
        BigInteger b = new BigInteger(masukan[1]);

        BigInteger i=new BigInteger("0");
        while(a.compareTo(new BigInteger("1"))!=0){
            if(a.compareTo(b)==1){
                i=i.add(a.divide(b));
                if(a.mod(b)==new BigInteger("0")){
                    a=new BigInteger("1");
                    b=new BigInteger("0");
                }
                else{
                    a=a.mod(b);
                }

            }
            else{
                BigInteger temp =a;
                a=b;
                b=temp;
            }
        }
        if(a.compareTo(new BigInteger("1"))==0){
            i=i.add(b);
        }
        System.out.println(i.toString());
    }
}


import java.util.*;
import java.lang.*;
import java.io.*;

public class Ideone {
	static long ans=0;
	public static void Stepgcd(long a,long b) {
		if (b!=0) {ans+=(a/b);Stepgcd(b,a%b);}
    }
	public static void main (String[] args) throws java.lang.Exception {
		Scanner in=new Scanner(System.in);
		long a=in.nextLong(),b=in.nextLong();
		Stepgcd(a,b);
		System.out.println(ans);
	}
}


import java.io.*;
import java.util.*;

public class A {

	long gcd(long a, long b) {
		if (b == 0) {
			return a;
		} else {
			return gcd(b, a % b);
		}
	}

	long solve(long a,long b) {
		if (a == 0 || b ==0) {
			return 0;
		}
		long t = gcd(a,b);
		a /= t;
		b /= t;
		if (a>b) {
			return solve(a%b,b)+a/b;
		} else {
			return solve(a,b%a)+b/a;
		}

	}

	void solve() throws IOException {
		long a = nextLong();
		long b = nextLong();
		out.println(solve(a, b));
	}

	void run() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new A().run();
	}

	BufferedReader br;
	PrintWriter out;
	StringTokenizer str;

	String next() throws IOException {
		while (str == null || !str.hasMoreTokens()) {
			str = new StringTokenizer(br.readLine());
		}
		return str.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	long nextLong() throws IOException {
		return Long.parseLong(next());
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Task343A {

	public static void main(String... args) throws NumberFormatException,
			IOException {
		Solution.main(System.in, System.out);
	}

	static class Scanner {

		private final BufferedReader br;
		private String[] cache;
		private int cacheIndex;

		Scanner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
			cache = new String[0];
			cacheIndex = 0;
		}

		int nextInt() throws IOException {
			if (cacheIndex >= cache.length) {
				cache = br.readLine().split(" ");
				cacheIndex = 0;
			}
			return Integer.parseInt(cache[cacheIndex++]);
		}

		long nextLong() throws IOException {
			if (cacheIndex >= cache.length) {
				cache = br.readLine().split(" ");
				cacheIndex = 0;
			}
			return Long.parseLong(cache[cacheIndex++]);
		}

		String next() throws IOException {
			if (cacheIndex >= cache.length) {
				cache = br.readLine().split(" ");
				cacheIndex = 0;
			}
			return cache[cacheIndex++];
		}

		void close() throws IOException {
			br.close();
		}

	}

	static class Solution {

		private static long r(long a, long b) {
			if (a == 0 || b == 0) {
				return 0;
			}
			if (a > b) {
				return a / b + r(b, a % b);
			}
			return b / a + r(a, b % a);
		}

		public static void main(InputStream is, OutputStream os)
				throws NumberFormatException, IOException {
			PrintWriter pw = new PrintWriter(os);
			Scanner sc = new Scanner(is);

			long a = sc.nextLong();
			long b = sc.nextLong();

			pw.println(r(a, b));

			pw.flush();
			sc.close();
		}
	}

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Niyaz Nigmatullin
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		FastPrinter out = new FastPrinter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        long a = in.nextLong();
        long b = in.nextLong();
        if (a < b) {
            long t = a;
            a = b;
            b = t;
        }
        long ans = 0;
        while (b > 0) {
            ans += a / b;
            long t = a % b;
            a = b;
            b = t;
        }
        out.println(ans);
    }
}

class FastScanner extends BufferedReader {

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public int read() {
        try {
            int ret = super.read();
//            if (isEOF && ret < 0) {
//                throw new InputMismatchException();
//            }
//            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public String next() {
        StringBuilder sb = new StringBuilder();
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        if (c < 0) {
            return null;
        }
        while (c >= 0 && !isWhiteSpace(c)) {
            sb.appendCodePoint(c);
            c = read();
        }
        return sb.toString();
    }

    static boolean isWhiteSpace(int c) {
        return c >= 0 && c <= 32;
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public String readLine() {
        try {
            return super.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    }

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class A {
	BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;

	public void solve() throws IOException {
		long A = nextLong();
		long B = nextLong();
		long ans = 0;
		while(A > 0){
			if(A >= B){
				ans += A/B;
				A %= B;
				if(A == 0) break;
			}
			else{
				long tmp = A; A = B; B = tmp;
			}
		}
		out.println(ans);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new A().run();
	}

	public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            out = new PrintWriter(System.out);
            solve();
            reader.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

}


import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputStreamReader in = new InputStreamReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, InputStreamReader inSt, PrintWriter out) {
        InputReader in = new InputReader(inSt);
        long a = in.nextLong();
        long b = in.nextLong();

        long result = 0;
        while (b != 1) {
            result += a / b;
            long r = a % b;
            long q = b;
            long top = q % r;
            long bottom = r;
            result += q / r;
            a = top;
            b = bottom;
        }
        result += a;

        out.println(result);
    }

    class InputReader {
        public BufferedReader reader;
        private String[] currentArray;
        int curPointer;

        public InputReader(InputStreamReader inputStreamReader) {
            reader = new BufferedReader(inputStreamReader);
        }

        public String next() {
            try {
                currentArray = null;
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public void nextChars(char[] t) {
            try {
                currentArray = null;
                reader.read(t);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public char nextChar() {
            try {
                currentArray = null;
                return (char) reader.read();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public int nextInt() {
            if ((currentArray == null) || (curPointer >= currentArray.length)) {
                try {
                    currentArray = reader.readLine().split(" ");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                curPointer = 0;
            }
            return Integer.parseInt(currentArray[curPointer++]);
        }

        public long nextLong() {
            if ((currentArray == null) || (curPointer >= currentArray.length)) {
                try {
                    currentArray = reader.readLine().split(" ");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                curPointer = 0;
            }
            return Long.parseLong(currentArray[curPointer++]);
        }

    }
}


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

import org.omg.CORBA.UNKNOWN;

public class HelloWorld {

    InputReader input;
    PrintWriter output;
    BufferedReader inp;

    void run(){
        output = new PrintWriter(new OutputStreamWriter(System.out));
        input = new InputReader(System.in);
        inp = new BufferedReader(new InputStreamReader(System.in));
        solve();
        output.flush();
    }

    public static void main(String[] args){
        new HelloWorld().run();
    }

    long stps;

    long gcd(long a, long b) {
        if(b == 0 || a == 0) {
            return 0;
        }
        return a/b + gcd(b, a%b);
    }

    void solve() {
        long a = input.readLong();
        long b = input.readLong();
        stps = gcd(a, b);
        output.println(stps);
    }

    class node implements Comparable<node>{
        int destination;
        int direction;
        int distance;

        public node(int destination, int distance, int direction) {
            this.direction = direction;
            this.distance = distance;
            this.destination = destination;
        }

        public int compareTo(node b) {
            return this.distance - b.distance;
        }
    }

    class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars)
            {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public Long readLong() {
            return Long.parseLong(readString());
        }

        public Double readDouble() {
            return Double.parseDouble(readString());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author dudkamaster
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskA {

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long a = in.nextLong();
        long b = in.nextLong();
        long ans = 0;
        while (a > 0 && b > 0){
            if (a < b){
                long t = a;
                a = b;
                b = t;
            }
            ans += a/b;
            a %= b;
        }
        out.print(ans);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader (InputStream stream){
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next(){
        while (tokenizer == null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (Exception e){
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public long nextLong(){
        return Long.parseLong(next());
    }

    }


import java.io.*;
import java.util.*;

public class Main implements Runnable {

	public void _main() throws IOException {
		long a = nextLong();
		long b = nextLong();
		long res = 0;
		while (b > 0) {
			res += a / b;
			long t = a % b;
			a = b;
			b = t;
		}
		out.println(res);
	}

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String next() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String rl = in.readLine();
			if (rl == null)
				return null;
			st = new StringTokenizer(rl);
		}
		return st.nextToken();
	}

	private int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	private long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	private double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.UK);
		new Thread(new Main()).start();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			//in = new BufferedReader(new FileReader("a.in"));
			//out = new PrintWriter(new FileWriter("a.out"));

			_main();

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(202);
		}
	}

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author sheep
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long a = in.nextLong();
        long b = in.nextLong();
        out.println(go(a, b));
    }

    private long go(long a, long b) {
        if (b == 0) return 0;
        return a / b + go(b, a % b);
    }
}

class InputReader {
    BufferedReader reader;
    StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (Exception e) {
                throw new UnknownError();
            }
        }
        return tokenizer.nextToken();
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    }


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Alex
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public long recurse(long a, long b) {
        if (b <= 1) return a;
        return Math.max(a, b)/Math.min(a,b) + recurse(Math.max(a, b) - Math.min(a, b)*(Math.max(a,b)/Math.min(a, b)), Math.min(a, b));
    }

    public void solve(int testNumber, InputReader in, OutputWriter out) {
        long a = in.readLong(), b = in.readLong(), i = 0;
//        while(true) {
//            if (b <= 1){
//                i += a;
//                out.print(i);
//                return;
//            }
//            i++;
//            long aa = Math.max(a, b);
//            long bb = Math.min(a, b);
//            aa -= bb;
//            a = aa;
//            b = bb;
//        }
//        out.print(i);
        out.print(recurse(a, b));
    }
}

class InputReader {
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public long readLong() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	public static boolean isWhitespace(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public void close() {
		writer.close();
	}
	public void print(long i) {
		writer.print(i);
	}
}


import java.util.Scanner;

public class C {
	static long n = 0;
		static void R (long a,long b){
			n += a/b;
			a = a%b;
			if(a==0) return;
			R(b,a);
		}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		R(a,b);
		System.out.println(n);
	}
}


import java.io.*;
import java.util.*;

public class CF {

	long getAns(long a, long b) {
		if (a == b)
			return 1;
		if (a < b) {
			return getAns(b, a);
		}
		// a > b
		long cnt = (a - 1) / b;
		return cnt + getAns(b, a - b * cnt);
	}

	void solve() {
		long a = in.nextLong();
		long b = in.nextLong();
		out.println(getAns(a, b));
	}

	FastScaner in;
	PrintWriter out;

	void run() {
		in = new FastScaner(System.in);
		out = new PrintWriter(System.out);

		solve();

		out.close();
	}

	void runWithFiles() {
		in = new FastScaner(new File("input.txt"));
		try {
			out = new PrintWriter(new File("output.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		solve();

		out.close();
	}

	public static void main(String[] args) {
		new CF().run();
	}

	class FastScaner {
		BufferedReader br;
		StringTokenizer st;

		FastScaner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		FastScaner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return null;
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

public class ProblemA {

	static final int INF = 100000000;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);

		String[] nmd = in.readLine().split(" ");
		long a = Long.valueOf(nmd[0]);
		long b = Long.valueOf(nmd[1]);

		long cnt = 0;
		while (true) {
			if (a == 0) {
				break;
			}
			if (a >= b) {
				cnt += a / b;
				a = a % b;
			} else {
				if (b % a == 0) {
					cnt += b / a - 1;
					b = a;
				} else {
					cnt += b / a;
					b = b % a;
				}
			}
		}
		out.println(cnt);
		out.flush();
	}

	public static void debug(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class A {
	static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st = new StringTokenizer("");

	static String readString() throws Exception {
		while(!st.hasMoreTokens())
			st = new StringTokenizer(stdin.readLine());
		return st.nextToken();
	}

	static int readInt() throws Exception {
		return Integer.parseInt(readString());
	}

	static long readLong() throws Exception {
		return Long.parseLong(readString());
	}
	public static void main(String[] args) throws Exception{
		long a = readLong();
		long b = readLong();

		System.out.println(rec(a,b));

	}

	private static long rec(long a, long b) {
		if(a == 1){
			return b;
		}
		if(a >= b){
			return a/b + rec(a%b, b);
		}
		return rec(b, a);
	}

}


import java.util.Scanner;

public class C344C {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long count = a / b, c;
		a = a % b;
		while(true){
			if (a <= 1 || b <= 1) break;
			c = b - a;
			b = a;
			a = c;
			count++;
			if (a > b) count += a / b;
			a = a % b;
		}
		if (b > 1) count += b;
		System.out.println(count);
		sc.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class RationalResistance {
	public static void main(String args[]) throws IOException{
		BufferedReader f= new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
		StringTokenizer st=new StringTokenizer(f.readLine());
		long a=Long.parseLong(st.nextToken());
		long b=Long.parseLong(st.nextToken());
		long sum = 0;
		while(a!= 0 && b!= 0){
			if (a > b){
				long val = a / b;
				sum += val;
				a -= val * b;
			}
			else{
				long val = b / a;
				sum += val;
				b -= val * a;
			}
		}

		out.println(sum);
		out.close();
		System.exit(0);
	}

}


import java.io.*;
import java.util.*;

public class Main {

    public static long func(long a, long b) {

        if (a < b)
            return func(b, a);
    	if (b < 2)
            return a;
        long q = a / b;
        long r = a - q * b;
        return q + func(b, r);
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ss = br.readLine().split(" +");
        long a = Long.parseLong(ss[0]);
        long b = Long.parseLong(ss[1]);

        System.out.println(func(a, b));

    }

}


import java.io.*;
import java.math.*;
import java.util.*;

import static java.lang.Math.*;

public class Main implements Runnable {

    public void solve() throws Throwable {
        long a = sc.nextLong();
        long b = sc.nextLong();
        long ans = 0;
        while (a > 0 && b > 0) {
            if (a > b) {
                ans += a/b;
                a %= b;
            } else {
                ans += b/a;
                b %= a;
            }
        }
        out.println(ans);
    }

    static Throwable t;

    BufferedReader reader;
    FastScanner sc;
    PrintWriter out;

    public static void main(String[] args) throws Throwable {
        Thread thread = new Thread(null, new Main(), "", 1 << 26);
        thread.start();
        thread.join();
        if (Main.t != null)
            throw t;
    }

    @Override
    public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            sc = new FastScanner(reader);
            solve();
        } catch (Throwable t) {
            Main.t = t;
        } finally {
            out.close();
        }
    }
}

class FastScanner {
    BufferedReader reader;
    StringTokenizer strtok;
    public FastScanner(BufferedReader reader) {
        this.reader = reader;
    }

    public String nextToken() throws IOException{
        while (strtok == null || !strtok.hasMoreTokens()) {
            strtok = new StringTokenizer(reader.readLine());
        }
        return strtok.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}


import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) throws IOException {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String line = stdin.readLine();
    String[] prms = line.split(" ");

    long a = Long.parseLong(prms[0]);
    long b = Long.parseLong(prms[1]);

    long cnt = 0;
    while (a > 0 && b > 0) {
      if (a >= b) {
        cnt += a / b;
        a =  a % b;
      }
      long c = a;
      a = b;
      b = c;
    }

    System.out.println(cnt);
  }
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class A {

    static StringTokenizer st;
    static BufferedReader br;
    static PrintWriter pw;
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        long x = nextLong();
        long y = nextLong();
        long ans = 0;
        while (x > 0 && y > 0) {
            if (x > y) {
                ans += x / y;
                x %= y;
            }
            else {
                ans += y / x;
                y %= x;
            }
        }
        System.out.println(ans);
    }
    private static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    private static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
    private static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
    private static String next() throws IOException {
        while (st==null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long a = in.nextLong();
        long b = in.nextLong();
        long res = 0;
        while (b > 0) {
            res += a / b;
            long t = a % b;
            a = b;
            b = t;
        }
        out.println(res);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    }


import java.util.*;
public class c {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    long a = input.nextLong(), b = input.nextLong();
    System.out.println(gcd(a, b));
}
static long gcd(long a, long b)
{
    if(b==1) return a;
    if(a==1) return b;
    if(a>b) return a/b + gcd(b, a%b);
    return b/a + gcd(a, b%a);
}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class A {

	public static void main(String[] args) throws Exception {
		new A().doit();
	}

	private void doit() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long a = Long.parseLong(st.nextToken());
		long b = Long.parseLong(st.nextToken());
		long ans = 0;
		while(a > 0 && b > 0) {
			if (a > b) {
				ans += a / b;
				a %= b;
			} else {
				ans += b / a;
				b %= a;
			}
		}

		System.out.println(ans);
	}

}


import java.util.*;

public class A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long A = in.nextLong();
        long B = in.nextLong();
        System.out.println(f(A,B));
    }
    static long f(long A, long B) {
        if(A==0) return 0;
        if(A < B) return f(B,A);
        else {
            long k = A/B;
            return k+f(A-B*k, B);
        }
    }
}


import java.util.*;

public class p343A
{
    static long n = 0;
    static void resistance(long a, long b)
    {
        n += a/b;
        a %= b;
        if(a!=0) resistance(b, a);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        long b = in.nextLong();
        resistance(a, b);
        System.out.println(n);
    }
}


import java.io.*;
import java.util.*;

public class RationalResistance {
    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(f.readLine());
        long a = Long.parseLong(st.nextToken());
        long b = Long.parseLong(st.nextToken());
        System.out.println(f(a,b));
    }

    public static long f(long a, long b)
    {
        if (a == 1 || b == 1)
            return a+b-1;
        if (a > b)
            return f(a%b,b) + a/b;
        else
            return f(a,b%a) + b/a;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	public long gcd(long a, long b) {
		long tmp;
		while (b > 0) {
			a %= b;
			tmp = a;
			a = b;
			b = tmp;
		}
		return a;
	}

	public void solve() throws Exception {
		long a = sc.nextLong();
		long b = sc.nextLong();
		long ans = 0;
		long k = 0;
		if (a == 1 || b == 1) {
			out.println(max(a, b));
			return;
		}
		while (a > 1 && b > 1) {
			if (a > b) {
				k = a / b;
				ans += k;
				a -= (k * b);
			} else {
				k = b / a;
				ans += k;
				b -= (k * a);
			}
			k = gcd(a, b);
			a /= k;
			b /= k;
		}
		if (a == 1)
			ans += b;
		else
			ans += a;
		out.println(ans);
	}

	static Throwable throwable;

	BufferedReader in;
	PrintWriter out;
	FastScanner sc;

	public static void main(String[] args) throws Throwable {
		Thread thread = new Thread(null, new Solution(), "", (1 << 26));
		thread.start();
		thread.join();
		if (throwable != null) {
			throw throwable;
		}
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable throwable) {
			this.throwable = throwable;
		} finally {
			out.close();
		}
	}

}

class FastScanner {
	BufferedReader reader;
	StringTokenizer strTok;

	public FastScanner(BufferedReader reader) {
		this.reader = reader;
	}

	public String nextToken() throws Exception {
		while (strTok == null || !strTok.hasMoreTokens()) {
			strTok = new StringTokenizer(reader.readLine());
		}
		return strTok.nextToken();
	}

	public int nextInt() throws Exception {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws Exception {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws Exception {
		return Double.parseDouble(nextToken());
	}
}


import java.util.Scanner;
import java.io.StreamTokenizer;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author KNIGHT0X300
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputStreamReader in = new InputStreamReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    StreamTokenizer in;
    PrintWriter out;
    BufferedReader re;
    Scanner sc;
    public void solve (int testNumber, InputStreamReader in, PrintWriter out)  {

          this.in = new StreamTokenizer(new BufferedReader(in));
          this.re =  new BufferedReader(in);
          this.sc = new Scanner(in);
          this.out = out;
          try {
                      this.solve();
                  } catch (IOException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
          out.flush();

    }

         void solve() throws IOException
       {
        long a=sc.nextLong(),b=sc.nextLong();
           long ans=0,t;
          while(Math.min(a,b)!=1){
              if(a>b){
                  ans+=a/b;
                  a%=b;

              }
              else{
                  t=b;
                  b=a;
                  a=t;
                  long g=gcd(a,b);
                  a/=g;b/=g;
              }

          }
           ans+=Math.max(a,b);
           out.println(ans);

       }
     public static long gcd(long a, long b) {
       long c = 0;

     if(a<0)  a=-a;
       if(b<0)  b=-b;
       while (b>0) {
          c = a % b;
          a = b;
          b = c;
       }
       return a;
     }

         // do the sum

}


import java.io.*;
import java.util.*;

public class cf {

	static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e){e.printStackTrace();}
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {st = new StringTokenizer(br.readLine());}
            catch (Exception e) {e.printStackTrace();}
            return st.nextToken();
        }

        public int nextInt() {return Integer.parseInt(next());}

        public long nextLong() {return Long.parseLong(next());}

        public double nextDouble() {return Double.parseDouble(next());}

        public String nextLine() {
            String line = "";
            if(st.hasMoreTokens()) line = st.nextToken();
            else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
            while(st.hasMoreTokens()) line += " "+st.nextToken();
            return line;
        }
    }
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        PrintWriter pw = new PrintWriter(System.out);

        int n = sc.nextInt();
        pw.println(n/2+1);
        pw.close();
    }
}


import java.io.PrintWriter;

import java.util.*;
import java.util.Arrays ;
import java .lang.String.* ;
import java .lang.StringBuilder ;

public class Test{
    static int pos = 0 ;
    static int  arr[] ;
  static LinkedList l1 = new  LinkedList() ;
static void find(int p ,char[]x,int put[],String s){
    int c= 0 ;
    for (int i = 0; i < s.length(); i++) {
        if(x[p]==s.charAt(i)){
        c++ ; }
    }
    put[p] = c ;
}
static int mode(int m ,int[]x ){
    int temp = 0 ;
    for (int i = x.length-1; i >=0; i--) {
        if(x[i]<=m){
            temp= x[i] ;
           /// break ;
             return m-temp ;

        }
    }
     return m-temp ;
}
static int mode2(int m ,int[]x ){
    int temp = 0 ;

    for (int i = x.length-1; i >=0; i--) {
        if(x[i]<=m){
            temp= x[i] ;
           /// break ;
             return x[i] ;

        }
    }
     return 0 ;
}
static int find(int x[],int temp){
    int j = 0 ;
    for (int i = x.length-1; i >=0; i--) {
        if(x[i]==temp) return j+1 ;
        j++ ;
    }
    return -1 ;
}
static String ch(long[]x,long b){
    for (int i = 0; i < x.length; i++) {
        if(x[i]==b)return "YES" ;
    }
    return "NO" ;
}

    public static void main(String[] args)  {

        Scanner in = new Scanner(System.in) ;
        PrintWriter pw = new PrintWriter(System.out);
        long n = in.nextLong() ;
        long count =1 ;
        long temp =n/2;
     temp+=count ;

        System.out.println(temp);
 }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class CodeForces
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

		System.out.println(input.nextInt() / 2 + 1);
	}
}


import java.util.*;
import java.io.*;

public class programA {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		if(n%2 == 0)System.out.println(n/2 +1);
		else System.out.println((int)Math.ceil((double)n/2));
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author Antonio "Teo" Alurralde
 */
public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    StringTokenizer tok = new StringTokenizer(br.readLine());
    int ax = Integer.parseInt(tok.nextToken());
    int ay = Integer.parseInt(tok.nextToken());
    tok = new StringTokenizer(br.readLine());
    int bx = Integer.parseInt(tok.nextToken());
    int by = Integer.parseInt(tok.nextToken());
    tok = new StringTokenizer(br.readLine());
    int cx = Integer.parseInt(tok.nextToken());
    int cy = Integer.parseInt(tok.nextToken());
    boolean ans = (bx < ax && cx < ax && by < ay && cy < ay) ||
    (bx < ax && cx < ax && by > ay && cy > ay) ||
    (bx > ax && cx > ax && by < ay && cy < ay) ||
    (bx > ax && cx > ax && by > ay && cy > ay);
    System.out.print(ans?"YES":"NO");
  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class KingEscape {

    public static void main(String[] args) {
        Reader read = new Reader();

        int n = read.nextInt();
        int a1 = read.nextInt();
        int a2 = read.nextInt();
        int b1 = read.nextInt();
        int b2 = read.nextInt();
        int c1 = read.nextInt();
        int c2 = read.nextInt();

        if (b1 > a1 && b2 > a2 && c1 > a1 && c2 > a2)
            System.out.print("YES");
        else if (b1 > a1 && b2 < a2 && c1 > a1 && c2 < a2)
            System.out.print("YES");
        else if (b1 < a1 && b2 > a2 && c1 < a1 && c2 > a2)
            System.out.print("YES");
        else if (b1 < a1 && b2 < a2 && c1 < a1 && c2 < a2)
            System.out.print("YES");
        else
            System.out.print("NO");
    }

    private static class Reader {

        private final BufferedReader reader;
        private final String separator;
        private String ln;
        private String[] tokens;
        private int ptr;

        Reader(String separator, InputStream input) {
            this.reader = new BufferedReader(new InputStreamReader(input));
            this.separator = separator;
            this.ptr = -1;
        }

        Reader(String separator) { this(separator, System.in); }

        Reader() { this(" "); }

        String nextStr(){
            if (Objects.isNull(ln)) {
                try {
                    ln = reader.readLine();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
                if (Objects.nonNull(ln)) {
                    tokens = ln.split(separator);
                    ptr = 0;
                } else {
                    throw new NoSuchElementException("no next element");
                }

            } else if (ptr == tokens.length) {
                ln = null;
                tokens = null;
                ptr = -1;
                return nextStr();
            }

            return tokens[ptr++];
        }

        int nextInt() { return Integer.parseInt(nextStr()); }

        long nextLong() { return Long.parseLong(nextStr()); }

        double nextDouble() { return Double.parseDouble(nextStr()); }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solve4 {

    public static void main(String[] args) throws IOException {
      FastReader sc = new FastReader();
      int x= sc.nextInt();
      int y= sc.nextInt();
      int z= sc.nextInt();
      int t1= sc.nextInt();
      int t2= sc.nextInt();
      int t3= sc.nextInt();
      if(Math.abs(x-y)*t1 < (Math.abs(x-z)+Math.abs(x-y))*t2+3*t3 ) System.out.println("NO");
      else System.out.println("YES");
    }

    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException ex) {
            }
            return s;
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main (String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String parameterStringList[] = reader.readLine().split(" ");
            int x = Integer.parseInt(parameterStringList[0]);
            int y = Integer.parseInt(parameterStringList[1]);
            int z = Integer.parseInt(parameterStringList[2]);
            int t1 = Integer.parseInt(parameterStringList[3]);
            int t2 = Integer.parseInt(parameterStringList[4]);
            int t3 = Integer.parseInt(parameterStringList[5]);

            int T1 = Math.abs(x-y) * t1;
            int T2 = Math.abs(x-z) * t2 + 3*t3 + Math.abs(x-y) * t2;

            if(T2 <= T1) System.out.println("YES");
            else System.out.println("NO");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ElevatorOrStairs {

	private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	private static final OutputStreamWriter writer = new OutputStreamWriter(System.out);

	public static void main(String...strings) throws Exception {
		String[] specs = reader.readLine().split(" ");

		int x = Integer.parseInt(specs[0]);
		int y = Integer.parseInt(specs[1]);
		int z = Integer.parseInt(specs[2]);
		int t1 = Integer.parseInt(specs[3]);
		int t2 = Integer.parseInt(specs[4]);
		int t3 = Integer.parseInt(specs[5]);

		reader.close();
		String ans = solve(x, y, z, t1, t2, t3);
		writer.append(ans);
		writer.flush();
		writer.close();
	}

	private static String solve(int x, int y, int z, int t1, int t2, int t3) {
		int time_using_stairs = Math.abs(x - y) * t1;
		int elevator_time_between_floor = Math.abs(x - z) * t2;
		int elevator_from_z_to_x = elevator_time_between_floor + 2*t3;

 		int time_using_elevator = elevator_from_z_to_x + (Math.abs(x - y) * t2) + t3;

 		if(time_using_elevator <= time_using_stairs) {
 			return "YES";
 		}
 		return "NO";
	}
}


    import java.util.Scanner;

    public class Sasha1113A {

        static int solution(int n, int v){
            int count;
            if(v>=n)
                return n-1;
            else{
                count = (v-1) + ((n-v)*(n-v+1))/2;
            }
            return count;
        }

        public static void main(String[] args){
            Scanner scan = new Scanner(System.in);
            int n = scan.nextInt();
            int v = scan.nextInt();

            System.out.print(solution(n, v));
        }
    }


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Stack;

import java.util.StringTokenizer;

 public class Test
 {

     static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args)throws Exception
    {
        Reader.init(System.in);
        int n = Reader.nextInt();
        int p = Reader.nextInt();
        int L = Reader.nextInt();
        int R = Reader.nextInt();
        int a = 1;
        int b = n;
        int res = 0;

        if(a == L && b == R)
        {
            res = 0;
        }
        else if(L != a && R != b && p >= L && p <= R)
        {
            res = Math.min(p-L, R-p);
            res += R- L + 2;
        }
        else if(L != a && R != b && p < L )
        {
            res += L-p + 1;
            res += R - L +1;
        }
        else if(L != a && R != b && p > R)
        {
            res += p-R + 1;
            res += R - L +1;
        }
        else if(a == L && p >=L && p<=R)
        {
            res += R - p + 1;
        }
        else if(R == b && p>=L && p<=R)
        {
            res += p - L + 1;
        }
        else if(a == L && p > R)
        {
            res += p - R + 1;
        }
        else if(R == b && p<L)
        {
            res += L - p + 1;
        }

        pw.print(res);
        pw.close();

    }

}

class Reader {

    static BufferedReader reader;
    static StringTokenizer tokenizer;

    public static int pars(String x) {
        int num = 0;
        int i = 0;
        if (x.charAt(0) == '-') {
            i = 1;
        }
        for (; i < x.length(); i++) {
            num = num * 10 + (x.charAt(i) - '0');
        }

        if (x.charAt(0) == '-') {
            return -num;
        }

        return num;
    }

    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static void init(FileReader input) {
        reader = new BufferedReader(input);
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return pars(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}


import java.util.Scanner;

public class LuckyDivision {
	public static void main (String[] args) {
		Scanner read = new Scanner(System.in);
		int n = read.nextInt();

		if (n % 4 == 0 ||
			n % 7 == 0 ||
			n % 47 == 0 ||
			n % 74 == 0 ||
			n % 447 == 0 ||
			n % 474 == 0 ||
			n % 477 == 0 ||
			n % 744 == 0 ||
			n % 747 == 0 ||
			n % 774 == 0)	{
				System.out.println("YES");
			}
		else {
				System.out.println("NO");
		}

	}
}


import java.util.Scanner;
public class LuckyDivison
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int inp = in.nextInt();
        if(inp%4==0||inp%7==0||inp%47==0||inp%74==0||inp%447==0||inp%474==0||inp%477==0||inp%747==0||inp%774==0||inp%777==0)
        {
          System.out.println("YES");
        }
        else System.out.println("NO");

    }
}


import java.io.*;
import java.util.*;

public class Test{
    public static void main(String[] args) {
        Scanner in= new Scanner(System.in);
        int n=in.nextInt();
        if(n%7==0 || n%4==0 || n%47==0 || n%74==0 || n%447==0 || n%474==0 || n%477==0 || n%747==0 || n%774==0){
            System.out.println("YES");
        }else
            System.out.println("NO");

    }
}