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.



Sample input:
5 2


Sample output:
12

Submission status: Runtime Error

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

def f(a,b):
     if a == b:
          return a
     elif a < b:
          return 2*a + f(a,b-a)
     else:
          return 2*b + f(a-b,b)

print(n+f(x,n-x))

Submission status: Runtime Error

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

n, x = map(int, input().split())
print(solve(n, n - x, x))

Submission status: Runtime Error

Solution code:
def solve(s, a, b):
    if a == 1:
        return s + b * 2 - 1
    if b == 1:
        return s + a * 2 - 1
    if a == b:
        return s + a
    elif a > b:
        return solve(s + b * 2, a - b, b)
    else:
        return solve(s + a * 2, a, b - a)

n, x = map(int, input().split())
print(solve(n, n - x, x))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000000)

def solve(s, a, b):
    if a == b:
        return s + a
    elif a > b:
        return solve(s + b * 2, a - b, b)
    else:
        return solve(s + a * 2, a, b - a)


n, x = map(int, input().split())
print(solve(0, n - x, x) + n)

Submission status: Runtime Error

Solution code:
def solve(s, a, b):
    if a == b:
        return s + a
    elif a > b:
        return solve(s + b * 2, a - b, b)
    else:
        return solve(s + a * 2, a, b - a)


n, x = map(int, input().split())
print(solve(0, n - x, x) + n)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
l = N//X
q = N%X
print(N + 2*(l-1)*X + q*((X//q) + 1))

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
l = N//X
q = N%X
print(N + 2*(l-1)*X + q*(X//q + 1))

Submission status: Runtime Error

Solution code:
#-*-coding: utf-8-*-
N, X = map(int, input().split())
l = N//X
q = N%X
print(N + 2*(l-1)*X + q*(X//q + 1))

Submission status: Runtime Error

Solution code:
#-*-coding: utf-8-*-
N, X = map(int, input().split())
print(N + 2*((N//X)-1)*X + (N%X) * (X//(X//(N%X)) + 1))

Submission status: Runtime Error

Solution code:
#-*-coding: utf-8-*-
N, X = map(int, input().split())

print(N + 2*((N//X)-1)*X + (N%X) * (X//(X//(N%X)) + 1))

Submission status: Runtime Error

Solution code:
#-*-coding: utf-8-*-
N, X = map(int, input().split())

print(N + 2*((N//X)-1)*X + (N%X) * (X//(X//q) + 1))

Submission status: Runtime Error

Solution code:
import fraction

li = [int(i) for i in input().split(" ")]
res = (li[0] // fraction.gcd(li[0], li[1]) -1) * 3 * fraction.gcd(li[0], li[1])
print(res)

Submission status: Runtime Error

Solution code:
import math

li = [int(i) for i in input().split(" ")]

res = (li[0] // math.gcd(li[0], li[1]) -1) * 3 * math.gcd(li[0], li[1])
print(res)

Submission status: Runtime Error

Solution code:
from fractions
n,x=map(int,input().split())
print(3*(n-fractions.gcd(n,x)))

Submission status: Runtime Error

Solution code:
import math
n,x=map(int,input().split())
print(3*(n-math.gcd(n,x)))

Submission status: Runtime Error

Solution code:
import math
n,x=map(int,input().split())
print(4*(n-math.gcd(n,x)))

Submission status: Runtime Error

Solution code:
import math
n,x=map(int,input().split())
print(4*(n-gcd(n,x)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

def f(a,b) :
    if a < b :
        return 2*a + f(a,b-a)
    elif a > b :
        return 2*b + f(b,a-b)
    else :
        return a

ans = N + f(X,N-X)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**12+1)

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

def f(a,b) :
    if a < b :
        return 2*a + f(a,b-a)
    elif a > b :
        a,b = b,a
        return 2*a + f(a,b-a)
    else :
        return a

ans = N + f(X,N-X)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

def f(a,b) :
    if a < b :
        return 2*a + f(a,b-a)
    elif a > b :
        a,b = b,a
        return 2*a + f(a,b-a)
    else :
        return a

ans = N + f(X,N-X)

print(ans)

Submission status: Runtime Error

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

def f(a,b) :
    if a < b :
        return 2*a + f(a,b-a)
    elif a > b :
        a,b = b,a
        return 2*a + f(a,b-a)
    else :
        return a

ans = N + f(X,N-X)

print(ans)

Submission status: Runtime Error

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

def f(a,b) :
    if a < b :
        return 2*a + f(a,b-a)
    elif b == 0 :
        return 0
    else :
        return ((a//b)*2-1)*b

ans = N + f(X,N-X)

print(ans)

Submission status: Runtime Error

Solution code:
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 rec(a: int, b: int) -> int:
    if a == b:
        return a
    
    else:
        mx = max(a,b)
        mn = min(a,b)
        return 2*mn + rec(mn, mx-mn)
    
n,x = li()
print(n + rec(x, n-x))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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 rec(a: int, b: int) -> int:
    if a == b:
        return a
    
    else:
        mx = max(a,b)
        mn = min(a,b)
        return 2*mn + rec(mn, mx-mn)
    
n,x = li()
print(n + rec(x, n-x))

Submission status: Runtime Error

Solution code:
def tri_len(x,y):
  if x== y:
    return x
  elif x < y:
    return tri_len(y-x,x)+2*x
  else:
    return tri_len(x-y,y)+2*y
import sys
sys.setrecursionlimit(100000)

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

print( n+tri_len(x,n-x))

Submission status: Runtime Error

Solution code:
def tri_len(x,y):
  if x== y:
    return x
  elif x < y:
    return tri_len(y-x,x)+2*x
  else:
    return tri_len(x-y,y)+2*y
import sys
sys.setrecursionlimit(10000)

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

print( n+tri_len(x,n-x))

Submission status: Runtime Error

Solution code:
def tri_len(x,y):
  if x== y:
    return x
  elif x < y:
    return tri_len(y-x,x)+2*x
  else:
    return tri_len(x-y,y)+2*y


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

print( n+tri_len(x,n-x))

Submission status: Runtime Error

Solution code:
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
x = 0
 
for i in range(0, 2*N, 2):
    print(L[i])
    x += L[i]
 
print(x)

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.argv[1])
X = int(sys.argv[2])


rect = [ X, N-X ]
rect_temp = rect
length = 0

while True :

    if rect_temp[0] < rect_temp[1]:
        length += rect_temp[0]*3
        rect_temp = [ rect_temp[0], rect_temp[1]-rect_temp[0]  ]

    elif rect_temp[0] > rect_temp[1]:
        length += rect_temp[1]*3
        rect_temp = [ rect_temp[0] - rect_temp[1], rect_temp[0]  ]

    elif rect_temp[0] == rect_temp[1]:
        break


print(length)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**15)

def f(a,b):
    if a==b:return a
    if not a<b:a,b=b,a
    return f(a,b-a)+2*a

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)

def f(a,b):
    if a==b:return a
    if not a<b:a,b=b,a
    return f(a,b-a)+2*a

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

Submission status: Runtime Error

Solution code:
N, X = map(int, input())

if X == N//2:
    print(N//2 * 3)
elif N % X == 0:
    a = max(N-X, X)
    b = min(N-X, X)
    print(a + a // b * 2)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

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 light_length(a:int, b:int) -> int:
    if a == b:
        return a
    
    if a > b:
        a,b = b,a

    return 2*a + light_length(min(a,b-a), max(a,b-a))

n,x = li()

print(n + light_length(x,n-x))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

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 light_length(a:int, b:int) -> int:
    if a == b:
        return a

    return 2*a + light_length(min(a,b-a), max(a,b-a))

n,x = li()

print(n + light_length(x,n-x))

Submission status: Runtime Error

Solution code:
N, X = [int(i) for i in input().split()]
import math

gcd = math.gcd(N, X)
print(gcd*(N//gcd-1)*3)

Submission status: Runtime Error

Solution code:
import math
n,x=map(int,input().strip().split())
print(3*(n-math.gcd(n,x)))

Submission status: Runtime Error

Solution code:
n,x=map(int,input().strip().split())
z=n-2*x
y=n-x
an=0
an+=3*x+y
an+=z*(x//z+1)
#x=x//z
#z-=2*x
while not z<x:
    an+=z*(x//z+1)
    if not x%z==0:
        an+=1
    z-=2*x
    
print(an)

Submission status: Runtime Error

Solution code:
#Include <bits/stdc++.h>
using namespace std;

int main(){

}

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
s = a/b
print(3b*(s-1))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,x = map(int,input().split())
import fractions
print+(3 * (n-fractions.gcd(n,x)))

Submission status: Runtime Error

Solution code:
import math
[N, X] = list(map(int, input().split()))
print(int(3*(N-math.gcd(N,X))))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, X):
    if X == N//2:
        return 3*X
    ans = 0
    if X > N//2:
        X = N-X

    ans = X

    def f(n, x):
        ans = 0
        ans += n
        n_x = n // x
        ans += 2*n_x*x - x
        if n_x % x != 0:
            ans += f(x, n - n_x*x)

        # print(n, x, n_x, 2*n_x*x - x, ans)
        return ans

    return ans + f(N-X, X)


def main():
    N, X = read_int_n()
    print(slv(N, X))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, X):
    ans = 0
    if X > N//2:
        X = N-X

    ans = X

    def f(n, x):
        ans = 0
        ans += n
        n_x = n // x
        ans += 2*n_x*x - x
        if n_x % x != 0:
            ans += f(x, n - n_x*x)

        # print(n, x, n_x, 2*n_x*x - x, ans)
        return ans

    return ans + f(N-X, X)


def main():
    N, X = read_int_n()
    print(slv(N, X))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

def func(a, b):
    if a < b:
        if b%a == 0:
            return (b//a*2 - 1)*a
        else:
            return 2*a + func(a, b-a)
    elif a > b:
        if a%b == 0:
            return (a//b*2 - 1)*b
        else:
            return 2*b + func(a-b, b)
    else:
        return a

if x == n/2:
    print(3*x)
else:
    print(x+(n-x)+func(x,n-x))

Submission status: Runtime Error

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

def func(a, b):
    if a < b:
        if b%a == 0:
            return b//a*2 - 1
        else:
            return 2*a + func(a, b-a)
    else:
        if a%b == 0:
            return a//b*2 - 1
        else:
            return 2*b + func(a-b, b)

print(x+(n-x)+func(x,n-x))

Submission status: Runtime Error

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

def func(a, b):
    if a < b:
        if b%a == 0:
            return b/a*2 - 1
        else:
            return 2*a + func(a, b-a)
    else:
        if a%b == 0:
            return a/b*2 - 1
        else:
            return 2*b + func(a-b, b)
            
if x == n/2:
    print(3*x)
else:
    print(x+(n-x)+func(x,n-x))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)

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

def f(x, n):
    debug(x,n)
    # if 2*x == n:
    #     return 3*x
    if x % (n-x) == 0:
        return 3*x
    if 2*x < n:
        return f(x, n-x) + 3*x# n + x
    a = x//(n-x) + 1
    t = a*(n-x) - x
    return f(n-x-t, n-x) + 3*x # + t

print(f(X, N))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
"""
            a
+          -------
|    b-a /       /    f(a, b) = 2*a + f(a, b-a)
|       /       /     answer = N + f(X, N-X)
b       -------       COMD(a < b)
|     /  \    /
|    /    \  /
|   /      \/
+   -------

"""
#--- define function ---#
def calc(a, b):
    if a > b:
        a, b = b, a

    if a == b:
        return a
    else:
        return 2 * a + calc(a, b - a)


 #--- main ---#
N, X = input().split()
N, X = int(N), int(X)

ans = N + calc(X, N - X)
print(ans)

Submission status: Runtime Error

Solution code:
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))

Submission status: Runtime Error

Solution code:
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))

Submission status: Runtime Error

Solution code:
from math import gcd

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

print(3*(N - gcd(N, X)))

Submission status: Runtime Error

Solution code:
def calc(a, b):
    if a <= 0 or b <= 0:
        return 0
    if a == b:
        return a
    a, b = min(a, b), max(a, b)
    return calc(b-a, a) + 2 * a

N, X = map(int, raw_input().split())
print calc(X, N-X) + N

Submission status: Runtime Error

Solution code:
'''def f(a, b):
    if a * b == 0:
        return 0
    res = f(min(a, b), max(a, b) % min(a, b)) + 2 * min(a, b) * (max(a,b)//min(a, b))
    if(max(a, b) % min(a, b) == 0):
        res -= 1
    return res'''
def f(a, b):
    if(a - b == 0):
        return a
    if(a * b == 0):
        return 0
    return 2 * min(a, b) + f(min(a, b), max(a,b) - min(a, b))

n, x = map(int, input().split())
res = n + f(n - x, x)
print(res)

Submission status: Runtime Error

Solution code:
'''def f(a, b):
    if a * b == 0:
        return 0
    res = f(min(a, b), max(a, b) % min(a, b)) + 2 * min(a, b) * (max(a,b)//min(a, b))
    if(max(a, b) % min(a, b) == 0):
        res -= 1
    return res'''
def f(a, b):
    minn = min(a, b)
    maxx = max(a, b)
    if(a - b == 0):
        return a
    if(a * b == 0):
        return 0
    return 2 * minn + f(minn, maxx - minn)

n, x = map(int, input().split())
res = n + f(n - x, x)
print(res)

Submission status: Runtime Error

Solution code:
#! /bin/python
# coding:utf-8
 
import sys
 
def gcd(N, X):
	while X:
		N, X = X, N % X
	return N
 
N = int(sys.argv[1])
X = int(sys.argv[2])

print 3 * (N - (gcd(N, X)))

Submission status: Runtime Error

Solution code:
#! /bin/python
# coding:utf-8

def gcd(N, X):
	while X:
		N, X = X, N % X
	return N

N, X = map(int, raw_input().split())
print = 3 * (N - (gcd(N, X)))

Submission status: Runtime Error

Solution code:
L = map(int, raw_input().split())
n = L[0]
x = L[1]
res = n
a = x
b = n-x
flag = True
while flag :
    if a > b:
        q = a // b
        a -= q*b
        res += 2*q*b
    elif  b > a:
        q = b // a
        b -= q*a
        res += 2*a*q
    else:
        res += a
        flag = False
print(res)

Submission status: Runtime Error

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



N = int(raw_input())
k = int(raw_input())
#A = map(int, raw_input().split())
ans = gcd(N,k)
print 3*(N - ans)

Submission status: Runtime Error

Solution code:
N, X = int(input()), int(input())
print(X * 6)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
import sys

sys.setrecursionlimit(1000000)

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

def f(a,b):
    if a > b:
        a,b = b,a

    if b%a is 0:
        return int(a*(2*(b/a)-1))

    return int(2*a*int(b/a) + f(a,b%a))

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
import sys

sys.setrecursionlimit(100000)

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

def f(a,b):
    if a > b:
        a,b = b,a

    if b%a is 0:
        return int(a*(2*(b/a)-1))

    return int(2*a*int(b/a) + f(a,b%a))

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1500)

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

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

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
def f(x, y):
    if x == y:
        return x
    x, y = min(x, y), max(x, y)
    return 2 * x + f(x, y - x)

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

Submission status: Runtime Error

Solution code:
def f(a, b):
    if a == 1 and b == 1:
        return 1
    r = min(a, b)
    q = max(a, b)
    return 2*r + f(r, q-r)

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

print N + f(X, N-X)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import functools

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
    
@functools.lru_cache(maxsize = None)
def func(N, X):
    g = gcd(N, X)
    if g == 1:
        if X > N // 2:
            return func(N, N - X)
        if N == 2:
            return 3
        if N == 3:
            return 6
        return X * 3 + func(N-X, X)
    else:
        return g * func(N//g, X//g)

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import functools

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
    
@functools.lru_cache(maxsize = None)
def func(N, X):
    g = gcd(N, X)
    if g == 1:
        if X > N // 2:
            return func(N, N - X)
        if N == 2:
            return 3
        if N == 3:
            return 6
        return X * 3 + func(N-X, X)
    else:
        return g * func(N//g, X//g)

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

Submission status: Runtime Error

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