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 to form an equilateral triangle with vertices a, b, and c. Inside the triangle, the rifle is placed at point p on segment ab such that ap = X. The rifle is about to fire a ray of Mysterious Light in the direction of bc. The ray will travel in a straight line and be reflected by mirrors and its own trajectory. When the ray comes back to the rifle, it will be absorbed. 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.

# Buggy version:
n, x = map(int, input().split())
a, b = max(n-x,x),min(n-x,x)
res = a+b
while b!=0:
    q = a/b
    r = a%b
    res += (b*2)*q
    if (r==0):
        res -= b
    a, b = b, r

print(res)

# Fixed version:
n, x = map(int, input().split())
a = max(n-x, x); b = min(n-x, x)
res = n
while b!=0:
    p=a; q=int(a/b); r=a%b
    res+=q*(2*b)
    if r==0:
        res-=b
    a=b; b=r

print(res)

# Buggy version:
import math
n, x = map(int, input().split())
print(3 * (n - gcd(n, x)))


# Fixed version:
import math
n,x=map(int,input().split())
print(3*(n-math.gcd(n,x)))

# Buggy version:
N,X = input().split()
N,X = int(N), int(X)
answer = X+ (N-X)


def rhombus(N,X):
  	if N<X:
        temp = N
        N = X
        X = temp 
        
    if X==1: 
        return N+1
    if X==0:
        return 0

    if N%X == 0:
        return X*(N//X)*2-X
      
    else: 
        k = N//X
        return k*2*X + rhombus(X,N-(k*X))

      

answer = answer + rhombus(N-X,X)
print(answer)

# Fixed version:
N,X = input().split()
N,X = int(N), int(X)
answer = X+ (N-X)


def rhombus(N,X):
  	
    if X==1: 
        return N*2-1
    if X==0:
        return 0

    if N%X == 0:
        return X*(N//X)*2-X
      
    else: 
        k = N//X
        return k*2*X + rhombus(X,N-(k*X))

      

answer = answer + rhombus(N-X,X)
print(answer)

# Buggy version:
n,x = [int(i) for i in input().split()]
length = n
n=n-x
while True:
    if x==0:
        break
    t=n//x
    length+=2*t*x
    a=n
    n=x
    x=a-t*x
print(length)

# Fixed version:
n,x = [int(i) for i in input().split()]

length = n
n=n-x
while True:
    t=n//x
    length+=2*t*x
    a=n
    n=x
    x=a-t*x
    if x==0:
        length-=n
        break
print(length)

# Buggy version:
n, x = map(int, input().split())
ans = 0
a, b = max(n-x,x),min(n-x,x)
res = a+b
while b:
    q = a/b
    r = a%b
    res += (b*2)*q
    if (r==0):
        res -= b
    a, b = b, r

print(res)

# Fixed version:
n, x = map(int, input().split())
a, b = max(n-x,x),min(n-x,x)
res = a+b
while b!=0:
    q = a//b
    r = a%b
    res += (b*2)*q
    if (r==0):
        res -= b
    a, b = b, r

print(res)

# Buggy version:
N,X = map(int,input().split())
x = X
y = N-X
if x<y:
    x,y = y,x
res = N
while y>0:
    x,y = y,x
    print(x,y)
    m = x*(y//x)
    res += m*2
    y -= m
res -= x
print(res)

# Fixed version:
N,X = map(int,input().split())
x = X
y = N-X
if x<y:
    x,y = y,x
res = N
while y>0:
    x,y = y,x
    m = x*(y//x)
    res += m*2
    y -= m
res -= x
print(res)

# Buggy version:
def main():
    n,x=map(int,input().split())
    a,b=n-x,x
    if(a<b):
       a,b=b,a
    path_length = calc_path(a,b,n)
    print(path_length)

def  calc_path(a1,b1,c1):
    q , mod=divmod(a1,b1)
    count=0
    if mod==0:
         print(c1)
         c2=c1+2*b1*q-b1
         print(c2)
         return c2
    else:
        count=count+1
        c2=c1+2*b1*q
        a2=a1-b1*q
        b2=b1
        if(a2<b2):
           a2,b2=b2,a2
        return calc_path(a2,b2,c2)
if __name__=='__main__':
  main()


# Fixed version:
def main():
    n,x=map(int,input().split())
    a,b=n-x,x
    if(a<b):
       a,b=b,a
    path_length = calc_path(a,b,n)
    print(path_length)

def  calc_path(a1,b1,c1):
    q , mod=divmod(a1,b1)
    count=0
    if mod==0:
         c2=c1+2*b1*q-b1
         return c2
    else:
        count=count+1
        c2=c1+2*b1*q
        a2=a1-b1*q
        b2=b1
        if(a2<b2):
           a2,b2=b2,a2
        return calc_path(a2,b2,c2)
if __name__=='__main__':
  main()

# Buggy version:
def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)

n, x = map(int, input().split())
print(3*gcd(n, x))

# Fixed version:
def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)

n, x = map(int, input().split())
print(3*(n - gcd(n, x)))

# Buggy version:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

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

def rec(a: int, b: int) -> int:
    mx = max(a,b)
    mn = min(a,b)
    if gcd(a,b) == mn:
        return (2 * (mx // mn) - 1) * mn
    
    else:
        return (2 * (mx // mn) * mn)  + rec(mn, mn * (mx // mn))
    
n,x = li()
print(n + rec(x, n-x))

# Fixed version:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

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

def rec(a: int, b: int) -> int:
    mx = max(a,b)
    mn = min(a,b)
    if gcd(a,b) == mn:
        return (2 * (mx // mn) - 1) * mn
    
    else:
        return (2 * (mx // mn) * mn)  + rec(mn, mx - mn * (mx // mn))
    
n,x = li()
print(n + rec(x, n-x))

# Buggy version:
# your code goes here
from math import floor
N, X = [int(x) for x in input().split()]

y = N-X
x = X
L = X
while x > 0:
#	k = 2*floor(y/x)
	k, z = divmod(y, x)
	dL = x*(2*k-1)+y
	L += x*(2*k-1)+y
	print(x, y, k, dL)
	y, x = (x, z)

#L += y
print(L)

# Fixed version:
# your code goes here
from math import floor
N, X = [int(x) for x in input().split()]

y = N-X
x = X
L = X
while x > 0:
#	k = 2*floor(y/x)
	k, z = divmod(y, x)
#	dL = x*(2*k-1)+y
	L += x*(2*k-1)+y
#	print(x, y, k, dL)
	y, x = (x, z)

#L += y
print(L)

# Buggy version:
N, X = map(int, input().split())
path = N
a, b = X, N - X
if a < b:
    a, b = b, a
while b > 0:
    print(a, b)
    path += b * (a // b) * 2
    a, b = b, a % b
path -= a
print(path)

# Fixed version:
N, X = map(int, input().split())
path = N
a, b = X, N - X
if a < b:
    a, b = b, a
while b > 0:
    path += b * (a // b) * 2
    a, b = b, a % b
path -= a
print(path)

# Buggy version:
def gcd(a,b):
  if a%b==0:
    return b
  else:
    return gcd(b,a%b):
N,X=map(int,input().split())
print(3*(N-gcd(N,X))

# Fixed version:
def gcd(a,b):
  if a%b==0:
    return b
  else:
    return gcd(b,a%b)
N,X=map(int,input().split())
print(3*(N-gcd(N,X)))

# Buggy version:
import sys
sys.setrecursionlimit(1500)

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

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


# Fixed version:
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)