n,x = map(int,input().split())

res = n
a = x #横
b = n-x #縦


while True:
    if a == b:
        res += a
        break
    elif a > b:
        k = a//b
        c = a%b
        if c == 0:
            res += (2*k-1)*b
            break
        else:
            res += 2*k*b
            a = c
    else:
        k = b//a
        c = b%a
        if c == 0:
            res += (2*k-1)*a
            break
        else:
            res += 2*k*a
            b = c
           

print(res)
