n, x = map(int, raw_input().split())
ans = x
while 0 < x < n:
    if 2*x <= n:
        ans += n
        n, x = n-x, n-2*x
    else:
        k = x / (n-x)
        rest = x % (n-x)
        if rest:
            ans += (2*k+1)*(n-x) + rest
        else:
            ans += 2*k*(n-x)
        n, x = n-x, rest
print ans
