# -*- coding: utf-8 -*- import math M,N = map(int, input().split()) gcd_mn = math.gcd(M, N) M //= gcd_mn N //= gcd_mn ans = 0 # MとNが共に1,もしくは片方が負数となった時点でループを抜ける while True: if (M/N)<1: temp = M M = N N = temp else: M -= N ans += 1 if (M==1 and N==1) or M<0 or N<0: break if M==1 and N==1: print(ans) else: print(-1)