import sys
input = sys.stdin.readline

def f(a, b):
    if b % a == 0:
        return b // a * 2 * a - a
    return 2 * (b // a) * a + f(b % a, a)

def main():
    N, X = map(int, input().split())
    print(N + f(X, N-X))

if __name__ == '__main__':
    main()