def calc(a,b,ans):
  if b==0:
    return ans
  else:
    if b!=1:
      return calc(b,a%b,ans+b*3)
    else:
      return calc(b,a%b,ans+a*3)

n,x=map(int,input().split())
n=n-x
n,x=max(n,x),min(n,x)
print(calc(n,x,0))