import math M, N = map(int, input().split()) g = math.gcd(M, N) M, N = M//g, N//g if M == N: exit(print(0)) ans = 0 while not (M == 0 and N == 1): if M > N: sho, amari = divmod(M, N) ans += sho M -= N*sho else: M, N = N, M ans += 1 ans -= 1 print(ans)