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: Wrong Answer

Solution code:
def slab(base, side):
  if side == 0:
    return 0
  if base < side:
    #print(' + side -> base = %d' % (side))
    return side + slab(side, base)
  d = base % side
  #print('base %d, side %d, d %d' % (base, side, d))
  y = 2 * (base - d)
  if d == 0:
    y -= side
  #print(' + %d' % (y))
  if d != 0:
    y += slab(side, d)
  return y

n, x = map(int, raw_input().split())
#print(' + x = %d' % x)
result = x + slab(x, n - x)
print(result)

Submission status: Wrong Answer

Solution code:
import sys;
n,x=map(int,raw_input().split());
if(2*x<n):
        print x+(n-x)+x+x+(n-2*x)*(2*x-1);
    
elif(2*x==n):
        print x+(n-x)+x;

else:
        print x+(n-x)+x+(n-x)+x+(n-x);

Submission status: Wrong Answer

Solution code:
def compute(n, x):
    quo = n // x
    rem = n % x
    if rem is 0:
        if n < 2*x:
            return (n - 1) * 3
        else:
            return (2*quo - 1) * x
    else:
        if n % (n - x) is 0:
            return compute(n, n - x)
        else:
            return (2*quo - 1) * x + (n - x) + compute(x, rem)

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

Submission status: Wrong Answer

Solution code:
def compute(n, x):
    quo = n // x
    rem = n % x
    if rem is 0:
        return (n - 1) * 3
    else:
        if n % (n - x) is 0:
            return compute(n, n - x)
        else:
            return (2*quo - 1) * x + (n - x) + compute(x, rem)

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

Submission status: Wrong Answer

Solution code:
def f(a, b):
    if a == 0 or b == 0:
        return 0
    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: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(int, raw_input().split())
ans = n
temp = x
inc = 2
p = 2*x
while(temp>0):
    ans+=(inc*temp)
    temp = n-p
    inc+=1
    p+=2*x
print ans

Submission status: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(int, raw_input().split())
ans = n
temp = x
inc = 2
while(temp>0):
    ans+=(inc*temp)
    temp = n-inc*x
    inc+=1
print ans

Submission status: Wrong Answer

Solution code:
# encoding: utf-8

n, x = map(int, raw_input().split())
ac_bc = n - x
print ac_bc + x * 3 + 3 * (ac_bc - x)

Submission status: Wrong Answer

Solution code:
# encoding: utf-8

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

ac_bc = n - x
s = ac_bc / 3
l = ac_bc - s
print ac_bc + s * 3 + l * 3

Submission status: Wrong Answer

Solution code:
# encoding: utf-8

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

ac_bc = n - x
s = ac_bc / 3
l = ac_bc - s
print ac_bc + s * 3 + l * 3

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def compute(n, x):
    quo = n // x
    rem = n % x
    if rem is 0:
        return (n - 1) * 3
    else:
        return (2*quo - 1) * x + (n - x) + compute(x, rem)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if N % 2 == 0:
	print 3 * (N / 2 + abs(X - N / 2))
else:
	print 3 * (N - 1)

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if N % 2 == 0:
	3 * (N / 2 + abs(X - N / 2))
else:
	print 3 * (N - 1)

Submission status: Wrong Answer

Solution code:
n,x = map(int,raw_input().split())
if n > 2 * x:
	print 3*x + (n-x) + 3*(n - 2*x) 
elif n == 2 * x:
	print 3 * x
elif n < 2 * x:
	print x + 4 * (n-x)

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if N % 2 == 0:
	if 3 * X - N < 0:
		print 3 * (N - X)
	elif N - 2 * X > 0:
		print 6 * X
	else:
		print 3 * X
else:
	print 3 * (N - 1)

Submission status: Wrong Answer

Solution code:
import sys
n,x = map(int,raw_input().strip().split(" "))
print x*3*(int((n-1)/x))

Submission status: Wrong Answer

Solution code:
def compute(n, x):
    quo = n // x
    rem = n % x
    rst = (2*quo - 1) * x
    if rem is 0:
        return rst
    else:
        return rst + (n - x) + compute(x, rem)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = map(int, raw_input().split())
x = n[1]
n = n[0]
x = min(x, n-x)
tpl = x
tps = n - 2*x
l = x + n - tps
while tps != 0:
    l += tpl * 3  # tpl/tps * 3*tps -tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l

Submission status: Wrong Answer

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

s = n - 1

if n % 2 == 0:
	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: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
ans = 0
if N%2 ==0:
    d = min(X, N-X)
    ans = (N-d)*3
else:
    ans = (N-1)*3

print(ans)

Submission status: Wrong Answer

Solution code:
n = map(int, raw_input().split())
x = n[1]
n = n[0]
x = min(x, n-x)
tpl = x
tps = n - 2*x
l = x + n - tps
while tps != 0:
    print l
    l += tpl * 3  # tpl/tps * 3*tps -tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l

Submission status: Wrong Answer

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

if 2 * X == N:
	print(X*3)
elif 2 * X < N:
	ans = Y + 3 * X
	a = N-2*X
	b = X
	# tmp = min(N-2*X, N-Y)
	if a < b:
		ans += ((X // a) * 2 - 1) * a
	else:
		ans += ((X // b) * 2 - 1) * b
	raise NameError('NG!')
	print(ans)
else:  # 2 * X < N
	ans = X
	ans += ((X // Y) * 2) * Y
	print(ans)

Submission status: Wrong Answer

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

ans = (N-1) * 3
if N % 2 == 0:
    ans -= 3 * min(X-1, N - X - 1)
print(ans)

Submission status: Wrong Answer

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

if 2 * X == N:
	print(X*3)
elif 2 * X < N:
	ans = Y + 3 * X
	a = N-2*X
	b = X
	# tmp = min(N-2*X, N-Y)
	if a < b:
		ans += ((X // a) * 2 - 1) * a
	else:
		ans += ((X // b) * 2 - 1) * b
		raise NameError('NG!')
	print(ans)
else:  # 2 * X < N
	ans = X
	ans += ((X // Y) * 2) * Y
	print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,x = map(int,raw_input().strip().split(" "))

print x*3*(int((n-1)/x))

Submission status: Wrong Answer

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,s,l-s)
print cal(ans,n-x,x)

Submission status: Wrong Answer

Solution code:
N,X = map(int,raw_input().split())
X = min(X,N-X)
if  N % X == 0:
	print X *(N / X - 1) * 3
else:
	print (N - 1) * 3

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
if x>n/2:
    x = n-x
if n%x!=0:
    x = 1
if n%2==0:
    print(int(n/2)*3 + (int(n/2)-x)*3)
else:
    print((n-1)*3)

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if 3 * X - N < 0:
	print 3 * (N - X)
elif N - 2 * X > 0:
	print 6 * X
else:
	print 3 * X

Submission status: Wrong Answer

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

if 2 * X == N:
	print(X*3)
elif 2 * X < N:
	ans = Y + 3 * X
	a = N-2*X
	b = X
	# tmp = min(N-2*X, N-Y)
	if a < b:
		ans += ((X // a) * 2 - 1) * a
	else:
		ans += ((X // b) * 2 - 1) * b
	print(ans)
else:
	ans = X
	ans += ((X // Y) * 2) * Y
	print(ans)

Submission status: Wrong Answer

Solution code:
n = map(int, raw_input().split())
x = n[1]
n = n[0]
x = min(x, n-x)
tpl = x
tps = n - 2*x
l = x + n
while tps != 0:
    l += tpl * 3 - tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().strip().split())
print N + (N-X)*2 -1

Submission status: Wrong Answer

Solution code:
N, x = map(long, raw_input().split())
print (N - x) + 3 * x + 3 * (N - x - x)

Submission status: Wrong Answer

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

if X == 1:
    print(3*(N-1))
elif N%X==0:
    t = N//X
    print(X*(3*(t-1)))
elif N%(N-X)==0:
    t = N//(N-X)
    print((N-X)*(3*(t-1)))
else:
    print(3*(N-1))

Submission status: Wrong Answer

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

if N % 2 != 0:
    print(N // 2 * 6)
else:
    mid = N // 2
    print(mid * 3 + 3 * abs(mid - X))

Submission status: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(int, raw_input().split())
print 2*n+x

Submission status: Wrong Answer

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

N, X = map(int, input().split())
if X*2 == N:
    print(X*3)
else:
    a = max(X, N-X)
    b = min(X, N-X)
    print(b*3*(a//b + (1 if a%b>0 else 0)))

Submission status: Wrong Answer

Solution code:
import sys
n,x = map(int,raw_input().strip().split(" "))

print x*3*(x+int((n-2)/x-1))

Submission status: Wrong Answer

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

path = 0
if X < N/2.0:
    res = N - X
    path += N

    while res > X:
        path += X*2
        res -= X

    if res != 0:
        path += (X*2 - 1)

else:
    res = X
    path += N

    while res > N-X:
        path += (N-X)*2
        res -= (N-X)

    if res != 0:
        path += ((N-X)*2 - 1)

print path

Submission status: Wrong Answer

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

def light(N,X):
	if (X >= N / 2):
		return 3 * X
#	return 3 * (math.ceil((N - 2 * X) / X) + 1) * X
	return 3 * X * ((N - X) // X + 1)

print (light(N,X))

Submission status: Wrong Answer

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

alpha = 0
if X > N/2:
    alpha = X
    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 - alpha

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N, X = map(int, input().split())
if X*2 == N:
    print(X*3)
else:
    a = max(X, N-X)
    b = min(X, N-X)
    print(X*3*(a//b + (1 if a%b>0 else 0)))

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
ans = 0
if N%2 ==0:
    ans = (N-X)*3
else:
    ans = (N-1)*3

print(ans)

Submission status: Wrong Answer

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

def light(N,X):
	if (X >= N / 2):
		return 3 * X
	return 3 * (math.ceil((N - 2 * X) / X) + 1) * X

print (light(N,X))

Submission status: Wrong Answer

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

N, X = map(int, input().split())
if X*2 == N:
    print(X*3)
else:
    print(X*3*((N-X)//X + (1 if (N-X)%X>0 else 0)))

Submission status: Wrong Answer

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

s = 3 * (n - 1)

if n % 2 == 0:
	s = 3 * (n / 2 + abs(n / 2 - x))

print s

Submission status: Wrong Answer

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

def light(N,X):
	if (X >= N / 2):
		return 3 * X
	return 3 * (math.ceil((N - 2 * X) // X) + 1) * X

print (light(N,X))

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
if n%2==0:
    print(int(n/2*3 + abs(n/2-x)*3))
else:
    print((n-1)*3)

Submission status: Wrong Answer

Solution code:
N,X = map(int,raw_input().split())
if N % 2 == 0:
	print (abs(X-N/2)+N/2) * 3
else:
	print (N - 1) * 3

Submission status: Wrong Answer

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

def main():
    n, x = [int(i) for i in input().split()]
    result = n + (x * 2) + (x // 2) * 3
    print(result)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(int, raw_input().split())
print 4*(n-x)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if n%2==0:
    print(n/2*3 + abs(n/2-x)*3)
else:
    print((n-1)*3)

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if 3 * X - N < 0:
	print 3 * (N - X)
elif N - 2 * X > 0:
	print 6 * X
else:
	print 3 * N

Submission status: Wrong Answer

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

if n == 2:
    ans = 3

elif n % 2 == 1:
    ans = (n-1) * 3

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

    std = (n//2) * 3
    ans = std + (n//2 - x) * 3

print(ans)

Submission status: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(float, raw_input().split())
print int(n+2*x+(x/2)*3)

Submission status: Wrong Answer

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

Ans = 4 * N - 4 * X

print(Ans)

Submission status: Wrong Answer

Solution code:
L = input().split()
n=int(L[0])
x=int(L[1])
val = int(x+(n-x)+x+x+3*(x/2))
print(val)

Submission status: Wrong Answer

Solution code:
import math
def inp():
    return int(raw_input())
def linp():
    return map(int, raw_input().split())
n, x = map(float, raw_input().split())
print n+2*x+(x/2)*3

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
if N % X != 0:
    print(N * 3 - 3)
else:
    print((N - min(X, N - X)) * 3)

Submission status: Wrong Answer

Solution code:
N, X = map(int,input().split())
if N == X*2:
    print(X*3)
else:
    print(3*(N-1))

Submission status: Wrong Answer

Solution code:
n, x = map(int,input().split())
ans = (n-1) * 3
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, X = map(int, raw_input().split())
if N - 2 * X > 0:
	print 4 * (N - X)
else:
	print 3 * N

Submission status: Wrong Answer

Solution code:
print 3*(int(raw_input().split()[0])-1)

Submission status: Time Limit Exceeded

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

ans = N
N = N - X
while N:
  if X < N:
    ans += X * 2
    N, X = X, N - X
  else:
    d, m = divmod(N, X)
    ans += d * X + m * 2
    N, X = m, X - m

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

ans = 0
ans += n

l1, l2 = min(n - x, x), max(n - x, x)
#while True:
#    if l1 == l2:
#        ans += l1
#        break
#    else:
#        ans += (l1 * 2)
#        l1, l2 = min(l1, l2 -l1), max(l1, l2 - l1)
while True:
    if l2 % l1 == 0:
        ans += (l1 * ((l2 // l1) * 2 - 1))
        break
    else:
        ans += (l1 * 2)
        l1, l2 = min(l1, l2 -l1), max(l1, l2 - l1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

ans = 0
ans += n

l1, l2 = min(n - x, x), max(n - x, x)
while True:
    if l1 == l2:
        ans += l1
        break
    else:
        ans += (l1 * 2)
        l1, l2 = min(l1, l2 -l1), max(l1, l2 - l1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))
a = K
b = N - K
c = 0
d = min(a, b)
while not a == b == c:
    a, b, c = sorted([a, b, c])
    if b == c and (b - a) % d == 0:
        a = b
    elif a == b:
        a += d
    else:
        d = b - a
        a += d * 2
print(a + b + c)

Submission status: Time Limit Exceeded

Solution code:
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))
a = K
b = N - K
c = 0
d = min(a, b)
while not a == b == c:
    a, b, c = sorted([a, b, c])
    if a != b:
        d = b - a
    a += d
print(a + b + c)

Submission status: Time Limit Exceeded

Solution code:
N, X = map(int, input().split(" "))
ans = N
N -= X
while(True):
  if N%X==0:
    ans += (N*2-X)
    break
  else:
    ans += X*2
    N = N - X
    if N < X:
      tmp = N
      N = X
      X = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, X = map(int, input().split(" "))
ans = X
count = 0
while(True):
  if count == 0:
    ans = ans + N - X
    N -= X
    if N < X:
      tmp = N
      N = X
      X = tmp
    count = 1
  elif count == 1:
    if N%X == 0:
      ans += (N*2-X)
      break
    else:
      ans = ans + X
      X = N-X
      if N < X:
        tmp = N
        N = X
        X = tmp
      count = 0
  if X == 0:
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, X = map(int, input().split(" "))
ans = X
count = 0
while(True):
  if count == 0:
    ans = ans + N - X
    N -= X
    if N < X:
      tmp = N
      N = X
      X = tmp
    count = 1
  elif count == 1:
    ans = ans + X
    X = N-X
    if N < X:
      tmp = N
      N = X
      X = tmp
    count = 0
  if X == 0:
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, x = map(int, input().split())
ans = n
y, x = n-x, x
while y != x:
  if x > y:
    y, x = x, y
  ans += 2*x
  y, x = y-x, x
ans += x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, X = map(int, input().split())
hei = [X, N - X]
distance = N

while hei[0] != hei[1]:
    if hei[0] > hei[1]:
        distance += 2 * hei[1]
        hei[0] = hei[0]- hei[1]
    else:
        distance += 2 * hei[0]
        hei[1] = hei[1] - hei[0]

distance += hei[0]

print(distance)

Submission status: Time Limit Exceeded

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

ans = N
a = N - X
b = X
while a != b:
    if a >= b:
        a, b = b, a
    if b%a != 0:
        ans += 2 * a * (b//a)
        b = b%a
    else:
        ans += 2 * a
        b = b - a
print (ans + a)

Submission status: Time Limit Exceeded

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




ans = N
a = N - X
b = X
while a != b:
    if a >= b:
        a, b = b, a
    ans += 2 * a
    b = b - a
print (ans + a)

Submission status: Time Limit Exceeded

Solution code:
N,X = map(int,input().split())
x = X
y = N-X
res = N
while 1:
    if x==y:
        res += x
        break
    if x > y:
        x,y = y,x
    res += x*2
    y -= x
print(res)

Submission status: Time Limit Exceeded

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

e_long = max(X, N-X)
e_short = N - e_long

total = N

while e_long != e_short and e_long > 0 and e_short > 0:
    total += e_short * 2
    e_long -= e_short
    if e_short > e_long:
        tmp = e_long
        e_long = e_short
        e_short = tmp

total += e_short

print(total)

Submission status: Time Limit Exceeded

Solution code:
n,x= list(map(int,input().split(" ")))
y = n-x
s = n
while x!=y:
  if x>y:
    s +=2* y
    x -=y
  else:
    s += 2*x
    y -=x
s+=x 
print( s)

Submission status: Time Limit Exceeded

Solution code:
input_word = input()
tmp_list = input_word.split(" ")
N = int(tmp_list[0])
Y = int(tmp_list[1]) * 2 - 1
output = [[ 0 for i in range(2*N) ] for j in range(2*N)]
X = 0
flag = 1
output[Y][X] = 1

while True:
  if flag == 1:
    if Y == 2*X + 1:
      if output[Y+1][Y+1] == 0:
        Y, X = Y+1, Y+1
        flag = 2
      else:
        flag = 4
    else:
      if output[Y-1][2*X+1] == 1 and output[Y-1][2*X+2] == 1 and output[Y+1][2*X+2] == 1 and output[Y+1][2*X+3] == 1:
        flag = 4
      elif output[Y-1][2*X+1] == 1 and output[Y+1][2*X+3] == 1: 
        Y, X = Y+1, 2*X+2
        flag = 2
      elif output[Y-1][2*X+2] == 1 and output[Y+1][2*X+2] == 1:
        Y, X = Y-1, 2*X+1
        flag = 3
      else:
        X += 1
  elif flag == 2:
    if Y == 2*N -2:
      if output[Y][X-1] == 0:
        Y,X = Y,X-1
        flag = 3
      else:
          flag = 4
    else:
      if output[Y][X-1] == 1 and output[Y+1][X//2-1] == 1 and output[Y+1][X//2] == 1 and output[Y+2][X+1] == 1:
        flag = 4
      elif output[Y][X-1] == 1 and output[Y+2][X+1] == 1:
        Y,X = Y+1,X//2
        flag = 1
      elif output[Y+1][X//2-1] == 1 and output[Y+1][X//2] == 1:
        Y,X = Y,X-1
        flag = 3
      else:
        Y += 2
  else:
    if X == 1:
      if output[Y-1][0] == 0:
        Y,X = Y-1,0
        flag = 1
      else:
          flag = 4
    else:
      if output[Y-2][X-1] == 1 and output[Y-1][(X-1)//2-1] == 1 and output[Y-1][(X-1)//2] == 1 and output[Y][X-1] == 1:
        flag = 4
      elif output[Y-1][(X-1)//2-1] == 1 and output[Y-1][(X-1)//2] == 1:
        X -= 1
        flag = 2
      elif output[Y-2][X-1] == 1 and output[Y][X-1] == 1:
        Y,X = Y-1,(X-1)//2
        flag = 1
      else:
        Y -= 2
        X -= 2
        
  if flag == 4:
    break
  else:
    output[Y][X] = 1

res = 0

for i in output:
  for j in i:
    res += j
print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = list(map(int, input().split()))
l = s[0]

while True:
    tmp = s[0]
    s[0] = s[1]
    s[1] = tmp - s[1]
    s = np.sort(s)[::-1]
    
    if s[0] == s[1]:
        l += s[0]
        break
    else:
        l = l + s[1] * 2

print(l)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
A=S.split()
N=int(A[0])
X=int(A[1])

Suma=0
Suma=Suma+X
Bx=X
By=N-X
Suma=Suma+By

while (Bx%By!=0):
    Path=int(Bx/By)
    Suma=Suma+By*(2*Path)
    Res=Bx%By
    Suma=Suma+2*Res
    Bx=Res
    By=By-Res
    
Suma=Suma+By*(2*Bx/By-1)
print(int(Suma))

Submission status: Time Limit Exceeded

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

sys.setrecursionlimit(100000)

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

def f(x, n):
    if x == 0 or x == n:
        return 0
    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: Time Limit Exceeded

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

sys.setrecursionlimit(100000)

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: Time Limit Exceeded

Solution code:
def gcd(a, b):
        while a != b:
                if a > b :
                        a -= b
                else:
                        b -= a
        return a

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

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

Submission status: Time Limit Exceeded

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:
        a -= b
        res += 2*b
    elif  b > a:
        b -= a
        res += 2*a
    else:
        res += a
        flag = False
print(res)

Submission status: Time Limit Exceeded

Solution code:
n, x = map(int, raw_input().split())
s = [x, n-x, n-x, n]
ans = x
s.sort()
while not(s[0] == s[3]):
    dup = set([x for x in s if s.count(x) > 1])
    if (len(dup) == 1):
        elem = dup.pop()
        s[3] -= elem
        ans += elem
        s.sort()
    else:
        s[2] -= s[0]
        ans += s[0]
        s.sort()
ans += s[0]
print ans

Submission status: Time Limit Exceeded

Solution code:
from numpy import array
N,X=list(map(int,input().split()))


'''
M[i段目][左からj]
0.右  (i,j+1)
1.左上 (i-1,j-1)
2.右上 (i-1,j+1)
3.左下(i+1,j)
4.右下(i+1,j+1)

'''
M =[[array([0,0]) for _ in range(n+1)] for n in range(N+1)]
#print(M)
#start M[X][0]
pos = array([X,0])
ans = 0
dist = array([0,1])
while not( ans and pos[0]==X and pos[1]==0):
    pos += dist
    ans += 1
    if pos[0] ==pos[1]:
        dist = array([1,0]) #dl
    elif pos[1]==0:
        dist = array([0, 1]) #r
    elif pos[0]==N:
        dist = array([-1, -1]) #ul

    if not M[pos[0]][pos[1]].any():
        M[pos[0]][pos[1]]= dist
    else:
        dist_tmp =M[pos[0]][pos[1]]
        dist = -dist-dist_tmp
        #r + dl -> ul
        #ul + r -> dl
    #print(M)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
(N, X) = tuple([int(term) for term in input().split()])

T = N

A = N - X
B = X
while (True) :
    if (B == 0) :
        break
    n = int(A / B)
    if (A % B == 0) :
        T += (2 * n - 1) * B
        break
    T += 2 * n * B + 2 * (A % B)
    a = A % B
    b = B - (A % B)
    A = a
    B = b

print(T)

Submission status: Time Limit Exceeded

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
f = False

while True:
    if yy >= xx:
        ans += 2 * xx
        yy -= xx

        if yy == 0:
            ans -= xx
            f = True
            break

    if xx > yy:
        ans += yy * 2
        xx -= yy

        if xx == 0:
            ans -= yy
            f = True
            break

print(ans)

Submission status: Time Limit Exceeded

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

ans = n
c = [x, n-x]
while (c[0] != c[1]):
    ans += 2 * min(c)
    c = [min(c), max(c)-min(c)]
print(ans+c[0])

Submission status: Time Limit Exceeded

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
f = False

while True:
    if yy >= xx:
        ans += 2 * xx
        yy -= xx

        if yy == 0:
            ans -= xx
            f = True
            break

        if f:
            break

    if xx > yy:
        ans += yy * 2
        xx -= yy

        if xx == 0:
            ans -= yy
            f = True
            break

        if f:
            break

print(ans)

Submission status: Time Limit Exceeded

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

X,N = sorted((X,N-X))
while N != X:
    A += 2*X
    X,N=sorted((X,N-X))
print(A+X)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = N
N, X = X, N - X
while N and X:
  if X > N:
    d, m = divmod(X, N)
    if m == 0:
      ans += (d * 2 - 1) * N
    else:
      ans += d * 2 * N
    N, X = N, m
  else:
    d, m = divmod(N, X)
    if m == 0:
      ans += (d * 2 - 1) * X
    else:
      ans += d * 2 * X + m * 2
    N, X = m, X - m

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def func(X, Y):
    if X < Y:
        return func(Y, X)
    # X>=Y
    if X % Y == 0:
        return 2*X-Y
    else:
        return 2*Y*(X//Y) + func(Y, X % Y)


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = n
e = n-x
count = 0
while True:
    
    if x%e == 0:    
        ans += x//e*(e*2)-e
        break
    else:
        ans += (x//e)*e*2
    x,e = e,x%e
    count += 1
    
print(ans)

Submission status: Accepted

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

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

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