Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
min_n = n
rlist = []
while x>0:
    if (k == 1):
        rlist = [0]
        break
    rlist.append(abs(x-k))
    x = abs(x-k)
    min_n = min(rlist)
    if (min_n == abs(x-k)):break
    if (min_n == 0):break
print(min(rlist))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
i = 0
c = max(n, k) / min(n, k)
if k == 1:
    print("0")
else:
    while i <= c:
        x = abs(x - k)
        i += 1
print(min(n, x))

Submission status: Runtime Error

Solution code:

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

if K == 1:
    print(0)

else:
    shou, amari_1 = divmod(N, abs(N-K))
    N_min_1 = N - (shou * abs(N-K))
    N_min_2 = abs(N_min_1 - abs(N-K))
    print(min(N_min_1, N_min_2))

Submission status: Runtime Error

Solution code:
list=list(map(int,input().split()))
x=list[0]
k=list[1]
if list[0]>=list[1]:
  y=x%k
z=k-y
if z >=y:
  print(y)
else:
  print(z)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
if 0 <= N <= 10^18 and 1 <= K <= 10^18:
    a = abs(N-K)
    b = abs(a-K)
print(b)

Submission status: Runtime Error

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

k = N/K
print(K - N*k)

Submission status: Runtime Error

Solution code:
def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
    else:
        print(b)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
    else:
        print(abs(a[0] % a[1] - a[1]))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
i = 1
lis = []
while i < N:
    N = abs(N-4)
    lis.append(N)
    i += 1
print(min(lis))

Submission status: Runtime Error

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

def okikae(t):
    if K==0:
        print(N)
    elif N%K==0:
        print("0")
    elif K/2 >= t and t <= K:
        print(t)
    else:
        okikae(abs(t-K))

okikae(N)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
sa=abs(n-k)

if n < k:
    at=n
else:
    at=n%sa
    
while abs(at - k)<at:
    at = abs(at - k)

print(at)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
if 0 <= N <= 10^18 and 1 <= K <= 10^18:
    a = abs(N-K)
    b = abs(a-K)
print(b)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    if n>=k:
        n%=k
        print(k%n)
        exit()
    if n<=k//2:print(n)
    else: print(k-n)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
while True:
    if ((n-k) > 10000):
        n = n // 100
    else:
        an = abs(n-k)
        if n <= an:
            break
        else:
            n = an
print(n)

Submission status: Runtime Error

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    f = abs(n-k)
    print(max(f,k)%min(f,k))
    return

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
X = list(map(int, input().split()))
N = X[0]
K = X[1]
A = math.inf
if K == 1:
  print(0)
else:  
  while True:
    A = abs(N - K)
    if N > A:
      N = A
    else:
      break
  print(N)

Submission status: Runtime Error

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(N)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if (N%2 == 0 and K%2 == 0):
  if N > K:
    print(0)
    sys.exit(0)
  else:
    print(N)
    sys.exit(0)
if (N%2 != 0 and K%2 != 0):
  if N<K:
    print(N)
    sys.exit(0)
  else:
    print(min(N,abs(N-K),Q))
    sys.exit(0)
else:
  if N > K:
   	if N%K == 0:
      print(0)
      sys.exit(0)
    else:
      print(min(N,Q,fractions.gcd(N,K)))
      sys.exit(0)
  else:
    print(min(N,Q))

Submission status: Runtime Error

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

test = n

while True:
  zet = abs(test-k)
  if zet == 0:
    print(zet)
    break
  if zet > test:
    print(f)
    break
  test = zet

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
Nm = N % K
ans = Nm
A = [0]*(K+1)
A[Nm] = 1
while 1:
    if ans == 0:
        break
    elif A[abs(Nm-K)] != 1:
        if abs(Nm-K) < ans:
            ans = abs(Nm-K)
        A[abs(Nm-K)] = 1
        Nm = abs(Nm-K)
    else:
        break
    
print(ans)

Submission status: Runtime Error

Solution code:
list1=[]
a,b=map(int,input().split())
d=a
for i in range(d):
    e=a
    a=abs(a-b)
    list1.append(a)
    if e<a:
        break
list2= sorted(list1, reverse=True)
print(list2[-1])

Submission status: Runtime Error

Solution code:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif k == 2:
    if n % 2 = 0:
      print(2)
    else:
      print(3)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

if n>=k:
    if n%k != 1 or n%k != 2 or n%k != 0:
        print(n%(k-1))
    else:
        print(n%k)
elif n<k:
    print(n)

Submission status: Runtime Error

Solution code:
x,y=list(map(int,input().split()))
h=[]
while x>y:
  x-=y
  if (x-y)!=0:
    t=x-y
    h.append(t)
  else:
    u=abs(x)
    h.append(u)
print(min(h))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    if n>=k:
        n%=k
        print(k%n)
        exit()
    else: print(n if n<=k//2 else k-n)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

if k==1 :
  ln=0
elif n>=k and k!=0 :
  if n//k==0:
    ln=0
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)

Submission status: Runtime Error

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

def okikae(t):
    if N%K==0:
        print("0")
    elif K/2 >= t and t <= K:
        print(t)
    else:
        okikae(abs(t-K))

okikae(N)

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
def d(a,b):
    if a==1 or b==1:
        return 0
    elif a<b:
        if min(l)!=b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            return b-a
    elif b<a:
        if min(l)!=a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            return a-b
print(d(a[0],a[1]))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    ave=float(k/2)
    if n>=k:
        n%=k
        print(k%n)
        exit()
    else: print(n if n<ave else k-n)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
 
s1 = n//k
if s1 == 0:
    s1 = k//n
    ans = min(abs(k - n*(s1-1)),abs(k - n*(s1),))
    print(ans)
else:
    ans = min(abs(n - k*(s1+1)),abs(n - k*(s1),))
    print(ans)

Submission status: Runtime Error

Solution code:
ans = 1
    while True :
        if ans < 0 :
            print(abs(ans))
            break
        ans = N - K
        N = ans

Submission status: Runtime Error

Solution code:
list=list(map(int,input().split()))
k=list[1]
y=max(list)%min(list)
z=k-y
if z >=y:
  print(y)
else:
  print(z)

Submission status: Runtime Error

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

ans = N % abs(N-K) if K!=1 else 0
print(ans)

Submission status: Runtime Error

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

if k==1 :
  ln=0
elif n>=k :
  if n//k==0:
    ln=0
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)

Submission status: Runtime Error

Solution code:
import math
N, K = map(int,input().split())
N = math.gcd(N,K)
for i in range(10*6):
    N = abs(N-K)
    if N == 0:
        break
print(N)

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()

    r = N%K
    if r == 0:
        print(0)
    else:
        if 2*r > K:
            print(abs(r - K))
        else:
            print(r)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ans = n
all = [n]
if n == 1 or k == 1 or n == 2*k or k == 2*n:
  ans = 0
elif abs(n - k) < n or abs(n - k) < k:
  ans = abs(n - k)
else:
  all = []
  for i in range(2*n):
    n = abs(n - k)
    all.append(n)

  
print(min(ans, min(all)))

Submission status: Runtime Error

Solution code:
import math

N, K = [int(n) for n in input().split()]
print(0 if K==1 or N==0 else math.gcd(N, K))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
result = []
while N > 0
    M -= abs(N - M)
    result.append(M)
print(min(result))

Submission status: Runtime Error

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

if N < K:
    N, K = K, N

past = N

N = N % K

while N != past:
    past = N
    N = abs(N-K)
    if N < K:
        N, K = K, N

print(min(N, K))

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
def d(a,b):
    if a<b:
        if min(l)!=b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(b-a)
            exit()
    elif b<a:
        if min(l)!=a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(a-b)
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

Solution code:

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

if K == 1:
    print(0)

else:
    shou, N_min_1 = divmod(N, abs(N-K))
    N_min_2 = abs(N_min_1 - abs(N-K))
    print(min(N_min_1, N_min_2))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if n>k:
    n=n-k
    print(n%val)
else:
    print(n)

Submission status: Runtime Error

Solution code:
from fractions import gcd
n,k = map(int,input().split())

def process(a,b):
	while flag:
		a,b = max(a,b),min(a,b)
		a = a%b
		if a == 0:
			flag = False

	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
sa=abs(n-k)

at=n%sa
while abs(at - k)<at:
    at = abs(at - k)
# if abs(at - k)<at:
#     print(abs(at-k))
# else:
print(at)

Submission status: Runtime Error

Solution code:
list1=[]
a,b=map(int,input().split())
d=a
for i in range(d):
    a=abs(a-b)
    list1.append(a)
list2= sorted(list1, reverse=True)
print(list2[-1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    ave=float(k/2)
    ans=n
    dis=abs(ans-k)
    if n>=k:
        ans=n%k
        if k>=ans:
            print(k%ans)
            exit()
    print(n if n<ave else k-n)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, input().split())

while True:
    if ((n-k) > 10000):
        n = n // 10000
    else:
        an = abs(n-k)
    if n <= an:
        break
    else:
        n = min(n, an)
print(n)

Submission status: Runtime Error

Solution code:
import math
N, K = map(int,input().split())

N = math.gcd(N,K)

for i in range(10*6):
    N = abs(N-K)
    if N == 0:
        break

print(N)

Submission status: Runtime Error

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

ans = k
all = [k]
if n == 1 or k == 1 or n == 2*k or k == 2*n:
  ans = 0
else:
  all = []
  for i in range(2*n):
    n = abs(n - k)
    all.append(n)

  
print(min(ans, min(all)))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
min_n = n
rlist = []
while x>0:
    rlist.append(abs(x-k))
    x = abs(x-k)
    min_n = min(rlist)
    if (min_n == abs(x-k)):break
print(min(rlist))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
sa=abs(n-k)

at=n%sa
if abs(at - k)<at:
    print(abs(at-k))
else:
    print(at)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    ave=float(k/2)
    ans=n
    dis=abs(ans-k)
    if n>=k:
        ans=n%k
        if k>ans:
            print(k%ans)
            exit()
    print(n if n<ave else k-n)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
str = input().split()
a1 = int(str[0])
a2 = int(str[1])
diff = abs(a1-a2)
if diff > a2 :
    larger = diff
    smaller = a2
else:
    larger = a2
    smaller = diff
print(larger%smaller)

Submission status: Runtime Error

Solution code:
import math
N, K = map(int,input().split())

#print(N,K)

N = math.gcd(N,K)

for i in range(10*7):
    N = abs(N-K)
    if N == 0:
        break

print(N)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
if (K / N) > 1:
    print(N)
else:
    amari = N % K
    mainasu_noatai = abs(amari - K)
    if (amari > mainasu_noatai):
    	print(mainasu_noatai);
    else:
        print(amari)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
if N > K and N//K>2:
	a = N//K
	N= N-K*(a-2)

while N>0:
	T = N
	N = abs(N-K)
	if N >T:
		break

print(min(N,T))

Submission status: Runtime Error

Solution code:
import math
n,k = list(map(int,input().split()))

if n < k:
    if k - n > n:
        print(n)
    else:
        print(k - n)
else:
    if n % k == 0:
        print(0)
    else:
        print(math.gcd(n, k))

Submission status: Runtime Error

Solution code:
import math

N, K = [int(n) for n in input().split()]
print(0 if K==1 else math.gcd(N, K))

Submission status: Runtime Error

Solution code:
def main():
  n,k = map(int, input().split())
  
  if k == 1:
    return 0
  
  result = check(n, k, [n])
    
  return result

def check(val1, val2, checked):
  tmp = val1 - val2 if val1 - val2 > 0 else val2 -val1
  if tmp == 0:
    return 0
  if tmp in checked:
    return min(checked)
  checked.append(tmp)
  return check(tmp, val2, checked)
  
print(main())

Submission status: Runtime Error

Solution code:
import math

n, k = [int(x) for x in input().strip().split()]

res = math.gcd(n,k)

if n>=k:
    if res==k:
        res=0

print(res)

Submission status: Runtime Error

Solution code:
import math

#n,k
n,k = (int(x) for x in input().split())

b = n % k

print(b)

#c = abs(k-b)

if b < c:
    ans = b
else:
    ans = c

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if n>k:
    val=n-k
    print(n%val)
else:
    val=k-n
    print(k%val)

Submission status: Runtime Error

Solution code:

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

shou, N_min_1 = divmod(N, abs(N-K))
N_min_2 = abs(N_min_1 - abs(N-K))
print(min(N_min_1, N_min_2))

Submission status: Runtime Error

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = 10
l = 0
while v > l:
  v = l
  l = abs(n - k)

print(l)

Submission status: Runtime Error

Solution code:
from math import gcd
n,k = map(int,input().split())

def process(a,b):
	a = gcd(a,b)
	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
left = 0
right = 10**19

while right - left >1:
  middle = (right + left)//2
  if middle < abs(middle - k) :
    left = middle
  else:
    right = middle
if n < k and k //n == 2:
  print(n)
elif n//k ==2:
    print(0)
else:
  print(min(left,abs(n-k),n))

Submission status: Runtime Error

Solution code:
x,y,z=map(int,input().split())
print(z,x,y)

Submission status: Runtime Error

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

a = N % K
b = K % a

if (N > K) and a < b:
	print (a)
elif (N > K) and b < a:
	print (b)
elif (N < K):
	print (a)

Submission status: Runtime Error

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

delta = abs(N-K)

while N>=delta:
  N//=delta
  delta = abs(N-K)
  if delta==0:
    N=0
    break
  
print(N)

Submission status: Runtime Error

Solution code:

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

shou, N_min = divmod(N, abs(N-K))
print(N_min)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
Print(abs((N-M))-N)

Submission status: Runtime Error

Solution code:
def rec(x,k):
    tmp = abs(x-k)
    if(tmp>x):
        return x
    else:
        return rec(tmp,k)

def main():
    n,k = list(map(int,input().strip().split()))
    print(rec(n,k))

main()

Submission status: Runtime Error

Solution code:
import sys

table = list(map(int, input().split()))
min = abs(table[0] - table[1])
table[0] = min

if not table[1] == 0:

    if table[0] > table[1]:
        if table[0] % table[1] == 0:
            print(0)
            sys.exit()
    else:
        if table[1] % table[0] == 0:
            print(0)
            sys.exit()

while True:
    if min <= abs(table[0] - table[1]):
        break
    min = abs(table[0] - table[1])
    table[0] = min
print(min)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define F(i,n) for(i = 0 ; i < n ; i++)
typedef long long ll;


/*ll n;

void solve()
{
	cin>>n;
	vector< int > ans;
	ans.push_back(1);
	ve
	for(int i = 1 ; i <= n ; ++i){
		ans.push_back(i);
		bool p =true ;
		for(int j = i +1 ; j <= n ; j++) {

			if(__gcd(i,j) == 1)
				p = false;
		}
		if(!p)

		cout<<ans.size() <<" ";
		for(int i = 0 ; i < ans.size() ; i++)
		cout<<ans[i]<<" ";
	cout<<endl;
	}
	

   

}

*/




int main()
{
	
    
	ll n , k ;
	cin>>n >> k;
	int mind = abs(n - k) ;
	while(1){
		n = abs(n - k);
		if(n < mind)
			mind= n;
		else
			break;
	} 
	cout<<mind ;
	

}

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
n_1 = N - (K * (N // K))
n_2 = abs(n - K)
print(min(n_1, n_2))

Submission status: Runtime Error

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

a = N % K
b = K % a

if (N > K) and a < b:
	print (a)
elif (N > K) and b < a:
	print (b)
elif (N < K):
	print (N)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=n-k*(-((k-n)//k))
print(min(a,abs(a-k))

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    value_diff_abs = 0

    if K == 1 :
        print("0")
    elif K == 2 and N % 2 == 0 :
        print("0")
    elif K == 2 and N % 2 != 0 :
        print ("1")
    else :
        for i in range(0 , (10^18 // N) + 2) :
            value_diff_abs = N
            N = abs(N - K)
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

a = N // K
b = ceil(N / K)
# print(a, b)
c,d = N-a, N-b
# print(c,d)
# print(N-c,N-d)

#
x = N // (N - K)
y = x+1
# print(x, y)
a = N - x * (N-K)
b = N - y * (N-K)
# print(a, b)

# print(min(abs(a), abs(b)))

if N < K:
    print(min(K-N, N))
elif K == 1:
    print(0)
else:
    ans = min(abs(a), abs(b))
    print(ans)

# if N >= K:
#     ans = min(abs(a), abs(b))
# # K > N:
# else:
#     ans = min(K-N, N)
# print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
import math

if k == 1:
    print(0)
else:
    print(math.gcd(n, k))

Submission status: Runtime Error

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

if N<=K:
    while K<2*N:
        N=abs(N-k)
    print(N)
else:
    R=N%K
    print(min(R,K-R))

Submission status: Runtime Error

Solution code:
import sys
n,k = map(int,input().split())
if n%k < abs((n%k) - k):
    print(n%k)
    sys.exit(n%k)
print(abs((n%k) - k))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000000000001)
n,k=map(int,input().split())
for v in range(1000000000000000001):
    if k*v >= n:
        t = v
        tt = v - 1
        break
if t*k - n >= n - tt*k:
    print(str(n - tt*k))
else:
    print(str(t*k - n))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import math
n, k = map(int, input().split())

if n < k:
    print(n)
elif k == 1 or (k % 2 == 0 and n % 2 == 0):
    print(0)
elif (k % 2 == 0 and n % 2 == 1) or (n % 2 == 0 and l % 2 == 1):
    print(1)

Submission status: Runtime Error

Solution code:
a=n/k
if a >1:
    b=n%k
    if b==0:
        print(0)
    else:
        print(abs(b-k))
else:
    print(n)
    exit()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200000000000000000000000000000000)
n,k=map(int,input().split())
for v in range(0,1000000000000000000):
    if k*v >= n:
        t = v
        tt = v - 1
        break
if t*k - n >= n - tt*k:
    print(str(n - tt*k))
else:
    print(str(t*k - n))

Submission status: Runtime Error

Solution code:
import sys

table = list(map(int, input().split()))
min = abs(table[0] - table[1])
table[0] = min

if table[0] > table[1]:
    if table[0] % table[1] == 0:
        print(0)
        sys.exit()
else:
    if table[1] % table[0] == 0:
        print(0)
        sys.exit()

while True:
    if min < abs(table[0] - table[1]):
        break
    min = abs(table[0] - table[1])
    table[0] = min
print(min)

Submission status: Runtime Error

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


if K > N:
    print(N)
    exit()

a = abs(N - K)
ans1 = N % a
ans2 = abs((N % a) - a)
if K == 1:
    print(0)
else:
    print(min(ans1, ans2))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
if K==0:
  ans=N
elif N%K==0 and K%N==0:
  ans=0
else:
  if N%2==0:
    ans=min(N,abs(N-K))
  else:
    ans=1
print(ans)

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(200000000000000000000000)
n,k=map(int,input().split())
for v in range(0,1000000000000000000):
    if k*v >= n:
        t = v
        tt = v - 1
        break
if t*k - n >= n - tt*k:
    print(str(n - tt*k))
else:
    print(str(t*k - n))

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
def abc(n, m):
    if n<=abs(n-m):
        return n
    else:
        return abc(abs(n-m), m)
p=abc(N,M)
print(p)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
if max(N,K) % min(N,K) == 0:
    print(0)
else:
    while N > abs(N - K):
        N = abs(N - K)
    print(N)

Submission status: Runtime Error

Solution code:
NK = input().split()
N = int(NK[0])
K = int(NK[1]

def function(N,K):
    pre =N
    if N > K*10:
        return 0



    while(1):    
        # print('N',N)
        N = abs(N-K)
        if pre == N:
            return pre

        if pre< N:
            return pre
        pre = N 

print(function(N,K))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
#L = list(map(int,input().split()))
#a = max(n,k)%min(n,k)
L = []
a = 1
while a <=10:
    if  max(n,k)%min(n,k) == 0:
        print(0)
        exit()
    if n >= k and k!=0:
        n = n%k
        if n == 0:
            print(k)
    elif n<k and n!=0:
        k = k%n
        if k == 0:
            print(n)
    L.append((n,k))
    a +=1

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
def abc(n, m):
    if n<=abs(n-m):
        return n
    else:
        return abc(abs(n-m), m)
    p=abc(N,M)
print(p)

Submission status: Runtime Error

Solution code:
import math
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1:
    print(0)
    exit()
elif N==0:
    print(K)
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

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

if N < K:
    k = K % N
    ans = abs(N - k)
    print(ans)
elif N > K:
    k = N % K
    ans = abs(K - k)
    if k == 0:
        print("0")
    else:
        print(ans)
else:
    print("0")

Submission status: Runtime Error

Solution code:
from math import gcd

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

if n == 1 or k == 1:
	print(1)
else:
	print(gcd(n, k))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
suma=sum(a)
l=suma/(4*m)
a.sort(reverse=True)
if a[m-1]>=l:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
x,y= list(map(int, input().split()))
if x%y==0:
    print(0)
else:
    print(min((x%y),(y-(x%y)))

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
def abc(n, m):
    if n<=abs(n-m):
        return n
    else:
        return abc(abs(n-m), m)
print(abc(N,M))

Submission status: Runtime Error

Solution code:


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

ans = n % abs(n-k)

ans = min(ans, n, k)

if ans == 1 and k == 1:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import math
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1 or N ==0:
    print(0)
    exit()
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split()) 
A = sorted(map(int, input().split()), reverse=True)

if sum(A)/(4*M) >= A[M-1]:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
from fractions import gcd

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

if n == 1 or k == 1:
    print(0)
else:2 6
    print(gcd(n,k))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if(n>k):
    if(n%k==0):
        print(0)
    else:
        print(min((n%k),(n//k+1)*k-n))
elif(n==k):
    print(0)
else:
    if(k%n==0):
        print(n)
    else:
        if(k-n)<=n:
            print(k-n)
        else:
            print(n)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
def gcd(x, y):
    if x>y:
        if y==0:
            return x
        return gcd(y,x%y)
    else:
        if x==0:
            return y
        return gcd(x,y%_)
if (n%m==0):
    print(0)
else:
    print(gcd(n,m))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
tmp = min(n,k)
n = max(n, k)
k = tmp

def calc(n, k):
  if n < k:
    return abs(n-k)
  else:
    return calc(abs(n-k), k)
 
if k == 1:
  print(0)
else:
  print(calc(n,k))

Submission status: Runtime Error

Solution code:
print(int(input()) % int(input()))

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()

    ans = gcd(N, K) 
    print(0 if ans == 1 else ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
NK = input().split()
import math
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1:
    print(0)
    exit()
else:
    print(math.gcd(N,K))