peterverebics/CodeNet_Python_118 huggingface dataset:
n,k = map(int,input().split())
n = n % k
if n == 0:
    print(0)
    exit()

if n < k:
    print(n)
    exit()

while True:
    ans = n - k
    next_ans = abs(ans) - k
    if abs(next_ans) > abs(ans):
        print(abs(ans))
        break
    else:
        print(abs(next_ans))
        break
n,k = map(int, input().split())
min = n + 1
if n >= k:
    n = n % k
else:
    n = abs(k - n)
while True:
    if n < min:
        min = n
        n = abs(n - k)
    else:
        break

print(min)
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k == 1:
	print(0)
elif n > k:
	print(1)
else:
	print(k%n)
n, m = map(int, input().split())
l = []
ans = 1
if(n%m == 0):
  ans=0
else:
  for x in (0,1000000000000000000):
    n = abs(n%m)
    l.append(n)
  l.sort()
  ans = min(n,l[0])
print(ans)
N, M = map(int, input().split())
check = N - M
result = abs(check) - M
print(abs(result))
import numpy as np
import math as mt
N,K=map(int, input().split())
if abs(N-K)<N:
    print(N-K*(N//K))
else:
    print(N)
n,k = map(int,input().split())
t = n//k
if (n < k//2):
  print(n)
elif(k==1):
  print(0)
elif (t < abs(k-t)):
  print(t)
else:
  print(abs(k-t))
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 x == k:
    print(0)
else:
    print(x)
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))
N,K = map(int,input().split())
if N == K:
  print(N)
elif N < K:
  print(N)
elif N > K:
  tmp = N%K
  if K%2 == 0:
    a = K-tmp
    b = tmp
    if a < b:
      print(a)
    elif a > b:
      print(b)   
  else:
    a = K-tmp
    b = tmp
    if a < b:
      print(a)
    elif a > b:
      print(b)
N, K = map(int, input().split(' '))


def main(n, k):
    amari = n % abs(n - k)

    if amari == k:
        return 0
    else:
        return min(amari, n)


print(main)

# main(7,4)
# main(2,6)
# main(1000000000000000000 ,1)
from sys import stdin
import numpy as np

############################################

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
elif N <= K:
    print(N)
else:
    rem = N%K
    print(np.abs(rem - K))
N,K = map(int,input().split())
x = N
min_x = x
i = 0
for i in range(1,N):
    x = abs(x - K)
    i = i + 1
    if x < min_x:
        min_x = x
    if i == N + 1:
        break
print(min_x)
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                print(l)
                exit()
    print(min(l))
n,k=map(int, input().split())
l=[]

i = n - (k * int(n/k))
l.append(abs(i-k))
l.append(abs(l[0]-k))
print(min(l))
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 2:
            if res[i] == res[i-2]:
                print(min(res))
                exit()
    print(min(res))
val = input().split()
N = int(val[0])
K = int(val[1])
min = N
list = [N]
calc = abs(min-K)
while calc not in list:
  list.append(calc)
  if calc < min:
    min = calc
  calc = abs(min-K)
print(min)
print(list)
n,m=map(int,input().split())
ans=1e20
ans=(n % m)
ans=min(abs(ans),abs(n-m))
print(ans)
n,k = map(int, input().split())

while n >= k:
  n = n-k
print(n)
N,K = map(int, input().split())
ans = 0


if (N < K):
    ans = N
elif(N % 2 == 0):
    ans = 0
else:
    N = N % K
    if(N <= abs(N-K)):
        ans = N
    else:
        ans = abs(N-K)



print(ans)
n, k = map(int,input().split())
mod = n % k
if mod == n:
  print(n)
elif mod:
  print(abs(k - mod))
else:
  print(mod)
#n = int(input())
n,k = map(int,input().split())
#a_L = list(map(int,input().split()))

if abs(n-k) > n:
    print(n)
    exit()

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

if n == 0:
    print(0)

print(0)
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
if n > k:
  n = n % k
if n < k:
  if n < (k-n):
    print(n)
  else:
    print(k-n)
N, K = list(map(lambda n: int(n), input().split(" ")))
 
while N <= int(K/2):
  N = min([abs(N-K), N % K])

print(N)
data = input().split()
n = int(data[0])
k = int(data[1])
 
if n > k :
  res = k - n % k
else:
  res = min(abs(n - k), n, k - n % k)

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

print(ans)
num_list = [int(i) for i in input().split()]

min_list = []

if num_list[0] < num_list[1]:
    print(num_list[0])
elif num_list[0] == num_list[1]:
    print(0)
elif num_list[0] % num_list[1] == 0:
    print(0)
else:
    kake_num = num_list[0] // num_list[1]
    check_num = num_list[0] - (num_list[1]*kake_num)
    min_list.append(check_num)
    min_list.append((num_list[1] - check_num))
    print(min(min_list))
N,K = map(int,input().split())
if(N % K < K // 2):
  print(str(N % K))
else:
  print(str(K - (N % K)))
n, k = map(int, input().split())
buff = n

while True:
  buff -= k
  if n <= 0 or buff < n:
    n = abs(n)
    break
  n = buff

print(n)
a,b=map(int,input().split())
ans=[a]
for i in range(10000):
    ans.append(abs(ans[i]-b))
print(0 if b==1 else min(ans))
N,K = list(map(int,input().split()))
print(N%K)
in_ = input()
n, k = map(int, in_.split())

while True:
    n = n % k
    if n == 0:
        r = n
        break
    r = n - k
    if r < 0:
        if n > r:
            r = n
        break
    else:
        n = r
print(abs(r))
#n=int(input())
n,k=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
ans=0
import fractions
if n>=k:
    if n%k==0:
        ans=0
    else:
        ans=fractions.gcd(n,k)
else:
    ans=fractions.gcd(n,k)
print(ans)
N, K = map(int, input().split())
s = int(str(N)[-1])
x = abs(s-K)
l = [x]
for i in range(s+K):
    x -= K
    l.append(abs(x))
print(min(l))
N, K = list(map(int, input().split()))
if K == 1:
    print(0)
elif (N % 2 == 0 and K % 2 == 0):
    if N % K == 0:
        print(0)
    else:
        print(2)
else:
    while N > abs(N - K):
        N = abs(N - K)
    print(N)
N, K = map(int, input().split())

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

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

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

else:
    print(2)
n,k=map(int,input().split());print(n-k*(-((k-n)//k)))
N,K=map(int,input().split())
if N%K==0:
    print(0)
    exit()
kari=int(N/K)
N=N-K*kari
while True:
    if N<abs(N-K):
        print(N)
        exit()
    else:
        N=abs(N-K)
n, k = map(int, input().split())

rot = n//k
x = n

if n == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  x = n - k*rot
  
t = x%k

if n == 0:
  print(k)
else:
  print(min(t, abs(t-k)))
N, K = map(int, input().split())

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

A = N % K

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

if N % K == 0:
    print(0)
else:
    q, r = divmod(N, N - K)
    print(abs(r))
n,k=map(int,input().split())
print(str(min(abs(n-int(n/k)*k),abs(n-(int(n/k)+1)*k))))
N, K = map(int, input().split())


if N == 0:
    print(N)
elif K == 0:
    print(N)
elif 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")
N, K = map(int, input().split())

result = abs(N - K)

if N % K == 0:
    print(0)
elif N < K:
    if N < result:
        print(N)
    else:
        print(result)
elif result % 2 == 1:
    print(1)
else:
    print(2)
import math
N,K=map(int,input().split())
small=math.floor(N/K)
b=abs(N-K*small)
a=abs(b-K)
print(min(a,b))
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()
    ans=n
    dis=abs(ans-k)
    if n>=k:
        print(n%k)
        exit()
    else:
        while ans>dis:
            ans=dis
            dis=abs(ans-k)
    print(ans)



if __name__ == "__main__":
    main()
n,k=map(int,input().split())
if n<k:
    print(n)
elif n==k:
    print(1)
elif n%k==0:
    print(0)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))
N,K = map(int,input().split())
if N >= K:
    if N%K == 0:
        print(0)
        exit()
    else:
        print(0)
        exit()
else:
    A = [N]
    M = N 
    N = abs(N-K)
    A.append(N)
    while N != M:
        N = abs(N-K)
        A.append(N)
    print(min(A))
n, k = map(int, input().split(' '))
if n < k:
    r = n
else:
    if n % k == 0:
        r = 0
    else:
        r = n % k
        r1 = abs(r - k)
        if r > r1:
            r, r1 = r1, r
print(r)
N,K = map(int, input().split())
from fractions import gcd
ans = gcd(N,K)


print(ans)
#print(*ans,sep=' ')
nm = list(map(int,input().rstrip().split()))
n = nm[0]
k = nm[1]
ans  = n
result = 0
for i in range(100):
    temp = ans
    ans = abs(ans - k)
    if ans > temp :
        result = temp
        if result == 0 or result == 1 :
            break
    elif ans < temp :
        result = ans
        if result == 0 or result == 1 :
            break
    else:
        result = temp
        if result == 0 or result == 1 :
            break
print(result)
n,k=map(int, input().split())
num=abs(n-k)
if num==0:
    print(0)
elif num>k or num<=(k//2):
    print(num%k)
else:
    print(k%num)
n,k=map(int,input().split())
if n<k:
    print(n)
else:
    print(min(n%k,k-(n%k)))
from sys import stdin
import numpy as np
############################################

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
if N < K:
    print(N)
    
rem = N%K
print(np.abs(rem - K))
N,K = map(int,input().split())

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    elif(N<K):
        return N
    else:
        N = N-(K*(N//K))   
        if (N-K<=0 and abs(N-K)<K):
            N = abs(N-K)
        return N
print(replace(N,K))
n, k = map(int, input().split())

def solve():
    global n, k
    if n == k or n % k == 0:
        return 0
    if n < k:
        return n
    
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n-k))
        if n == pre:
            break
        pre = n
    return n

print(solve())
N, K = input().split()
N,K = int(N),int(K)
if N>K:
    while N>0:
        N=N-K
        if N<=0:
            print(abs(N))
else:
    print(abs(N))
import sys
N, K = map(int, input().split())
if N == 1 or N == 0 or K == 1 or N == K:
    print(0)
    sys.exit()

if N > K:
    N = N%K
elif N < K:
    print(N)
    sys.exit()

ans = [N]
for _ in range(100):
    ans.append(abs(N-K))
    N = abs(N-K)


print(min(ans))
n, k = [int(x) for x in input().split()]

def replace_abs(x, k):
    return abs(x - k)

if n <= k:
    print(n)
else:
    print(n % abs(n-k))
import sys
n, k = map(int, sys.stdin.buffer.read().split())
print(n % k)
n, k = map(int ,input().split())
if k == 1:
    print(0)
else:
    ab = abs(n - k)
    if ab == (n + k) / 2:
        print(n)
    else:
        print(1)
N,K = map(int, input().split())
ans = 0


if (N < K):
    ans = N
elif(N % K == 0):
    ans = 0
else:
    N = N % K
    if(N <= abs(N-K)):
        ans = N
    else:
        ans = abs(N-K)

print(ans)
N,k = [int(x) for x in input().split()]
if N < k:
  print(N)
else:
  print(min(N%k,k-N%k))
from math import gcd

n, k = map(int, input().split())
if n == 1 or k == 1:
    print(0)
else:
    print(gcd(n, k))
N,K=map(int,input().split())
l=abs(N//K)
p=K*l
for i in range(1,10000):
	if abs(N-(K*(l+i)))<abs(N-p):
		p+=i
print(abs(N-p))
a = list(map(int,input().split()))

N = a[0]
K = a[1]

if K >= 2*N:
    print(N)
elif N >= K:
    print(N%K)
else:
    while 1:
        cul_result1 = abs(N-K)
        cul_result2 = abs(cul_result1-K)
        if cul_result1 < N and cul_result2 >=N:
            print(cul_result1)
            break
        elif cul_result1 < N and cul_result2 < cul_result1:
            N = cul_result1
            continue
#!/usr/bin/env python

t = input().split()
n = int(t[0])
k = int(t[1])

a = 0
if k == 1:
    a = 0
elif n < k:
    a = n
elif n%k == 0:
    a = 0
else:
    a = abs(k-n%k)
print(a)
N,K=map(int, input().split()) 

if N>K:
  y = int(N/K)
  x = N - y*K
else:
  x = N 
#print(x)
cnt = 0
ans = x
while cnt<10:
  x = abs(x-K)
  ans = min(ans, x)
  cnt += 1
ans = max(ans, 0)
print(ans)
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))
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())
n, k = map(int, input().split())
if n <= k:
    print(min(n, k-n))
else:
    print(n % k)
#abc 161c
import math
n,k = map(int, input().split())
i = int(n/k)
ilow = i
iup = i+1
print(min(abs(n-k*iup),n-k*ilow))
N,K=map(int,input().split())
B=int(N/K)
A=N-B*K
while A>K/2:
    A=min(abs(A-K),abs(A+K))
print(A)
n,k = map(int,input().split())
print(min(n%k,(k-(n&k))%k))
a = list(map(int,input().split()))
print(a)
if a[0]<a[1]:
  print(str(min(a[0],a[1]-a[0])))
else:
  r=a[0]%a[1]
  print(str(min(r,a[1]-r)))
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=b
for i in range (1000000):
    x=b-a
    if x==0:
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if y[i]<=y[i+1]:
        f=y[i]
        break

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

def change(n,k):
    if n <= k:
        print(n)
    elif n % k == 0:
        print(0)
    else:
        for i in range(n):
            a = []
            a.append(n)
            n = abs(n-k)
            if a[-1] < n:
                print(a[-1])
                break

change(n,k)
# 初期入力
N,K = (int(x) for x in input().split(" "))

#
zeroika = N//K
N1 =abs(N-K*zeroika) #N1がマイナスになって初めてABS、そこまではKの倍数単調に減る
while N1<N:
    N =N1
    N1 =abs(N-K)
    #print(N1,end=" ")
print(N)
N, K = input().split()
N = int(N)
K = int(K)

k = int(N/K)
print(N - K*k)
N, K = map(int, input().split())

if N < K:
    print(str(N))
elif N%K==0:
    print("0")
else:
    print(str(min(abs(N%K - K), abs(N%K))))
N, K = map(int, input().split())
if N < K:
    print(min(N, abs(N-K)))
elif N == K:
    print(N-K)
else:
    cnt = N//K
    ans = abs(N - K*(cnt+1))
    print(ans)
n,k = (int(i) for i in input().split())
m = n%k
l =abs(n-k)
r = abs(m-k)
if m !=0:
	print(min(l,r))
else:
	print("0")
N , M = (map(int , input().split()))
a = -(-N//M)
if N == M:
    ans = 0
elif a == 1:
    ans = N
else:
    ans = (a*M)-N
print(ans)
N, K = map(int, input().split())

ans = float('inf')
base = N
diff = K
while True:
    d = abs(base - diff)
    mn = min(d, ans)
    if ans <= mn:
        break
    else:
        base = min(K, d)
        ans = mn

print(ans)
n, k = map(int, input().split())
new_n = 0

if n < k :
  new_n = k - n
else:
  new_n = n - k
  
if new_n >= n:
  print(n)
else:
  if n < k and k % n == 0:
    print(0)
  elif n > k and n % k == 0:
    print(0)
  elif n < k and (k - n ) % 2 == 0:
    print(0)
  elif n < k and (k - n) % 2 == 1:
    print(1)
  elif n > k and (n - k ) % 2 == 0:
    print(0)
  elif n > k and (n - k ) % 2 == 1:
    print(1)
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)
N, K = map(int, input().split())
for i in range(100):
  if 0 <= N-K < N:
    N = N-K
  elif 0 < K-N < N:
    N = K-N
print(N)
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

def solve():
    n, k = map(int, input().split())
    if n <= k:
        print(n)
    else:
        print(n%k)
    return 0

if __name__ == "__main__":
    solve()
def resolve():
    n, k = map(int, input().split())
    if n < k:
        print(n)
    else:
        _n = n % k
        if _n == 0:
            print(0)
        else:
            print(abs(_n - k))


resolve()
N , K = map(int,input().split())
if N<K:
    print(N)
elif N ==1 and K ==1:
    print(1)
else:
    n = N%K
    ans = min(n,abs(n-K))
    print(ans)
n, k = map(int, input().split())

if n < k:
    print(n)
    exit()

if n % k == 0:
    print(0)
    exit()

print(abs((n % k) - k))
N, K = map(int, input().split())
r = N%K
if r == 0 or r == 1:
  print(r)
s = abs(r - K)
if s < r:
  print(s)
else:
  print(r)
N , K = map(int, input().split())
N = abs(N - K)
while N > 0:
    N = (N - K)
print(abs(N))
n, k = map(int, input().split())

ans = n
while abs(n - k):
    if abs(n - k) < n:
        ans = abs(n - k)
    else:
        print(ans)
        break
n,k=map(int,input().split())
if k==1 or n%k==0:
    ans=0
else:
    if k%2==0:
        ans=2
    if k%2==1:
        ans=1
print(ans)
n,x=map(int, input().split())
l=[]


n = n - x * int(n/x)
l.append(abs(n-x))
l.append(abs(l[0]-x))
print(min(l))
N,K = map(int,input().split())

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    else:
        flag = 0
        while(abs(N-K)>=0):
            N = abs(N-K*round(N/K))
            if(N == 0 ):
                return 0                
            elif (N-K<0):
                return abs(N-K)
        return N
print(replace(N,K))
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
if s > 100 * j:
    s = s - 100000 * j
while v > s:
    v = s
    s = abs(s - 10000 * j)
while v > s:
    v = s
    s = abs(s - j)

v = s
s = abs(s-j)
if v > s:
  print(s)
else:
  print(v)
import math

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

t0 = math.floor(N / K) - 1
t1 = math.floor(N / K)
t2 = math.ceil(N / K)
t3 = math.ceil(N / K) + 1

res = min(abs(N - t1 * K), abs(N - t2 * K), abs(N - t0 * K), abs(N - t3 * K))

print(res)
n, k= list(map(int, input().split()))
if k>n:
    print(n)
    exit()
elif n%k == 0:
    print("0")
    exit()
amari = n%k
n = amari
while(True):
    new_n = abs(k-n)
    if new_n < n:
        n = new_n
    else :
        break
print(n)
N, K = map(int, input().split())

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    result = N // K

print(abs(result))
N,K=map(int,input().split())
ZET=N
b=100000000000000000
c=N
b1=N
for i in range(1000000000000000000):
  c=abs(b1-K)
  if b<c:
    break
  b1=c
  b=c
print(b1)
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k + 10 ** -4)
    n = n - t * k
    print(min(n, k - n))
n,k =input().split();
n = int(n);
k = int(k);
x = int(n/k);
 
print(n-x*k)
import math

if __name__ == "__main__":
    N, K = map(int, input().split())
    if N == 0:
        ret = 0
        print(int(ret))
        exit(0)
    ret =  N - ((math.floor(N/K))*K)
    if N % K >= K:
        ret = ret - K
    print(int(ret))
N,K=map(int, input().split()) 
min = abs(N-K)
if min%2 ==0:
    print(N)
else: 
    while min > abs(min-K):
        min = abs(min-K)
    print(min)
cl1 = input().split()
cl1 = [int(s) for s in cl1]
x = cl1[0]
k = cl1[1]
if(x < k):
    print(x)
else:
    a = x % k
    b = -(x % k - k)
    print(min(a,b))
N, K = list(map(int, input().split()))

tmp = N % K

if tmp == 0:
    print(0)
elif N <= tmp:
    print(N)
else:
    print(abs(K - tmp))
import numpy as np

def main():
    n, k = map(np.int64, input().split(" "))
    a1 = np.int64(np.floor(n // k))
    a2 = np.int64(np.floor(n // k + 1))
    b1 = abs(n - a1 * k)
    b2 = abs(n - a2 * k)
    #print(n - a1 * k, n - a2 * k)
    ans = np.min([b1, b2])
    print(ans)
    
if __name__ == '__main__':
    main()
N,K=input().split()
N,K=[int(N),int(K)]
 
minimum=10000000000000000000000
while N < minimum:
  N=abs(N-K)
  minimum=N
  
  
print(minimum)
N, K = map(int, input().split())
if(N<K):
    print(N)
elif(K==1):
    print(0)
elif(N>=K):
    d = int(N//K)
    a = N%K
    N = N - (d*K)
    print(abs(N-K))
n, k = map(int, input().split())
lst = []
leastnum = n

if n <= k:
    for i in range(10):
        x = abs(n - k)
        if x <= leastnum:
            leastnum = x
            n = x
        # n = x
        # lst.append(x)
    print(leastnum)

else:
    if n % 2 == 1:
        print(1)
    else:
        print(0)
N,K=map(int,input().split())
S=N%K
T=N//K
if T==0:
    print(N)
else:
    if S==0:
        print(0)
    else:
        if S-K<0:
            print(min(S,K-S))
        else:
            print(S)
n, k = map(int,input().split())

if abs(n%k) == 0:
  ans = 0
else:
  while n > k:
    n = abs(n - (n // k) * k)
  ans = min(abs(n-k),abs(abs(n-k)-n))

print(ans)
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

if n // k != 0:
    m = n // k
    print(min(n % k, m))
else:
    print(n)
n,k=map(int,input().split())
print(n%k,k-n%k)
N,K=map(int,input().split())
ZET=N
b=0
c=N
b1=N
for i in range(1000000000000000000):
  c=abs(b1-K)
  if b>=c:
    break
  b1=c
  b=c
print(c)
n,k = map(int,input().split());
a = [0]*2
i = 0
while True:
    x = n - k;
    if x >= 0:
        n = x
    else:
        n = (-1)*x;
    if i == 0:
        if a[1] == n:
            break;
    else:
        if a[0] == n:
            break;
    i = (i+1) % 2;
    a[i] = n;

print(a[i]);
N, K = map(int, input().split())

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    while N < 0:
        N = N - K
    result = abs(N - K)

print(abs(result))
N, K = map(int, input().split())
ans = int(N)
k_range = N // K
# print(k_range)

N = N - (K*k_range)

if ans > abs(N):
    ans = abs(N)
print(ans)
data=input().split()
a=int(data[0])
b=int(data[1])
a=a%b
print(a)
N,K = map(int,input().split())

if(N == 0):
  print(N)
elif(K == 1):
  print(K)
else:
  mod = N % K
  if(N<=K):
    if(K-mod < mod):
      print(K-mod)
    else:
      print(mod)
  else:
    print(K-mod)
N, K = map(int, input().split())
s = N // K
min(abs(N - s * K), abs(N- (s+1) * K))
N, K = map(int, input().split())
print(min([N, N%K] + (K < N)*[N-N%K]))
# 最大公約数
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)
elif N < K:
    print(N)
else:
    print(gcd(N, K))
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)
n,k = map(int,input().split())
lst = []
lst.append(n)
kaisuu = (n // k) + 1
for i in range(kaisuu):
    n = n - k
    lst.append(n)
print(lst)

list2 = []
kazu = len(lst)
for number in lst:
    if number>= 0:
        list2.append(number)
    elif number < 0:
        mainasu = abs(number)
        list2.append(mainasu)
        
print(list2)
print (min(list2))
from fractions import gcd
n,k = map(int,input().split())

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

	for i in range(100000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif n==1 and k == 0:
	print(1)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))
n, k = map(int, input().split())

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

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

mod = N % K

print(min(mod, abs(mod + K)))
a=list(map(int, input().split()))
n=a[0]
k=a[1]
def gcd(a,b):
    if(b>a):
        a,b=b,a
    i=b
    while i>0:
        if(a%i==0 and b%i==0):
            return i
        i-=1
if n==1 or k==1:
    print(0)
    exit()
else:
    print(gcd(n,k))