import fractions def solver(): gcd = fractions.gcd(M, N) s = [M//gcd, N//gcd] depth = 0 while 1: if s[0] == s[1]: # 1 break if s[0] > s[1]: if s[1] == 1: depth += s[0] - 1 break else: depth += 1 s = [s[0]-s[1], s[1]] else: depth += 1 s = [s[1], s[0]] if s[1] == 1: depth += s[0] - 1 break return depth M, N = map(int, input().split(" ")) if M == 0: print(-1) else: print(solver())