source_code: 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());
  		}
  	}
  }
  