import math m, n = [int(x) for x in input().split()] res = 0 cnt = 0 for i in range(10 ** 9): d = math.gcd(m, n) m //= d n //= d if m == 1 and n == 1: res = cnt break elif m < 1 or n < 1: res = -1 break if m < n: m, n = n, m cnt += 1 elif n == 1: cnt += m - 1 m = 1 else: cnt += m // n m = m % n print(res)