def f(a, b):
    if a == 0 or b == 0:
        return 0
    if a == 1 and b == 1:
        return 1
    r = min(a, b)
    q = max(a, b)
    return 2*r + f(r, q-r)

N, X = map(int, raw_input().split())

print N + f(X, N-X)