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: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]

def calculate(x, y):
	if(y % x == 0):
		return x * (2 * (y / x) - 1) 
	else:
		return x * 2 * (y // x) + calculate(y % x, x) 
(N, X) = read()
length = N + calculate(X, N - X)
print(int(length))

Submission status: Accepted

Solution code:
calculate_remainder=lambda pl,pr:2*(pl//pr)*pr-pr if pl%pr==0 else 2*(pl//pr)*pr+calculate_remainder(pr,pl%pr)
n,k=(int(s) for s in input().strip().split(' '))
print(str(n+calculate_remainder(n-k,k)))

Submission status: Accepted

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
N =NX[0]
X =NX[1]
D = NX[0]-NX[1]
A=X+D
def ans(x,y):
	global A
	if x%y==0:
		A+=2*y*(x/y)-y
		return print(int(A))
	A +=int(x/y)*y*2

	ans(y,x%y)

if X>D:
	ans(X,D)
else:	ans(D,X)

Submission status: Accepted

Solution code:
from sys import stdin
n,k = map(int,stdin.readline().split())
def gcd(a,b):
 while a%b:
  t = a%b; a=b; b=t
 return b

print (n-gcd(n,k))*3

Submission status: Accepted

Solution code:
N, x = [int(s) for s in input().split()]

l = N

n = max(x,N-x)
x = min(x,N-x)

while (True):
    q = n // x
    r = n % x
    if r == 0:
        print(l + (2*q-1)*x)
        break
    
    l += (2*q)*x
    n = x
    x = r

Submission status: Accepted

Solution code:
N, X = map(int, input().split())
def f(a, b):
	return 2*a*(b//a) + f(b%a, a) if a else -b
print(N + f(N-X, X))

Submission status: Accepted

Solution code:
n, x = map(int, raw_input().split())
ans = x
while 0 < x < n:
    k = x / (n-x)
    rest = x % (n-x)
    ans += 2*k*(n-x)
    if rest:
        ans += n-x + rest
    n, x = n-x, n-x-rest
print ans

Submission status: Accepted

Solution code:
a,b=map(int,raw_input().split())
t,n,m=a-b,b,a
ans=b
while 1:
    if n<m:
        ans+=t
        t,n,m=n,t,t
    elif n%t==0:
        ans+=n*2-t
        break
    else:
        s=n+m
        n,m=n%t,m%t+t
        ans+=s-n-m
print ans

Submission status: Accepted

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

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

print N + f(X, N - X)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a > b:
        tmp = a
        a = b
        b = tmp

    if a == 0:
        return b

    else:
        return gcd(a, b%a)

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

ans = 3 * (n - gcd(n, x))

print(ans)

Submission status: Accepted

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

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

xx = x
yy = n - x

ans = n
i = 0

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

    if yy == 0:
        ans -= xx
        break

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
        # print l, x, part
        return part
    part = int(l / x) * 2 * x
    # print l, x, part
    return part + get_length(x, l % x)

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

print length

Submission status: Accepted

Solution code:
#! /usr/bin/env python3

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))

Submission status: Accepted

Solution code:
n = map(int, raw_input().split())
x = n[1]
n = n[0]
tpl = x
# print '55',tps, tpl
if x < float(n)/2:
    tps = abs(n - 2*x)
    l = x + n - tps
else:
    l = 0
    tps = n - x
while tps != 0:
    # print l
    # print "t", tps, tpl
    l += tpl/tps * 3*tps  # tpl/tps * 3*tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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_)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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)


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))

Submission status: Accepted

Solution code:
def rec(a, b):
    mx = max(a, b)
    mn = min(a, b)
    #print(mn, mx//mn)
    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(N, X)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
s = 0
for i in range(2*n-1):
  s += min(a[i], a[i+1])
print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def milor(a,b):
    if a == b:
        return a
    if a > b:
        a,b = b,a
    return milor(b-a,a) + 2*a

def solve(n,x):
    res = n + milor(x,n-x)
    return res



if __name__ == '__main__':
    n,x = map(int,input().split())
    print(solve(n,x))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
k=(x/(n-2*x))*(n-2*x)
ans=2*x+n+k 
print(int(ans+1))

Submission status: Runtime Error

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from math import gcd
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

n,x = myinput()
ans = (n-1)*3
print(ans)

Submission status: Runtime Error

Solution code:
def move(mode, N, X, total):
  tmp = total
  if mode == 0:
    tmp += (N-X)
    return move(1, N-X, X, tmp)
  if mode == 1:
    tmp += X
    next = 0
    if N-X == 0:
      return tmp
    return move(next, N, N-X, tmp)

N, X = map(int, input().split(" "))
print(move(0, N, X, 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:
from math import gcd
n, x = map(int, input().split())
print(3*(n-gcd(n, x)))

Submission status: Runtime Error

Solution code:
N = int(input())
X = int(input())
Total_Length = 2*X + N + 3*(X//2)
print(Total_Length)

Submission status: Runtime Error

Solution code:
def run():
  N, X = map(int, input().split())
  y = N-X
  ans = y+X
  while True:
    a = y//X
    ans += a*X*2
    if y%X == 0:
      break
    y, X = max(X, a), min(X, a)
  print(ans-X)
  
  
if __name__ == '__main__':
  run()

Submission status: Runtime Error

Solution code:
def run():
  N, X = map(int, input().split())
  y = N-X
  ans = y+X
  while True:
    a = y//X
    ans += a*X*2
    if y%X == 0:
      break
    y, X = X, a
  print(ans-X)
  
  
if __name__ == '__main__':
  run()

Submission status: Runtime Error

Solution code:
import sys
import math

sys.setrecursionlimit(1000)

def f(x, y):

  if y < x:
    (y, x) = (x, y)

  if x == 0:
    return y

  return x + y + f(x, y - x)

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

print(x + f(x, n - x) - math.gcd(n, x))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(1000)

def f(x, y):

  if y < x:
    (y, x) = (x, y)

  if x == 0:
    return y

  return x + y + f(x, y - x)

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

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

Submission status: Runtime Error

Solution code:
def f(x, y):

  if y < x:
    (y, x) = (x, y)

  if x == 0:
    return y

  return x + y + f(x, y - x)

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

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

Submission status: Runtime Error

Solution code:
def f(x, y):
  
  if y < x:
	y, x = x, y
    
  if y == 0:
    return x
    
  return x + y + f(x, y - x)

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

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

Submission status: Runtime Error

Solution code:
import math

n, x = map(int, input().split())
print(n * x // math.gcd(n, x) + x)

Submission status: Runtime Error

Solution code:
import math

n, x = map(int, input().split())
print(n * x / math.gcd(n, x) + x)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, X = MAP()

def rec(a, b):
    if a == b:
        return a
    a, b = min(a, b), max(a, b)
    return a*2 + rec(a, b-a)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, X = MAP()

if N%2 == 1:
    print((N-1)*3)
else:
    raise Exception
    print(N*3//2+abs(X-N//2)*3)

Submission status: Runtime Error

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


if (n%2 == 1):
    print((n-1)*3)
elif (n==2):
    print(3)
else:
    k=n/2
    if (x<k):
        if((n/2)%2==0):
            l = []
            for i in range(1,k-2):
                l.append(((n-1)*3)-3*(x-1))
            last = l[0]
            l.append(last)
            print(l[x-1])
        else:
            if (x%2==0):
                print((n-2)*3)
            else:
                print((n-1)*3)
    elif(x==k):
        print(int(k*3))
    else:
        p = n-x
        if (p<k):
            if((n/2)%2==0):
                l = []
                for i in range(1,k-2):
                    l.append(((n-1)*3)-3*(p-1))
                last = l[0]
                l.append(last)
                print(l[p-1])
            else:
                if (p%2==0):
                    print((n-2)*3)
                else:
                    print((n-1)*3)

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 a
    if a >= b:
        a, b = b, a
    if b%a != 0:
        return 2 * a * (b//a) + f(a, b%a)
    return 2 * a + f(a, b - a)

print (N + f(N - X, 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 a
    if a >= b:
        a, b = b, a
    return 2 * a + f(a, b - a)

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
#座標平面に落とし込んで再帰関数で無理やり計算
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5)
N, X = map(int, input().split())

if 2 * X > N: #対称性
    X = N - X

#座標の作成
dp = [[-1] * (N+3)]
for i in range(N+1):
    tmp = [-1]
    for j in range(N+1):
        if i <= j:
            tmp.append(0) #到達していないマス
        else:
            tmp.append(-1) #壁(範囲外)
    tmp.append(-1)
    tmp.reverse()
    dp.append(tmp)
dp.append([-1] * (N+3))
# print (np.array(dp))


#m: 0:右へ, 1:左下へ, 2:左上へ
def moving(x, y, total, m):
    if x == X and y == 1 and dp[x][y] == 1: #一周回ってもとの座標に戻ってきた時
        # print ('D')
        return total
    if m == 0: #右に動く時
        tmp = 0
        while dp[x-1][y+1] == 0: #右に進める時
            dp[x-1][y+1] = 1 #右進んだマスを到達したことにする
            tmp += 1
            x -= 1
            y += 1
        if dp[x-1][y+1] == -1: #右側が壁の時-->左下に進む
            # print ('1-B')
            return moving(x, y, total + tmp, 1)
        if dp[x-1][y+1] == 1: #右側が光の線の時-->左上へ進む
            tmp += 1
            # print ('1-C')
            return moving(x-1, y+1, total + tmp, 2)
    
    if m == 1: #左下へ動く時
        tmp = 0
        while dp[x+1][y] == 0: #左下に進める時
            dp[x+1][y] = 1
            tmp += 1
            x += 1
        if dp[x+1][y] == -1: #左下が壁の時-->左上に進む
            # print ('2-C')
            return moving(x, y, total + tmp, 2)
        if dp[x+1][y] == 1: #左下が光の線の時-->左上に進む
            tmp += 1
            # print ('2-C')
            return moving(x+1, y, total + tmp, 2)
    
    if m == 2: #左上に動く時
        tmp = 0
        while dp[x][y-1] == 0: #左上に進める時
            dp[x][y-1] = 1
            tmp += 1
            y -= 1
        if dp[x][y-1] == -1: #左上が壁の時-->右に進む
            # print ('3-A')
            return moving(x, y, total + tmp, 0)
        if dp[x][y-1] == 1: #左上が光の線の時-->左下に進む
            tmp += 1
            # print ('3-B')
            return moving(x, y-1, total + tmp, 1)

X += 1
print (moving(X, 1, 0, 0))
# print (np.array(dp))

Submission status: Runtime Error

Solution code:
# import numpy as np
# import sys
# sys.setrecursionlimit(100)
N, X = map(int, input().split())
if N > 1000:
    exit()

if 2 * X > N:
    X = N - X

#座標の作成
dp = [[-1] * (N+3)]
for i in range(N+1):
    tmp = [-1]
    for j in range(N+1):
        if i <= j:
            tmp.append(0) #到達していないマス
        else:
            tmp.append(-1) #壁(範囲外)
    tmp.append(-1)
    tmp.reverse()
    dp.append(tmp)
dp.append([-1] * (N+3))
# print (np.array(dp))


#m: 0:右へ, 1:左下へ, 2:左上へ
def moving(x, y, total, m):
    if x == X and y == 1 and dp[x][y] == 1:
        # print ('D')
        return total
    if m == 0: #右に動く時
        tmp = 0
        while dp[x-1][y+1] == 0: #右に進める時
            dp[x-1][y+1] = 1 #右進んだマスを到達したことにする
            tmp += 1
            x -= 1
            y += 1
        if dp[x-1][y+1] == -1: #右側が壁の時-->左下に進む
            # print ('1-B')
            return moving(x, y, total + tmp, 1)
        if dp[x-1][y+1] == 1: #右側が光の線の時-->左上へ進む
            tmp += 1
            # print ('1-C')
            return moving(x-1, y+1, total + tmp, 2)
    
    if m == 1: #左下へ動く時
        tmp = 0
        while dp[x+1][y] == 0: #左下に進める時
            dp[x+1][y] = 1
            tmp += 1
            x += 1
        if dp[x+1][y] == -1: #左下が壁の時-->左上に進む
            # print ('2-C')
            return moving(x, y, total + tmp, 2)
        if dp[x+1][y] == 1: #左下が光の線の時-->左上に進む
            tmp += 1
            # print ('2-C')
            return moving(x+1, y, total + tmp, 2)
    
    if m == 2: #左上に動く時
        tmp = 0
        while dp[x][y-1] == 0: #左上に進める時
            dp[x][y-1] = 1
            tmp += 1
            y -= 1
        if dp[x][y-1] == -1: #左上が壁の時-->右に進む
            # print ('3-A')
            return moving(x, y, total + tmp, 0)
        if dp[x][y-1] == 1: #左上が光の線の時-->左下に進む
            tmp += 1
            # print ('3-B')
            return moving(x, y-1, total + tmp, 1)

X += 1
print (moving(X, 1, 0, 0))
# print (np.array(dp))

Submission status: Runtime Error

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


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

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

Submission status: Runtime Error

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


def para(a,b):
    if  b == 0:
        return a
    if a >= b:
        return a+b + para(b,a-b)
    if a < b:
        return 2*a + para(a,b-a)

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

Submission status: Runtime Error

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


def para(a,b):
    if a > b:
        return a+b + para(b,a-b)
    if a < b:
        return 2*a + para(a,b-a)
    if a == b:
        return 3*a
print(X+para(X,N-X))

Submission status: Runtime Error

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

def para(a,b):
    if a > b:
        return a+b + para(b,a-b)
    if a < b:
        return 2*a + para(a,b-a)
    if a == b:
        return 3*a
print(X+para(X,N-X))

Submission status: Runtime Error

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

def para(a,b):
    if a > b:
        return 2*b + para(b,a-b)
    if a < b:
        return 2*a + para(b-a,a)
    if a == b:
        return a
print(N+para(X,N-X))

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
ans=0
if x<n/2:
    a=x
    b=n-x
    ans+=x+n
    while True:
        if b%a==0:
            ans+=a*2*(b/a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a
elif x=n/2:
    ans=3*x
elif x>n/2:
    a=n-x
    b=x
    ans+=x
    while True:
        if b%a==0:
            ans+=a*2*(b/a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a

print(ans)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
ans=0
if x<n/2:
    a=x
    b=n-x
    ans+=x+n
    while True:
        if b%a=0:
            ans+=a*2*(b/a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a
elif x=n/2:
    ans=3*x
elif x>n/2:
    a=n-x
    b=x
    ans+=x
    while True:
        if b%a=0:
            ans+=a*2*(b/a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a

print(ans)

Submission status: Runtime Error

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

if N/2 == X:
    print(X*3)
elif N/2 > X:
    # 上側
    l = X + N - X
    l += ((N-X)//X)*2
    n = (N-X)%X
    l += (X//n)*2*n
    l += n
else:
    l = X
    n = X-N
    l += (N//n)*2*n
print(l)

Submission status: Runtime Error

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

if N/2 == X:
    print(X*3)
else:
    # 上側
    l = X + N - X
    while X > 0:
        l += X*2
        X -= 1
    l += 1
print(l)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
print(n//2*3if n//2=m else n*3-3)

Submission status: Runtime Error

Solution code:
n, x = [int(i) for i in input().split()]

cnt=0

short = n%x
long  = x

cnt += x*(2*(n//x)-1) + (n-x)

while(1):
    c = long % short
    a = long //short
    if c==0:
        break
    else:
        cnt += short * 2 * a
        long = short
        short = c

cnt += short * 2 * a -1
print(cnt)

Submission status: Runtime Error

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


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

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

Submission status: Runtime Error

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


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

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

Submission status: Runtime Error

Solution code:
def f(a,b):
	return 2*a + f_fast(a,b-a)



def f_fast(a,b):
	if a==b:
		return a+b
	elif a < b:
		if b%a == 0:
			return f(a,b)
		else:
			d = b//a
			return 2*a*d + f_fast(a,b%a)
	else:
		return f_fast(b,a)


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

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

Submission status: Runtime Error

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


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

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

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(b,a-b)

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