import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); int min = Math.min(N, M); int max = Math.max(N, M); scan.close(); if(N == M) { System.out.println(0); System.exit(0); } // m <= n // ユークリッドの互除法 int n = max; int m = min; int r = -1; while (r != 0) { r = n % m; n = m; m = r; } int a = N / n; int b = M / n; // M / N = a / b int cnt = 0; int d1, n1; if(a > b) { d1 = b; n1 = a; }else { d1 = a; n1 = b; cnt ++; } if(d1 == 1) { cnt += n1 - 1; System.out.println(cnt); System.exit(0); } int d2, n2; int cnt1 = 0; while(n1 != 1) { int t = n1 / d1; cnt1 ++; cnt += t; //System.out.println("t " + t + " cnt " + cnt); //System.out.println(n1 + " " + d1); d2 = n1 % d1; n2 = d1; n1 = n2; d1 = d2; if(d1 == 1) { cnt += n1; break; } } System.out.println(cnt + cnt1 - 1); } }