# 解説AC

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

ans = N
a, b = max(N - X , X), min(N - X, X)
# Euclidの互除法
while b:
    q, r = divmod(a, b)

    ans += (b * 2) * q
    if r == 0: ans -= b # 最後

    a, b = b, r

print(ans)