import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def f(a, b):
    if a == 0 or b == 0:
        return 0
    if a > b:
        q, r = divmod(a, b)
        return 2*q*b + f(a % b, b)
    else:
        return a+b+f(b-a, a)

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

print(X + f(X, N - X))