import java.util.Scanner; public class Main_yukicoder141 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int gcd = gcd(m, n); m /= gcd; n /= gcd; int ret = 0; while (m != 1 || n != 1) { if (n == 1) { ret += m - 1; break; } else if (m > n) { ret += m / n; m %= n; } else { int tmp = m; m = n; n = tmp; ret++; } gcd = gcd(m, n); m /= gcd; n /= gcd; } System.out.println(ret); sc.close(); } private static int gcd(int n, int m) { if (m == 0) { return n; } else { return gcd(m, n % m); } } }