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

ans = x
e = n-x
count = 0
while True:
    
    if x%e == 0:
        if count == 0:
            ans += x//e*(e*2)
        else:
            ans += x//e*(e*2)-e
        break
    else:
        if count%2 == 0:
            ans += e+(x//e)*e*2
        else:
            ans += (x//e)*e*2
        x,e = e,x%e
    count += 1
    
print(ans)