codeparrot/codecomplex huggingface dataset:
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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);
    }


}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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();
    }

}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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++]);
        }

    }
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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());
    }

    }
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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);
		}
	}

}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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());
    }

    }
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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);
	}
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant

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);
	}
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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());
		}
	}
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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));
	}
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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);
	}

}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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();
	}

}
// Problem: 343_A. Rational Resistance
// Time complexity: constant

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);
	}
	
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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));
        
    }
    
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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());
    }
}
// Problem: 343_A. Rational Resistance
// Time complexity: constant
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);
  }
}