n, x = map(int,raw_input().split())
x = min(n-x,x)
ans = 0
def cal(ans,l,s):
    ans += l/s*s*3
    if l%s == 0:
        return ans
    else:
        return cal(ans,max(s,l%s),min(s,l%s))
print cal(ans,n-x,x)