m, n = map(int, input().split()) ans = -2 while n != 0: ans += m // n + 1 m, n = n, m % n print(ans)