import sys

def reflect(A, B):
    if A > B:
        A, B = B, A

    ref = B // A
    if B % A == 0:
        return (ref * 2 - 1) * A
    else:
        return ref * 2 * A + reflect(B % A, A)


N, X = map(int, sys.stdin.readline().rstrip().split())
print(N + reflect(X, N - X))