N, X = map(int, raw_input().split())

def f(a, b):
  if a == 0:
    return -b
  elif b == 0:
    return -a
  elif a < b:
    return f(a, b % a) + (b / a) * 2 * a
  else:
    return f(a % b, b) + (a / b) * 2 * b

print N + f(X, N - X)

