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

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

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

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

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

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

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

}