N, X = list(map(int, input().split()))

ans = N
N = N - X
while N:
  if X < N:
    ans += X * 2
    N, X = X, N - X
  else:
    d, m = divmod(N, X)
    ans += d * X + m * 2
    N, X = m, X - m

print(ans)