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

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

if abs(N-K) >= N:
    print(N)

elif N % K % 2 == 1:
    print(1)

elif N % K == 0:
    print(0)

else:
    print(2)

Submission status: Wrong Answer

Solution code:
aa=list(map(int,input().split()))
n=aa[0]
k=aa[1]
minn=n%k
print(abs(min(n%k,(n%k)-k)))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N == 0:
    print(0)
    exit()
if N < K:
    print(N)
    exit()

if N % K == 0:
    print(0)
else:
    print(K - N%K)

Submission status: Wrong Answer

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

min_0 = K
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

print(min(min_0, min_1, min_2))

Submission status: Wrong Answer

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

if N % K == 0:
    print(0)
else:
    q, r = divmod(N, N - K)
    print(abs(r))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
if n//k != 0:
    print(min(n%k, abs(k-n%k)))
else:
    print(min(n%k, n))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
kari=int(N/K)
N=N-K*kari
while True:
    if abs(N-K)>=N:
        print(N)
        exit()
    else:
        N=abs(N-K)

Submission status: Wrong Answer

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

while True:
    dif = abs(n-k)
    if dif == 0:
        n = 0
        break
    old_n = n
    n = n % dif
    if n==old_n:
        break

print(n)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

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

Submission status: Wrong Answer

Solution code:
NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

def rep(x, K):
    return abs(x-K)

def solve(N, K):

    if K == 1:
        return 0
        
    bef = rep(N, K)
    
    while(True):
        a = rep(bef, K)
        if bef < a:
            bef = a
        else:
            break
    return a

print(solve(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if n % 4 == 0:
    ans = 0
else:
    x = n % k
    y = abs(k - x)
    ans = min(x,y)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=(int(i) for i in input().split())
print(n%k)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
min_kakeru = int(n/k)
max_kakeru = int(n/k) + 1
a1 = abs(n - min_kakeru * k)
a2 = abs(n - max_kakeru * k)

if a1 < a2:
  print(a1)
else:
  print(a2)

Submission status: Wrong Answer

Solution code:
a,b=map(int, input().split())

if a > b:
    tmp = a % b
    tmp2 = -1 * (tmp - b)
    if tmp > tmp2:
        print(tmp2)
    else:
        print(tmp)
elif a < b:
    print(a)
elif a == b:
    print(a)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
print(gcd(n,m)) #出力値：10

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
flag=True
tmp=n-int(n/k)*k
cnt=k

while(flag):
    if tmp > abs(tmp-k) :
        tmp=abs(tmp-k)
        cnt+=1
    else:
        flag=False
        print(tmp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
if N >= K:
    N -= (N - K) // K
N = min(N, abs(N-K))
if N == K:
    print(0)
    exit()
if N > (K / 2):
    print(K - N)
    exit()
print(N)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N == 0:
    print(K)
    exit()
if N < K:
    print(N)
    exit()

if N % K == 0:
    print(0)
else:
    print(K - N%K)

Submission status: Wrong Answer

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

a=int(n/k)
b=abs(n-k*a)
c=abs(n-k*(a+1))
print(min(b,c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K:
  print(N)
else:
  div = N//K
  print(min(abs(N-div*K), abs(N-(div+1)*K)))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mov = n%k
ans = min(mov,abs(mov-k))

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    if n < k:
        print(n)
        return

    a = n // k
    b = n - a*k
    c = abs(b - k)
    print(min(b, c))


main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, K = map(int, input().split())
if (N < K): N, K = K, N
if (K == 1 or N == K):
    print(0)
    exit(0)
print(gcd(N, K))

Submission status: Wrong Answer

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

if N<=K:
    print(N)
else:
    a = K*(N//K)
    b = K*(N//K + 1)
    print(min(abs(N-a), abs(N-b)))

Submission status: Wrong Answer

Solution code:
import sys
import math
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import itertools
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

ans = N % 4
if ans > abs(K - ans):
    ans = abs(K - ans)
    print(ans)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
if N*2<M:
    print(N)
elif M == 1:
    print(0)
else:
    yo = N % M
    print(M-yo)

Submission status: Wrong Answer

Solution code:
from math import ceil
n, k = map(int, input().split())
val = ceil(n/k)
print(min(n%k,abs(n-(k*val)),n))

Submission status: Wrong Answer

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()

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

if N % K == 0:
    print(0)
else:
    print(min(N, gcd(N,K)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = map(int,input().split())
x = np.roots([-K,N])[0].astype(int)
print(min(abs(N - K * x),abs(N - K *(x+1))))

Submission status: Wrong Answer

Solution code:
n,k =input().split();
n = int(n);
k = int(k);
x = int(n/k);
 
print(n-x*k)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N == 0:
    print(K)
if N < K:
    print(N)
else:
    if N % K == 0:
        print(0)
    else:
        print(K - N%K)

Submission status: Wrong Answer

Solution code:
I=lambda:list(map(int,input().split()))
n,k=I()
print(n-k*(n//k))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
n1 = int((N/K + 0.5)//1)
n2 = int((N/K - 0.5)//1)
if (N/K - 0.5)%1 != 0: n2 += 1

print(min(abs(n1*K-N),abs(n2*K-N)))

Submission status: Wrong Answer

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

if k == 1:
    print(0)
    exit()

low = 10**9
while True:
    tmp = abs(n-k)
    if tmp < low:
        low = tmp
        n = tmp
    else: break

print(low)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
if (N//K)==0:
  print(N)
else:
  x=N-K*(N//K)
  print(min(x,abs(x-K)))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
print(N%K,N%K-K,min(N%K,abs(N%K-K)))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, ans, abs(N - K*(N//K) )))

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


if __name__ == '__main__':
    b, c = map(int, input().split())
    maxnum =b
    print( abs(b% c) )

Submission status: Wrong Answer

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

if K > N:
    print(N)
else:
    t1 = int(N/K)
    t2 = t1 + 1
    ans1 = abs(N-K*t1)
    ans2 = abs(N-K*t2)
    print(min([ans1,ans2]))

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
print(min(N, abs(N % K - K)))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if N == 0 or K == 1:
    print(0)
    exit()

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

print(gcd(N,K))

Submission status: Wrong Answer

Solution code:
def solve():
    N, M = map(int, input().split())
    a = abs(N-M)
    
    if a == 0:
        return 0
    
    if a > N:
        return N
    
    b = N // a
    d = N % b
    
    return d

print(solve())

Submission status: Wrong Answer

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

right=N%K
left=int(N/K+1)*K-N
#print(left,right)

if right>left:
    print(left)
else:
    print(right)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if k == 1 or n == k:
    print(0)
elif n < k:
    print(n)
else:
    div_num = n % k
    print(min(div_num, abs(div_num - k)))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, K = map( int, input().split() )
print( min( gcd( N, K ), ( N % K ) ) )

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    print(fractions.gcd(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N<= K:
    print(min(N,K-N))
else:
    N = N - math.floor(N/K)*K 
    print(min(N, K))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

g = gcd(N, K)
ans = min(g, abs(K-g))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
while(1):
	A=N%K
	if N<K:
		print(N)
		exit()
	elif A==0:
		print(0)
		exit()
	else:
		print(1)
		exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n%k == 0:
    print(0)
else:
    print(min(abs(n-k),abs(abs(n-k)-k)))

Submission status: Wrong Answer

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

min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

if N==0:
  print(K)
else:
  print(min(min_1, min_2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n % k == 0:
    print(0)
else:
    print(min(n, n % abs(n - k), n % k))

Submission status: Wrong Answer

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)
    if f==0 or k==1:
        print(0)
        return
    print(n%f)
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

d = int(N/K)
if d == 0:
    print (N)
else:
    if N%K == 0:
        print (abs(N - K*d))
    else:
        print (abs(N - K*(d+1)))

Submission status: Wrong Answer

Solution code:
# ABC161_C
from fractions import gcd
n,k=map(int,input().split())
if k==1:
    print(0)
    exit()
ans=0
ans=gcd(n,k)

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
#操作: NをNとKの差の絶対値で置き換える。
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
print(fractions.gcd(N,K))

Submission status: Wrong Answer

Solution code:
import  fractions
n, k = map(int, input().split())
if k == 1:
    print(0)
else:
    print(fractions.gcd(n, k))

Submission status: Wrong Answer

Solution code:
# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


N, K = map(int, input().split())
if K == 1 or N == 0 or N % K == 0:
    print(0)
else:
    print(gcd(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
aa=list(map(int,input().split()))
n=aa[0]
k=aa[1]
minn=n%k
if minn==0:
    print("0")
elif n<k:
    print(n)
else:
    print(abs(min(n%k,(n%k)-k)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from fractions import gcd
N, K = map(int, input().split())
if N%K == 0:
    print(0)
else:
    print(gcd(N, K))

Submission status: Wrong Answer

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

def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a

x = gcd(max(k, n), min(k, n))
if n % k == 0:
    print(0)
else:
    print(x)

Submission status: Wrong Answer

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

temp=min(n,k)

for i in range(10^6):
    temp=min(temp,abs(temp-k))


print(temp)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
ans = 100000
for i in range(1000000):
    k = abs(N-K)
    if ans > k:
        N = k
        ans = k
if N != 1 and K != 1:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

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

min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

if N==0:
  print(1)
else:
  print(min(min_1, min_2))

Submission status: Wrong Answer

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

if N >= K and N % K == 0: ans = 0
else:
    ans = gcd(N,K)
    if K == 1: ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n%k == 0:
    print(0)
elif n%2 == 0 and k%2 == 0:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

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

N, K = map(int, input().split())
if N <=K:
    print(min(abs(N-K), N))
else:
    print(N%K)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N < K:
    print(N)
else:
    if N % K == 0:
        print(0)
    else:
        print(K - N%K)

Submission status: Wrong Answer

Solution code:
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
a,b=map(int,input().split())
if a==0:
    print(0)
else:
    if a%b==0:
        print(0)
    else:
        print(gcd(a,b))

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if n % k == 0 and 1 < k:
        print(0 if n - k * k <= 0 else n - k * k)
    else:
        print(gcd(n, k) if 1 < k else 0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b=map(int, input().split())

if a > b:
    tmp = a % b
    tmp2 = -1 * (tmp - b)
    if tmp > tmp2:
        print(tmp2)
    else:
        print(tmp)
elif a < b:
    print(a)
elif a == b:
    print(0)

Submission status: Wrong Answer

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()

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

if N % K == 0:
    print(0)
else:
    print(gcd(N,K))

Submission status: Wrong Answer

Solution code:
def main():
    N,K = map(int,input().split())
    a = int(N / K)
    N = N - a * K
    while True:
        N2 = abs(N - K)
        if N2 < N:
            N = N2
        elif N2 >= N:
            break
    print(N)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=n%k
if n<k:
    print(n)
    exit()

if n%k==0:
    print(0)
else:
    print(min(k-a,a))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
kari=int(N/K)
N=N-K*kari
while True:
    if abs(N-K)>N:
        print(N)
        exit()
    else:
        N=abs(N-K)

Submission status: Wrong Answer

Solution code:
from math import ceil
n, k = map(int, input().split())
val = ceil(n/k)
print(min(n%k,abs(n-(k*val))))

Submission status: Wrong Answer

Solution code:
import fractions
N,K = map(int, input().split())
if K == 1 or N == 0 or N % K == 0:
  print(0)
else:
  print(fractions.gcd(N,K))

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
if N*2<M:
    print(N)
else:
    yo = N % M
    print(M-yo)

Submission status: Wrong Answer

Solution code:
import fractions
n, k = map(int, input().split())
if n%k==0:
    print(0)
else:
    print(fractions.gcd(n, k))

Submission status: Wrong Answer

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

if n < k:
	print(n)
else:
	if n % k == 0:
		print(0)
	else:
		print((k - n) % k)

Submission status: Wrong Answer

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 < 0:
    return abs(n-k)
  else:
    return calc(k, abs(n-k))
        
if n == 1 or k == 1:
  print(0)
else:
  print(calc(n,k))

Submission status: Wrong Answer

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

min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

print(min(min_1, min_2))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
N,K = LI()
if N == 1 or K == 1:
    print (0)
    exit (0)
if N == 0:
    print (K)
    exit(0)
print (gcd(N,K))

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

if M==1:
    print(0)
else:
    print(gcd(N, M))

Submission status: Wrong Answer

Solution code:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left #bisect_left(list, value)
#from fractions import gcd
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

d = defaultdict(int)
res = N
x = N
while 1:
    val = res//K
    x = abs(res-val)
    y = abs(res-K)
    d[x] += 1
    d[y] += 1
    #print(res, x, y)
    res = min(res, x, y)
    if d[x] > 10:
        break
print(res)

Submission status: Wrong Answer

Solution code:
from math import floor

n, k = map(lambda i: int(i), input().split())

if n / k == 0:
    print(0)
else:
    c = floor(n/k)
    print(min(abs(n-c*k), abs(n-k*(c+1))))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

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

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N < K:
    print(N)
    exit()
if N % K == 0:
    print(0)
else:
    print(K - N%K)

Submission status: Wrong Answer

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

if n >= k:
    tmp = n%k
    if tmp > abs(tmp-k):
        tmp = abs(tmp-k)
else:
    tmp = n

print(tmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
num=int(N/K)
a=N-K*num
b=N-int(K*(num+1))
if(a<0):
  a*=-1
if(b<0):
  b*=-1
if(a<b):
  print(a)
else:
  print(b)

Submission status: Wrong Answer

Solution code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p


n, k = map(int, input().split())
if n%k == 0:
  print(0)
elif n < k:
  print(min([n, k-n]))
else:
  print(gcd(n, k))

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial
from fractions import gcd
import sys
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 10**9+7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N, K = mii()

if K == 0 or N == K:
    print(0)
    exit()

ans = N % K
ans = min(ans, N % abs(N-K))

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p


n, k = map(int, input().split())
if n%k == 0:
  print(0)
elif n < k:
  print(min(n, k-n))
else:
  print(gcd(n, k))

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

ans = 0
if N > K:
    ans = N % K
else:
    ans = min(N, abs(N-K))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
q = N % K
ans = min(q, N-q)
print(ans)

Submission status: Wrong Answer

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

ans = 0
if N>1 and K>1:
    ans = gcd(N,K)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
tmp = N%K
print(min(N,K-tmp))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n % k == 0:
    print(0)
else:
    print(min(n, n % abs(n - k)))

Submission status: Wrong Answer

Solution code:
#AtCoder
N, K = map(int, input().split())
q, mod = divmod(N, K)
print(min(q, mod))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = n
mn = x%k
for _ in range(1000): # Should be 1 I think, but whatever
    x = abs(x - k)
    mn = min(x, mn)
print(mn)

Submission status: Wrong Answer

Solution code:
import fractions
n, k = map(int, input().split())
if n == 0:
    ans = n
elif n == 1 or k == 1:
    ans = 0
else:
    ans = fractions.gcd(n, k)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math


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

a = n - math.ceil(n/k) * k
b = n - math.floor(n/k) * k

if a < 0:
	a = a * -1
if b < 0:
	b = b * -1


print(min([a, b]))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
from fractions import gcd
ans = gcd(N,K)
if N%K==0:
  ans = 0


print(ans)
#print(*ans,sep=' ')

Submission status: Wrong Answer

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

if n % k == 0:
    print(0)
else:
    while n > abs(n-k):
        n = abs(n-k)
print(n)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
t, v = divmod(N, K)
print(min(t,v))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
from fractions import gcd
print(gcd(N, K))

Submission status: Wrong Answer

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

print(N % K)

Submission status: Wrong Answer

Solution code:
# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

N,K = map(int,input().split())
if K == 1:
    print(0)
else:
    print(gcd(N,K))

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    return x if y == 0 else gcd(y, x % y)

n, k = map(int, input().split())
print(gcd(n, k) if k != 1 else 0)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=n%k
if n<k:
    print(n)
    exit()

if n%k==0:
    print(0)
else:
    print(k-a)

Submission status: Wrong Answer

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

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

if K == 1:
    print(0)

elif N == K:
    print(0)

elif N < K:
    print(min(K - N, N))

else:
    M = N - K
    print(min(K - M, N))

Submission status: Wrong Answer

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

print((n+k)%k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if N % K == 0:
        print(0)
    else:
        print(gcd(N, K))
    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

#import sys
#input = sys.stdin.readline
def main():
    N, K = map( int, input().split())
    if N >= K:
        if N%K == 0:
            print(0)
            return
    print( gcd(K,N))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import *

N, K = map(int, raw_input().split())
print gcd(N, K) if N%K!=0 else 0

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if n % k == 0:
        print(0)
    else:
        print(gcd(n, k))


if __name__ == '__main__':
    main()