Memo={(1,1):0} def f(a,b,r): if b==1: return r+(a-1) x,y=a//b,a%b return f(b,y,r+x+1) import math M,N=map(int,input().split()) g=math.gcd(M,N) M//=g N//=g print(f(M,N,0))