// yukicoder: 141 魔法少女コバ // 2019.5.209 bal4u #include int gcd(int a, int b) { int r; while (b != 0) r = a % b, a = b, b = r; return a; } int main() { int t, M, N, ans; scanf("%d%d", &M, &N); t = gcd(M, N); if (t > 1) M /= t, N /= t; if (M == 1 && N == 1) ans = 0; else if (N == 1) ans = M-1; else { ans = 0; while (M > 1) { if (M > N) ans += M/N, M %= N; if (M > 1) ans++, t = M, M = N, N = t; } ans += N; } printf("%d\n", ans); return 0; }