n,x = map(int,input().split())

if 2 * x == n:
    print(3 * x)
    exit()
elif 2 * x < n:
    res = 2 * x + n
    a,b = sorted([x,n-2*x])
else:
    res = n
    a,b = n-x, x

while a!=b:
    res += a * 2
    a,b = sorted([a,b-a])
print(res+a)
