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

ans = N
N, X = X, N - X
while N and X:
  if X > N:
    d, m = divmod(X, N)
    if m == 0:
      ans += (d * 2 - 1) * N
    else:
      ans += d * 2 * N
    N, X = N, m
  else:
    d, m = divmod(N, X)
    ans += (d * 2 - 1) * X + m * 2
    N, X = m, X - m

print(ans)