Problem:
 Score : 500 points    Problem Statement Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light .  Three mirrors of length N are set so that they form an equilateral triangle.
Let the vertices of the triangle be a, b and c .  Inside the triangle, the rifle is placed at the point p on segment ab such that ap = X .
(The size of the rifle is negligible.)
Now, the rifle is about to fire a ray of Mysterious Light in the direction of bc .  The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light.
There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror!
When the ray comes back to the rifle, the ray will be absorbed.  The following image shows the ray's trajectory where N = 5 and X = 2 .     It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of N and X .
Find the total length of the ray's trajectory.      Constraints  2≦N≦10^{12}  1≦X≦N-1  N and X are integers.       Partial Points  300 points will be awarded for passing the test set satisfying N≦1000 .  Another 200 points will be awarded for passing the test set without additional constraints.         Input The input is given from Standard Input in the following format:  N  X       Output Print the total length of the ray's trajectory.        Sample Input 1 5 2      Sample Output 1 12  Refer to the image in the Problem Statement section.
The total length of the trajectory is 2+3+2+2+1+1+1 = 12 .

Solution (Python):
n, x = map(int,input().split())

if n % 2 == 0 and n//2 == x:
    print(3 * x)
    exit()

if x > n // 2:
    x = n - x

xx = x
yy = n - x


ans = n
i = 0

while True:
    
    if yy >= xx:
        ans += 2 * (yy // xx) * xx
        yy %= xx

        if yy == 0:
            ans -= xx
            break

        if yy < xx:
            tmp = xx
            xx = yy
            yy = tmp

print(ans)

Solution (Python):
N, X = map(int, input().split())
ans = N
N -= X
while X > 0:
    N, X = max(N, X), min(N, X)
    ans += N // X * X * 2
    N, X = X, N % X
ans -= N

print(ans)

Solution (Python):
N, X = map(int, input().split())
Y = N - X

ans = N

while True:
	if X == Y:
		ans += X
		break
	else:
		if X > Y:
			tmp = X
			X = Y
			Y = tmp
		a = Y % X
		b = Y // X
		if a == 0:
			ans += (b*2 - 1) * X
			break
		else:
			ans += b*2*X
		Y = X
		X = a
print(ans)

Solution (Python):
n, x = map(int, raw_input().split())
s = n
r = 1
if n - x > x:
    a, b = n - x ,x
else:
    a, b = x, n - x
while r > 0:
    q = a / b
    r = a % b
    if r > 0:
        s += 2 * q * b
    else:
        s += (2 * q - 1) * b
    a, b = b, r
print s

Solution (Python):
N,X = map(int,raw_input().split(' '))
l = 0
N, X = max(X,N-X), min(X,N-X)
while True:
	l += (N/X)*X*3
	if N%X==0: break
	N, X = X, N%X
print l

Solution (Python):
n, x = map(int, input().split())
ans, e = n, n-x
while x > 0:
    ans += x * (e//x*2)
    e, x = x, e%x
print(ans-e)

Solution (Python):
def f(a, b):
    a, b = min(a, b), max(a, b)
    return (b // a) * 2 * a + (f(a, b % a) if b % a > 0 else -a)
n, x = map(int, input().split())
print(f(x, n - x) + n)

Solution (Python):
N, X = map(int,input().split())
ans = N
u = N-X
d = X

while(d!=0):
    ans += d*(u//d)*2
    t = u
    u = d
    d = t%d

print(ans-u)

Solution (Python):
from fractions import gcd
n, k = map(int, input().split(' '))
print(3*(n-gcd(n, k)))

Solution (Python):
def sub(a, b):
    if (a%b == 0):
        return a//b-1
    return a//b

def mod(a, b):
    if a%b==0:
        return b
    return a%b

n, x = map(int, input().split(' '))
ans = n
c = [x, n-x]
while (c[0] != c[1]):
    c = [min(c), max(c)]
    ans += 2*sub(c[1], c[0])*c[0]
    c[1] = mod(c[1], c[0])
print(ans+c[0])

Solution (Python):
import sys
sys.setrecursionlimit(1500)

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

N, X = list(map(int, input().split()))
print(f(X, N-X) + N)

Solution (Python):
tmp = [int(x) for x in raw_input().split(' ')]
N = tmp[0]
X = tmp[1]

if(X > 0.5 * N):
	X = N-X

totaldist = N
step = X
dist = N-X

while(1):
	totaldist += (dist/step) * 2 * step
	distnew = step
	stepnew = dist % step
	if(stepnew == 0):
		totaldist -= step
		break
	dist = distnew
	step = stepnew

print totaldist

Solution (Python):
args = [ int(x) for x in raw_input().split() ]
l0 = args[0]
x0 = args[1]

def get_length(l, x):
    if l % x == 0:
        part = l * 2 - x
        
        return part
    part = int(l / x) * 2 * x
    
    return part + get_length(x, l % x)

length = l0 + get_length(l0 - x0, x0)

print length

Solution (Python):


def f(a, b):
    if a < b : b, a = a, b
    c = a // b
    r = b * 3 * c
    if a % b > 0 : r += f(b, a-c*b)
    return r
N, X = map(int, input().split())
print(f(X, N-X))

Solution (Python):
n = map(int, raw_input().split())
x = n[1]
n = n[0]
tpl = x

if x < float(n)/2:
    tps = abs(n - 2*x)
    l = x + n - tps
else:
    l = 0
    tps = n - x
while tps != 0:
    
    
    l += tpl/tps * 3*tps  
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l

Solution (Python):
"""
def solve(N, X):
	xmax = ymax = zmax = N
	xmin = ymin = zmin = 0

	org = [N-X, X, 0]
	pos = [N-X, X, 0]
	dir = (0, -1, 1)
	ans = 0

	while True:
		if dir == (0,-1,1):
			if pos[1]-ymin < zmax-pos[2]:
				d = pos[1]-ymin
				pos[1] -= d
				pos[2] += d
				dir = (-1,1,0)
				xmax = pos[0]
			else:
				d = zmax-pos[2]
				pos[1] -= d
				pos[2] += d
				dir = (1,0,-1)
				xmin = pos[0]
		elif dir == (1,0,-1):
			if pos[2]-zmin < xmax-pos[0]:
				d = pos[2]-zmin
				pos[2] -= d
				pos[0] += d
				dir = (0,-1,1)
				ymax = pos[1]
			else:
				d = xmax-pos[0]
				pos[2] -= d
				pos[0] += d
				dir = (-1,1,0)
				ymin = pos[1]
		elif dir == (-1,1,0):
			if pos[0]-xmin < ymax-pos[1]:
				d = pos[0]-xmin
				pos[0] -= d
				pos[1] += d
				dir = (1,0,-1)
				zmax = pos[2]
			else:
				d = ymax-pos[1]
				pos[0] -= d
				pos[1] += d
				dir = (0,-1,1)
				zmin = pos[2]
		else:
			print "error"
			break

		ans += d

		if pos==org:
			break
	return ans

for N in range(2, 17):
	print N,
	for X in range(1, N):
		print solve(N, X)/3,
	print

"""

import fractions
N, X = map(int, raw_input().split())
g = fractions.gcd(N, X)
print 3*g*(N/g-1)

Solution (Python):
N,X = map(int,raw_input().split())
X = min(X,N-X)
ans = 0
N -= X
while X > 0:
	ans += N/X * X *3
	N,X= X,N%X
print ans

Solution (Python):


N, X = list(map(int, input().split()))
sum_ = X
prev = X
next = N - X
i = 0
while True:
    num = prev // next
    if i == 0:
        sum_ += 2 * num * next
    elif i % 2 == 1:
        sum_ += 2 * num * next - next
    else:
        sum_ += 2 * num * next - next
    mod_ = prev % next
    if mod_ == 0:
        break
    sum_ += next
    prev = next
    next = mod_
    i += 1
print(sum_)

Solution (Python):
def calc(N,X):
    if N%X ==0:
        return 3*N
    else:
        return 3*X*(int(N/X)) + calc(X,N-X*(int(N/X)))


N,X = list(map(int, input().split()))
X = min(X,N-X)
ans = calc(N-X,X)
print('%s' % str(ans))

Solution (Python):
n, x = map(int, raw_input().split())

s = 0
t = n - x
while t % x != 0:
	m = t % x
	s += x * (t / x)
	t = x
	x = m
s += x * (t / x)

print s * 3

Solution (Python):
n,x=map(int,raw_input().split())
y=n-x
ans=x+y
if y>=x:
    while 1:
        if y%x==0:
            ans+=x+((y-x)/x)*x*2
            print(ans)
            break
        else:
            ans+=((y)/x)*x*2
            y,x=x,y%x
else:
    while 1:
        if x%y==0:
            ans+=y+((x-y)/y)*y*2
            print(ans)
            break
        else:
            ans+=((x)/y)*y*2
            x,y=y,x%y

Solution (Python):
n, x = map(int,raw_input().split())
x = min(n-x,x)
ans = 0
def cal(ans,l,s):
    ans += l/s*s*3
    if l%s == 0:
        return ans
    else:
        return cal(ans,max(s,l%s),min(s,l%s))
print cal(ans,n-x,x)

Solution (Python):
n,x=map(int,input().split())
if x>n/2:
    x = n-x
ans = n
c = x
d = n-x
while c > 0:
    if d%c==0:
        ans += int(d/c)*(2*c) - c
    else:
        ans += int(d/c)*(2*c)
    c,d = d%c,c
print(ans)

Solution (Python):

import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

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


if X > N/2:
    alpha = X = N - X

ans = N
R = N - X
while R and X:
    if R%X == 0:
        ans += X + 2*(R/X - 1)*X
        break
    K = R/X
    ans += 2*K*X
    if R == 1: break
    R, X = X, R%X
print ans

Solution (Python):
from fractions import gcd
N, X = map(int, input().split())
print(N * 3 - 3 - (gcd(N, X) - 1) * 3)

Solution (Python):
n,x=map(int, input().split())
total = n
h = n-x
w = x
while h and w:
    if h == w:
        total += h
        h = 0
        w = 0
        break
    elif h > w:
        total += (h//w)*2*w
        h %= w
    else:
        total += (w//h)*2*h
        w %= h
total -= max(w,h)
print(total)

Solution (Python):



import functools

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
    















def func_fast(N, X):
    g = gcd(N, X)
    if g == 1:
        return 3 * N - 3
    else:
        return g * func_fast(N // g, X // g)

N,X = map(int,input().split())
print(func_fast(N,X))

Solution (Python):
def rec(a, b):
    mx = max(a, b)
    mn = min(a, b)
    
    if mx % mn == 0:
        return (2 * (mx // mn) - 1) * mn
    return (2 * mn * (mx // mn)) + rec(mn, mx % mn)

N, X = list(map(int, input().split()))


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

Solution (Python):
if __file__.startswith('/tmp'):
	import sys; sys.stdin = open('input.txt')

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

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

print n + foo(x, n - x)

Solution (Python):
N, X = map(int, input().split())

def solve(a, b):
    d = a // b
    m = a % b
    if m==0:
        return (d * 2 - 1) * b
    else:
        return d * 2 * b + solve(b, m)

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

Solution (Python):

def f(n, x):
    if n % x == 0:
        return (n // x * 2 - 1) * x
    else:
        return (n // x * 2) * x + f(x, n % x)
n, x = map(int,input().split())
ans = n + f(n - x, x)
print(ans)

Solution (Python):
from fractions import gcd
n, x = map(int, raw_input().split())
g = gcd(n, x)
n /= g
x /= g
print 3 * (n - 1) * g

Solution (Python):
N,X=map(int,input().split())
A=N

X,N = sorted((X,N-X))
while N != X and X != 0:
    if N%X == 0:
        A += 2*(N//X-1)*X
        X,N=X,X
    else:
        A += 2*(N//X)*X
        X,N=sorted((X,N%X))
print(A+X)

Solution (Python):
n, x = map(int, raw_input().split())


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

print solve(x, n-x) + n