import sys

N,X = map(int,input().split())
ans = 0
oldx = 0

if N > 2 * X:

    ans = N
    N = N - X

elif N == 2 * X:
    ans = 3 * X
    print (ans)
    sys.exit()

else:

    ans = N
    oldx = X
    X = N - X
    N = oldx

while True:

    #print (N,X,ans)

    if N % X == 0:
        ans += ((N // X) * 2 - 1) * X
        break

    else:
        ans += ((N // X) * 2 * X)

        oldx = X
        X = N % X
        N = oldx

    

print (ans)
