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 f(a,b):
    if a>b:
        return f(b,a)
    if b%a==0:
        return a*(2*(b//a) + 1)
    return 2*(b//a) + f(b%a,a)
N,X=map(int,input().split())
print(N+f(X,N-X))

Submission status: Wrong Answer

Solution code:
n, x = map(int, input().split())
i = n - x
j = 1
ret = x

while (i > 0):
    ret += i*j
    i -= 1
    j += 1

print(ret)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
"""
n = I()
l = LI()
l.sort()
ans = 0
for i in range(n):
    ans += l[2*i]
print(ans)
"""

#B
n,x = LI()
print((n-1)*3)
#C

#D

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

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

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

k = gcd(n,x)
if k == 1:
    print((n-1)*3)
else:
    print((int(n/k)-1)*3)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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+b1*(q+1)
         return c2
    else:
        count=count+1
        c2=c1+b1*(q+1)
        a2=a1-b1*q
        b2=b1
        if(a2<b2):
           a2,b2=b2,a2
        return calc_path(a2,b2,c2)
if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l = N - X
s = X
cost = N

while(l % s != 0):
        cost += 2 * s
        l, s = s, l - s

print(cost + l + s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#x, N-x, x
#N-x-x
N,x = list(map(int, input().split()))
ll = 0
if x*2 < N:
    ll = x + N
    if (N-x*2) == x:
        ll += 2*x
    elif (N-2*x) > x:
        ll += (N-x*2)/x * 2*x
    else :
        ll += x + x/(N-x*2)*2-(N-x*2)
elif x*2 == N:
    ll = x + x + x
else:
    ll = x
    ll += x/(N-x)*2*(N-x)
print(int(ll))

Submission status: Wrong Answer

Solution code:
#x, N-x, x
#N-x-x
N,x = list(map(int, input().split()))
ll = 0
if x*2 < N:
    ll = x + N
    if (N-x*2) == x:
        ll += 2*x
    elif (N-2*x) > x:
        ll += (N-x*2)/x * 2*x
    else :
        ll += x + x/(N-x*2)*2-(N-x*2)
elif x*2 == N:
    ll = x + x + x
else:
    ll = x
    ll += x/(N-x)*2
print(int(ll))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)
from math import floor
n,x=map(int,input().split())
ans=0
def f(x,y):
    if y%x==0:
        return y//x*2-x
    return 2*x*(y//x)+f(y%x,x)
print(n+f(min(x,n-x),max(x,n-x)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
N,X=map(int,input().split())
def f(a,b):
    if a>b:
        return f(b,a)
    if a==b:
        return a
    if a==0:
        return 0
    if a==1:
        return b
    return 2*a*(b//a)+f(b%a,b)
print(N+f(X,N-X))
#print(f(3,2),f(3,1))

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
if b < (a-b):
    print(4*a-4*b)
else:
    print(4*a-3*b)

Submission status: Wrong Answer

Solution code:
n,x = (int(i) for i in input().split())
if 2*x ==n :ans = 3*x
else:ans = 4*x +4*abs((n-2*x))
print(ans)

Submission status: Wrong Answer

Solution code:
a = input().rstrip().split(" ")
N = int(a[0])
X = int(a[1])

l = X * 4 + (N - 2 * X) * 4

print(l)

Submission status: Wrong Answer

Solution code:
li = [int(i) for i in input().split(" ")]
res = (li[0]-1) * 3
print(res)

Submission status: Wrong Answer

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

ans = 2*N + X

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)
def inpl(): return list(map(int, input().split()))
N, X = map(int, input().split())

def calc(A, B, i):
    d, m = divmod(A, B)
    if m == 0:
        tmp = (2*d-(i != 0))*B
        return tmp
    else:
        tmp = (2*d+1-(i!=0))*B
        tmp += calc(B, m, (i+1)%3)
        return tmp
ans = calc(X, N-X, 0) + X
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def inpl(): return list(map(int, input().split()))
N, X = map(int, input().split())

def calc(A, B, i):
    d, m = divmod(A, B)
    if m == 0:
        tmp = (2*d-(i%3 > 0))*B
        return tmp
    else:
        tmp = (2*d+1-i)*B
        tmp += calc(B, m, (i+1)%3)
        return tmp
ans = calc(X, N-X, 0) + X
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X==N/2:
    print(3*X)
else :
    if X>N/2:
        X = N-X
    L = X + (N-X) + X 
    rest_ab = N-2*X
    while rest_ab > X:
        L += 2*X
        # print('>X',L)
        rest_ab -= X
    if rest_ab == X:
        L += 2*X
    elif rest_ab < X:
        # print('rest_ab=', rest_ab)
        L += X + 2*(X-1) + 1
    print(L)

Submission status: Wrong Answer

Solution code:
value = input()
values = value.split()
     
N = int( values[0] )
X = int( values[1] )
     
shorter = min([X,N-X])
longer = max([X,N-X])
     
length = 0

while shorter>0 and longer >0 :
 
    length = int(longer/shorter)*3*shorter
     
    sho = longer/shorter
    amari = longer%shorter

    longer = shorter
    shorter = amari
    
     
print(length)

Submission status: Wrong Answer

Solution code:
import sys

value = input()
values = value.split()

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

#print(sys.argv)


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

while rect_temp[0] > 0 and  rect_temp[1] > 0 :

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



print(length)

Submission status: Wrong Answer

Solution code:
value = input()
values = value.split()

N = int( values[0] )
X = int( values[1] )

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

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

Submission status: Wrong Answer

Solution code:
# 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)
	L += x*k
#	print(x, y, k, L)
	y, x = (x, y-x)

print(L)

Submission status: Wrong Answer

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

print(N, X)

x0 = N
x1 = X
x2 = x0-x1
L = 2*X
c = 1
while x2 > 0:
	k = floor(x0/(x1/2))-1
	L += x1*k
	x0, x1, x2 = (x1, x2, x0-x1)

print(L)

Submission status: Wrong Answer

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

ans = X + (N-X) + X*2 + (N-2*X)*3
print(ans)
#print(X,N-X,X,N-2*X,N-2*X,N-2*X)

Submission status: Wrong Answer

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 2*(b//a)*a+f(a,b%a) if b%a else 2*(b//a)*a-1

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

Submission status: Wrong Answer

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

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

Solution code:
import math

def func(a,b):
    if a<=b :
        if b%a > 0:
            return 2*math.floor(b/a)*a+func(a,b%a)
        else:
            return 2*math.floor(b/a)*a-1
    else:
        return func(b,a)
N,X = map(int,input().split())

dis =N 
dis += func(N-X,X)
print(dis)

Submission status: Wrong Answer

Solution code:
#最大公約数
def gcd(a,b):
    if a==b:
        return b
    elif b==1:
        return 1
    else:
        return gcd(b,a%b)

def func(a,b):
    if a<=b :
        if b%a > 0:
            return 2*(b//a)*a+func(a,b%a)
        else:
            return 2*(b//a)*a-1
    else:
        return func(b,a)
N,X = map(int,input().split())

dis =N 
dis += func(N-X,X)
print(dis)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
print(n*24/10)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
print(int(n + (7*x) / 2))

Submission status: Wrong Answer

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

if X ==N/2:
    ans = 3*N/2

elif X> N/2:
    ans =  3*X

else:
    ans = 3*X + X + 2*X

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    return 2 * (b//a) * a + light_length(a, b%a)

n,x = li()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N, X = map(int, input().split())
    ans = (3 * X)
    if X % 2 == 0 and N % 2 == 0:
        ans += (N - (X + 2)) * 3
    else:
        ans += (N - (X + 1)) * 3

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().strip().split())
y=n-x
an=y+x*3+(n-2*x)*3
print(an)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().strip().split())
y=n-x
an=y+x*3+x/2*3
print(an)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    n, x = map(int, input().split(' '))
    print((n-x)*3)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n % x == 0:
  nn = int(n / x)
  print((nn - 1)* 3 *x)
else:
  print((n-1)*3)

Submission status: Wrong Answer

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

if n / x == 2:
  print(x*3)
else:
  print((n-1)*3)

Submission status: Wrong Answer

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

if int(n / x) == (n / x):
  print(x*3)
else:
  print(n+x+n)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
if n % x == 0 or n % (n-x) == 0:
  print(max(x,n-x) * 3)
else:
  print(min(x,n-x) * 6)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
print(3 * max(n-x,x) + 3 * (n % x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
L = input()
L = L.split()
n = int(L[0])
x = int(L[1])


if (n % 2 == 1):
    print(3*(n-1))
else:
    if (x < n/2):
        print(int(3*(n-1)-(n/2)*(x-1)))
    elif(x == n/2):
        print(int(3*x))
    else:        
        print(int(3*(n-1)-(n/2)*(n-x-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, 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)

Submission status: Wrong Answer

Solution code:
n, x = map(int, input().split())
#divs = [x]
ans = n
while n % x != 0:
	ans += 2 * (n // x) * x
	n , x = x, n % x
ans -= x
print(ans)

Submission status: Wrong Answer

Solution code:
n, x = map(int, input().split())
#divs = [x]
ans = n
while n % x != 0:
	ans += 2 * ((n - 1) // x) * x
	n , x = x, n % x
ans -= x
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main19():
    strbuf = input("");
    strbufs = strbuf.split();
    buf = [];
    for i in range(2):
        buf.append(int(strbufs[i]));
    if buf[0] <= buf[1] * 2:
        print((buf[1]*3));
    elif buf[0] >= buf[1] * 3:
        print(((buf[0]-buf[1])*3));
    else:
        print(buf[1]*6);





if __name__ == '__main__':
    main19()

Submission status: Wrong Answer

Solution code:
def main19():
    strbuf = input("");
    strbufs = strbuf.split();
    buf = [];
    for i in range(2):
        buf.append(int(strbufs[i]));
    result = buf[1] * 6;
    print(result);



if __name__ == '__main__':
    main19()

Submission status: Wrong Answer

Solution code:
n, x = input().split()
n = int(n)
x = int(x)
d1 = x
d2 = n - x
d3 = min(d1, d2)
d4 = max(d1, d2)
ans = n
while True:
    if d4 - d3 >= 1:
        ans += 2 * d3
    else:
        ans += d3
        break
    temp = d4 - d3
    d4 = d3
    d3 = temp
print(ans)

Submission status: Wrong Answer

Solution code:
n, x = input().split()
n = int(n)
x = int(x)
d1 = x
d2 = n - x
d3 = min(d1, d2)
d4 = max(d1, d2)
ans = n
while True:
    ans += 2 * (d4 // d3) * d3
    if d4 % d3 == 0:
        break
    d3 = d4 - d3
    d4 = d3
print(ans + 1)

Submission status: Wrong Answer

Solution code:
n,a=[int(i) for i in input().split()]
x=min(a,n-a)
print((n-x)*3+(n-x*2)*3)

Submission status: Wrong Answer

Solution code:
x,n=[int(i) for i in input().split()]
print((n-x)*3+(n-x*2)*3)

Submission status: Wrong Answer

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

print(3*(n-x+1))

Submission status: Wrong Answer

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

print(2*x)

Submission status: Wrong Answer

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

L = [N, N - X, X, N - X]
i = 0
S = 0
while True:
    ip = (i + 3) % 4
    S += L[ip]
    L[i] -= L[ip]
    if L[i] == 0:
        break
    i = (i + 1) % 4

print(sum(L) - 1 + S)

Submission status: Wrong Answer

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

A = sorted([X, N - X, N, N - X], reverse=True)

while True:
        if A[0] == A[1]:
                A[0] -= A[2]
        else:
                A[0] -= A[1]
        if A[0] == 0:
                break
        A.sort(reverse = True)

print(3*N - sum(A))

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
if X <= N / 3:
    result = 3 * (N - X)
if N / 3 < X and X < N /2:
    result = 6 * X
if N / 2 <= X:
    result = 3 * X
print(result)

Submission status: Wrong Answer

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

import sys
import os

s = 0

pair = map(int, sys.stdin.readline().strip().split(" "))

for i in range(pair[0]-2,pair[0]+1):
    s += i

print s

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def f(a, b):
    if a * b == 0:
        #print(a, b, 0)
        return 0
    if a == b:
        #print(a, b, a)
        return a
    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
    #print(a, b, res)
    return res

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

Submission status: Wrong Answer

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

def f(a, b):
    if not a * b:
        return 0
    if not (a - b):
        return a
    return f(min(a, b), max(a, b) % min(a, b)) + 2 * min(a, b) * (max(a,b)//min(a, b))

res = 2 * x + f(n - x, x)

print(res)

Submission status: Wrong Answer

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

def f(a, b):
    if not a * b:
        return 0
    return f(min(a, b), max(a, b) % min(a, b)) + 2 * min(a, b) * (max(a,b)//min(a, b))

res = 2 * x + f(n - x, x)

print(res)

Submission status: Wrong Answer

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

N, X = map(int, raw_input().split())
res = X + (N - X) + X + X + (N - X - X) * 3

print res

Submission status: Wrong Answer

Solution code:
def solve(a,b):
	a,b = max(a,b),min(a,b)
	if b == 0:
		return 0
	print a,b
	res = 2*a
	r = solve(b-a%b, a%b)
	if r == 0:
		res -= b
	res += r
	return res
	
n,x = map(int, raw_input().split())

ans = n + solve(x, n-x)
print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import fabs
N,  X = map(int, input().split())
ans = X
N -= X
while N and X:
	ans += N + X
	N, X = (int(fabs(N-X)), min(N, X))
print(ans)

Submission status: Wrong Answer

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

def cal(n):
	if n >= 2:
		return n + 1 + (n - 2)*2
	else:
		return 0

if n % x == 0:
	l = cal(n) - cal(x)
else:
	l = cal(n)

print l

Submission status: Wrong Answer

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

def cal(n):
	return n + 1 + (n - 2)*2

if n % x == 0:
	l = cal(n) - cal(x)
else:
	l = cal(n)

print l

Submission status: Wrong Answer

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

l = n + 1 + (n - 2)*2

print l

Submission status: Wrong Answer

Solution code:
a = input()
a = a.split()
a = [int(x) for x in a]

n = a[0]
x = a[1]

def light(n,x):
	if n == x:
		return 0
	return n + light((n-1),x)

print(str(light(n,x)))

Submission status: Wrong Answer

Solution code:
l = raw_input()
n, x = map(int, l.split())
print(2*n+x)

Submission status: Wrong Answer

Solution code:
l = raw_input()
n, x = map(int, l.split())
print (n-x)*4

Submission status: Wrong Answer

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]):
    if (s[1] == s[2]):
        s[3] = s[0]
        ans += s[2]
        s.sort()
    else:
        s[2] -= s[0]
        ans += s[0]
        s.sort()
ans += s[0]
print ans

Submission status: Wrong Answer

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
	A +=int(x/y)*y+y
	if x%y==0:
		return print(A)
	ans(y,x%y)

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

Submission status: Wrong Answer

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
D = NX[0]-NX[1]
if NX[0] ==0 or NX[1]==0:
	print(str(0))
if NX[1]<D:
	Mod = D%NX[1]
	if Mod!=0:
		print(int(2*NX[1]+NX[1]+(D-NX[1]/2)*3))
	else:
		print(int(NX[1]*4+D))
elif NX[1]==D:
	print(int(3*NX[1]))
elif NX[1]>D:	
	Mod = NX[1]%D
	if Mod!=0:
		print(int(NX[1]+(Mod*3)+D+(D*2*int(NX[1]/D))))
	else:
		print(int(NX[1]+int((NX[1]/D))*2*D))

Submission status: Wrong Answer

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
D = NX[0]-NX[1]

if NX[1]<D:
	Mod = D%NX[1]
	if Mod!=0:
		print(int(2*NX[1]+NX[1]+(D-NX[1]/2)*3))
	else:
		print(int(NX[1]*4+D))
elif NX[1]==D:
	print(int(3*NX[1]))
elif NX[1]>D:	
	Mod = NX[1]%D
	if Mod!=0:
		print(int(NX[1]+(Mod*3)+D+(D*2*int(NX[1]/D))))
	else:
		print(int(NX[1]+int((NX[1]/D))*2*D))

Submission status: Wrong Answer

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
D = NX[0]-NX[1]
if NX[1]<D:
	print(int(2*NX[1]+NX[1]+(D-NX[1]/2)*3))
elif NX[1]==D:
	print(int(3*NX[1]))
elif NX[1]>D:	
	Mod = NX[1]%D
	if Mod!=0:
		print(int(NX[1]+(Mod*3)+D+(D*2*int(NX[1]/D))))
	else:
		print(int(NX[1]+int((NX[1]/D))*2*D))

Submission status: Wrong Answer

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
D = NX[0]-NX[1]
if NX[1]<D:
	print(int(2*NX[1]+NX[1]+(D-NX[1]/2)*3))
elif NX[1]==D:
	print(3*NX[1])
elif NX[1]>D:	
	Mod = NX[1]%D
	if Mod!=0:
		print(NX[1]+(Mod*3)+D+(D*2*int(NX[1]/D)))
	else:
		print(NX[1]+int((NX[1]/D))*2*D)

Submission status: Wrong Answer

Solution code:

NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
if NX[1]>NX[0]/2:
	print(NX[1]+(NX[0]-NX[1])*2)
else:	print(int(2*NX[1]+NX[1]+(NX[0]-NX[1]-NX[1]/2)*3))

Submission status: Wrong Answer

Solution code:
NX = input()
NX = "".join(NX).split(" ")
NX = [int(s) for s in NX]
print(int(2*NX[1]+NX[1]+(NX[0]-NX[1]-NX[1]/2)*3))

Submission status: Wrong Answer

Solution code:
from sys import stdin
n,k = map(int,stdin.readline().split())
print (n-1)*3

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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+1)*(n-x) + rest
    n, x = n-x, rest
print ans

Submission status: Wrong Answer

Solution code:
n, x = map(int, raw_input().split())
ans = x
while x>0:
    ans += n
    n, x = n-x, n-2*x
print ans

Submission status: Wrong Answer

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 a==0 or b==0:
        return 0
    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: Wrong Answer

Solution code:

line = raw_input().split(" ")

n = int(line[0])
x = int(line[1])

print 2*n+x

Submission status: Wrong Answer

Solution code:
a,b=map(int,raw_input().split())
t,n,m=a-b,b,a
ans=b
while 1:
    print t,n,m
    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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)

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

print((n-1)*(n-2))

Submission status: Wrong Answer

Solution code:
n,x = map(int,raw_input().split())
ans = None
if n > 2*x :
    if n%x == 0 :
        l = 2*((n/x) - 1) - 1
        ans = n + l*x
    else :
        l = (2*x)/(n%x) - 1
        ans = n + ((n/x) - 1)*2*x + l*(n%x)
else :
    l = (2*x)/(n-x) - 1
    ans = n + l*(n-x)
print '%d' %(ans)

Submission status: Wrong Answer

Solution code:
n,x = map(int,raw_input().split())
if n > 2*x :
    l = (2*x)/(n%x) - 1
    print '%d' %(n + ((n/x) - 1)*2*x + l*(n%x))
else :
    print '%d' %(n + x)

Submission status: Wrong Answer

Solution code:
n,x = map(int,raw_input().split())
value = ((n/x) - 1)*2*x + 3*(n%x)
print '%d' %(value + n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,x = map(int,raw_input().split())
if n > 2*x :
    print '%d' %(n + 2*x + 3*(n-2*x))
else :
    print '%d' %(n + x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1500)

def f(x, y):
    if x == 0:
        return 0
    if x == y:
        return 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: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1500)

def f(x, y):
    if y == 0:
        return 0
    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: Wrong Answer

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

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