N, X = map(int, input().split(" "))
ans = X
count = 0
while(True):
  if count == 0:
    ans = ans + N - X
    N -= X
    if N < X:
      tmp = N
      N = X
      X = tmp
    count = 1
  elif count == 1:
    if N%X == 0:
      ans += (N*2-X)
      break
    else:
      ans = ans + X
      X = N-X
      if N < X:
        tmp = N
        N = X
        X = tmp
      count = 0
  if X == 0:
    break
print(ans)