n,x=map(int,raw_input().split())
y=n-x
ans=x+y
if y>=x:
    while 1:
        if y%x==0:
            ans+=x+((y-x)/x)*x*2
            print(ans)
            break
        else:
            ans+=((y)/x)*x*2
            y,x=x,y%x
else:
    while 1:
        if x%y==0:
            ans+=y+((x-y)/y)*y*2
            print(ans)
            break
        else:
            ans+=((x)/y)*y*2
            x,y=y,x%y