peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = map(int,input().split())
a = N % K

if a <= abs(a-K):
    print(a)
else:
    print(abs(a-K))
n, k = map(int, input().split())
m = n % k
ans = min(m, abs(m - k))
print(ans)
def calc(n, k):
    if n == 0:
        return 0
    if n > k:
        a = n%k
        return min(abs(a-k), a)
    # kのほうがnより大きいとき
    if n*2 < k:
        return n
    else:
        # k = k%n+n+n
        return min(abs(n-k), k-abs(n-k))

n, k = (int(each) for each in input().split())
print(calc(n, k))
# coding: utf-8
# Your code here!
import math
from decimal import *
getcontext().prec = 70

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


if N<= K:
    print(min(N,K-N))
else:

    N = N - math.floor(Decimal(N)/Decimal(K))*K 
    print(min(N, K-N))
N, K = map(int, input().split())
def f(x, k):
    d = abs(k-x)
    if d<x:
        return f(d, k)
    else:
        return x
x = N%K
print(f(x, K))
N, K = map(int, input().split())

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

print(ans)
n,k=[int(i) for i in input().split()]
if n <= k/2:
    print(n)
elif n % k == 0:
    print(0)
else:
    t1=n-(n//k*k)
    t2=abs(t1-k)
    print(min(t1,t2))
N, K = list(map(int, input().split()))
if N > K:
    p = (N - 1)//K
    N_p = N - p*K
    N_q  = -(N_p - K)
    ans = min(N_p, N_q)
else:
    ans = min(N, K-N)
print(ans)
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(abs(a%b-b))
N, K = map(int, input().split())

D = N%K
print(min([abs(D-K), D]))
N, K = list(map(int, input().split()))
print(min(abs(N - (K*(N//K+1))), abs(N - (K*(N//K)))))
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

tmp = n//k

a = abs(n-k*tmp)
b = abs(a-k)

print(min(a,b))
#
import sys
import math
import numpy as np
import itertools


# いくつか入力
n,k = (int(i) for i in input().split())

a = n % k
c = k - a

print(min(a,c))
n,k=map(int,input().split())
n=n%k



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

m_1 = n % k
m_2 = k - m_1
print(min(m_1, m_2))
import sys
n, k = map(int, input().split())

n = abs(n - k)

if k == n:
    print(0)
    sys.exit()
elif k > n:
    print(min(n, k-n))
    sys.exit()
else:
    while n > k:
        n %= k
    print(min(n%k, abs(n-k)))
n,k=map(int,input().split())
print(min(n%k,k-n%k))
N, K = map(int,input().split())
while True:
    if N%K == 0:
        print(0)
        break
    else:
        N=N%K
        N = min(N,abs(N-K))
        print(N)
        break
N,K=map(int,input().split())
if abs(N-(N//K)*K)<abs((N-(N//K)*K)-K):
    print(N-(N//K)*K)
else:
    print(abs((N-(N//K)*K)-K))
N, K = map(int, input().split())

n = N//K
m1 = N-K*n
m2 = abs(N-K*(n+1))
ans = min(m1, m2)
print(ans)
N,K=map(int,input().split())

t=N%K
ans=min(t,K-t)
print(ans)
N, K = map(int, input().split())
print(min(N % K,abs(N % K-K)))
n,m = map(int,input().split())
ans = n % m
if m - ans < ans:
    ans = m - ans
print(ans)
N,K=map(int,input().split())

a = int(N % K)
b = int(K - a)

print(min(a,b))
N,K = map(int,input().split())

print (min(N % K , K - N % K))
#

import sys
input=sys.stdin.readline

def main():
    N,K=map(int,input().split())
    print(min(N%K, abs(K-N%K)))
    
    
    
if __name__=="__main__":
    main()
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(m-n,l[0])
print(ans)
N, K = map(int, input().split())
mod = N%K
gyaku = K-mod
print(min(mod, gyaku))
from sys import stdin, stdout
import math
import bisect
import queue

n, k = map(int, stdin.readline().strip().split())

times = n // k

ans = n - k * times
ans = min(ans,abs(n - (times + 1) * k))

stdout.writelines(str(ans))
n,k = map(int,input().split())
print(min(n%k,k-n%k))
n,k=map(int,input().split())
if k==1:
  print(0)
else:
  a=n%k
  b=abs(a-k)
  print(min(a,b))
import sys
from collections import defaultdict
n,k=map(int,sys.stdin.readline().split())
if n>k:
    n=n%k
ans=min(n,abs(n-k))
print(ans)
n, k = map(int, input().split())

print(min(abs(n % k), abs((n % k) - k)))
n,k=map(int,input().split())
print(min(n%k,abs((-1*n)%k)))
N,K = map(int,input().split())

print(min(N%K,K-N%K))
N,K=map(int, input().split())
a=N%K
print(min(a,K-a))
# -*- coding: utf-8 -*-
n, k = map(int, input().split())

n_af = n % k
if k - n_af <= n_af:
    n_af = k - n_af

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

a = n
if n<=k:
    a = min([n,k-n])
else:
    a = min([n%k,k-n%k])
print(a)
n,k = map(int,input().split())
ans = n
ans = min(ans,n%k)
ans = min(ans,abs(k-n%k))
print(ans)
x, k = map(int, input().split())
print(min(x%k, k - x%k))
N, K = map(int, input().split())
x = N % K
ans = min(abs(x-K), x)
print(ans)
N, K = map(int, input().split())

def substract(min):
    if N % K == 0:
        return 0
    tmp = N % K
    if abs(tmp - K) < tmp:
        return abs(tmp-K)
    return tmp
    #while(1):
    #    tmp = abs(tmp - K)
    #    if tmp > min:
    #        break
    #    min = tmp
    #return min

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)
n,k=map(int,input().split())
n=n%k
flag=n
li=[n]
for i in range(10**10):
  n=abs(n-k)
  li.append(n)
  if n==flag:
    break
  
print(min(li))
import sys
input=sys.stdin.readline
#N = int(input())
#S = input()[:-1]
N,K = map(int,input().split())
#S,T = map(str,input().split())
#A = list(map(int,input().split()))
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
a = N % K
b = min(a,K-a)
print(b)
N, K = map(int, input().split())

N %= K
#print(N, abs(K-N))
print(min(N, abs(K-N)))
N,K=(int(x) for x in input().split())
x=N%K
print(min([x,abs(K-x)]))
N, K = map(int, input().split())
m = N % K
if m > 0:
    _m = abs(m - K)
    if _m < m:
        m = _m
print(m)
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

n,k=sep()
if n>=k:
    n=(n%k)
print(min(n,abs(k-n)))
# coding: utf-8
# Your code here!

N, K = map(int, input().split())
rem = N % K
mns = abs(rem - K)
print(min(rem, mns))
N, K = map(int, input().split())
n_1 = N - (K * (N // K))
n_2 = abs(n_1 - K)
print(min(n_1, n_2))
N, K = map(int, input().split())
print(min([N % K, abs((N % K) - K)]))
import numpy as np
N, K = list(map(int,input().split()))
tmp = np.zeros(2)
tmp[0] = N % K
tmp[1] = -N % K
print(int(tmp.min()))
a = list(map(int,input().split()))

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

if K >= 2*N:
    print(N)
elif K > N :
    print(abs(N-K))
elif N >= K:
    res = N%K
    min = res
    cul_result1 = abs(res-K)
    if cul_result1 < min:
        min = cul_result1
    cul_result2 = abs(cul_result1-K)
    if cul_result2 < min:
        min = cul_result2
    print(min)
n, k = map(int, input().split())

n1=n-(k*(n//k))

n2=abs(n1-k)

if n1<n2:
    print(n1)
else:
    print(n2)
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
if n > k:
  n = n % k
if n < (k-n):
    print(n)
else:
    print(k-n)
N,K = map(int,input().split())

n = N % K

ans = min(n,K-n)

print(ans)
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0

n,k = map(int,input().split())
n = n%k
if abs(n-k) >= n:
    print(n)
else:
    print(abs(n-k))
N, K = map(int, input().split())
x = N % K
print(min(x, abs(x-K)))
N, K = map(int, input().split())
if K > N :
  print(min(abs(N-K),N))
else:
  first = N // K 
  first_1 = N % K 
  print(min(first_1, K - first_1))
n, k = map(int, input().split())
if n==0 or k==1:
    print(0)
    exit()
elif n<k:
    print(min(n, abs(n-k)))
    exit()
elif n % k == 0:
    print(0)
    exit()
else:
    mx = n%k
print(min(abs(mx-k), mx))
#create date: 2020-04-04 21:11

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    print(min(n%k, k-(n%k)))

if __name__ == "__main__":
    main()
N,K = input().split()
N = int(N)
K = int(K)

import numpy as np
N = N % K
past = N

while(True):
    N = np.abs(N - K)
    if N >= past:
        break;
    past = N
print(past)
[N, K] = [int(i) for i in input().split()]
print(min(N%K, -N%K))
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

a = N % K
b = (-N) % K

ans = min(a, b)

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

print(min(n % k, abs(n % k - k)))
n, k = map(int, input().split())


if n % k > k/2:
    print(k-(n % k))
else:
    print(n % k)
N,K=map(int,input().split())

print(min(N%K,abs(N%K-K)))
n, k = list(map(int,input().split()))

if n <= k//2:
  print(n)
elif n < k:
  print(k-n)
elif n == k:
  print(0)
else:
  plus = n-n//k*k
  minus = abs(n-n//k*k-k)
  print(min(plus,minus))
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
#from math import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

def input():
    return sys.stdin.readline()[:-1]

def main():
    N, K = map(int, input().split())
    tmp = N % K
    print(min(tmp, abs(tmp-K)))

if __name__ == '__main__':
    main()
n,k = map(int, input().split())
print(min(n%k,k-(n%k)))
N,K=map(int,input().split())
ans=N%K
tmp=abs(ans-K)
if(tmp<ans):
  ans=tmp
print(ans)
n,k=map(int,input().split())
n=n%k
print(min(n,k-n))
(n,k)=map(int,input().split())
a=n%k
print(a if a<(k-a) else (k-a))
N,K = map(int,input().split())
X = N//K
ans1 = N-K*X
ans2 = abs(N-K*(X+1))
print(min(ans1,ans2))
import math
import sys
N,K=map(int,input().split())

if N%K > abs(N%K-K):
    print(abs(N%K-K))
else:
    print(N%K)
# Problem C - Replacing Integer

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

# initialization
compare_1 = N % K
compare_2 = abs(compare_1 - K)
ans = min(compare_1, compare_2)

# output
print(ans)
N, K = map(int, input().split())

ans = N - (K * (N // K))

if ans > abs(ans - K):
    print(abs(ans - K))
else:
    print(ans)
#input
N, K = map(int,input().split())

#main
t = N % K
ans = min(t, K - t)

#output
print(ans)
import math

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

n = n % k

if(k-n >= n):
    print(n)
else:
    print(k-n)
NK = input().split()
N = int(NK[0])
K = int(NK[1])

for i in range(2):
  if N >= K:
    N = N % K
  
  else:
    if K < N*2:
      N = K % N
    
print(N)
n, k = [int(x) for x in input().split()]

t = n % k
print(min(t, (k-t)))
n, k = map(int, input().split())
tmp = n%k
if(tmp>k/2):
    print(k-tmp)
else:
    print(tmp)
N, K = map(int, input().split())
N %= K

print(min(N, K - N))
n,k=map(int,input().split())
if n>k:
    n=n-k*(n//k)
if n<=k-n:
    print(n)
else:
    print(k-n)
inputnum = input().split()
N = int(inputnum[0])
K = int(inputnum[1])

Surplus = N % K
K_Surplus = K - Surplus

if Surplus <= K_Surplus:
  print(Surplus)
else:
  print(K_Surplus)
n, k = map(int, input().split())

print(min(n % k, k - (n % k)))
import sys
line = sys.stdin.readlines()[0]
n, k = [int(s) for s in line.split(" ")]
t = n // k
a1 = abs(n - (k * t))
a2 = abs(n - k * (t + 1))
answer = min(a1, a2)
print(answer)
n,k = map(int,input().split())
print(min(n%k,abs((n%k)-k)))
import sys
n,k = [int(_) for _ in input().split()]

amari = n % k
if amari == 0 :
  print("0")
  sys.exit()

base = amari
while base > abs(base - k):
  base = abs(base - k)

print(base)
def main():
    n,k = map(int,input().split(' '))
    n = n % k
    key = 0
    data = []
    while(key == 0):
        check = abs(n - k)
        for per in data:
            if(check == per):
                key = 1
        if(key == 0):
            data.append(check)
            n = check
        else:
            key = 1
    print(min(data))

if __name__ == '__main__':
    main()
N,K=map(int,input().split())
print(min(abs(N%K-K),N%K))
N,K=map(int,input().split())
r=N%K
print(min(r,abs(r-K)))
N, K = map(int, input().split())
N = N % K

while True:
    n = abs(N - K)
    if n >= N:
        ans = N
        break
    else:
        N = n

print(ans)
n,k = map(int,input().split())
if n <= k:
    print(min(n,k-n))
elif n % k == 0:
    print(0)
else:
    k = k * (n//k+1)
    print(k % n)
N, K = list(map(int, input().split()))
print(min(abs(K-N%K), abs(N%K), N%K))
N, K = map(int, input().split())

if N%K != 0:
    print(min(N%K, K-N%K))
else:
    print(0)
def MI():return map(int,input().split())
def LI():return list(MI())

N,K=MI()

ans=N%K
ans2=(-N)%K
print(min(ans,ans2))
N,K = map(int,input().split())
print(min(N%K,abs(K-N%K)))
import sys
input = sys.stdin.readline

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

a = n
b = k

if a > b:

    a -= (a // b) * b

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

else:
    print(min(a, abs(a - b)))
def main():
    N,K = map(int,input().split())
    a = (N % K)
    b = abs(a - K)
    print(min(a,b))

if __name__ == '__main__':
    main()
[n, k] = [int(i) for i in input().split()]
min = n%k
tmp = -1 * (min - k)
if min <= tmp:
    print(min)
else:
    print(tmp)
n,k = map(int,input().split())

ans = n % k
if ans > abs(ans - k):
    print(abs(ans - k))
else:
    print(ans)
n,k=map(int,input().split())
a=set()
for i in range(1,100000):
    n=n%k
    a.add(n)
    n=abs(n-k)
a=list(a)   
a.sort()
print(a[0])
N, K = [int(i) for i in input().split()]

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

amari = N % K
absplus = abs(amari - K)


if N <= (K // 2):
    print(N)
elif amari >= absplus:
    print(absplus)
else:
    print(amari)
n,k= map(int,input().split())

a=n%k
b=abs(k-a)
print(min(a,b))
n,k=map(int,input().split())
a=n%k
b=k-a
if a>=b:
  print(b)
else:
  print(a)
n, k = map(int, input().split())
m = n%k
print(min(m,k-m))
N, K = [int(t) for t in input().split(" ")]

seen = set()

while N not in seen:
    seen.add(N)

    if N//K > 0:
        N = N%K
    else:
        N = abs(N-K)

print(min(seen))
n,m = map(int, input().split())
print(min(n%m,m-n%m))
x, k = map(int, input().split())

a = x % k
b = abs(a - k)

if a > b:
    print(b)
else:
    print(a)
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())

    if N == K:
        print(0)
    else:
        amari = N % K
        if amari == 0:
            print(0)
        elif abs(amari - K) > amari:
            print(amari)
        else:
            print(abs(amari - K))

if __name__ == "__main__":
    main()
n,k = map(int,input().split())
a = n%k
b = abs(a-k)
print(min(a,b,n))
n, k = map(int, input().split())

ans = min(n % k, abs((n % k) - k))
print(ans)
n, k = map(int, input().split())

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

if x >= k/2:
    print(abs(x-k))
else:
    print(x)
import numpy as np
n_k = [int(i) for i in input().split()]
n = n_k[0]
k = n_k[1]
r = n % k
r_1 = abs(r - k)
min = np.array([r, r_1]).min()
print(min)
n, k = map(int, input().split())
t = n // k
ans = min(n - k * t, -(n - k * (t + 1)))
print(ans)
N,K=map(int,input().split())
N=N%K
ans=min(N,abs(N-K))
print(ans)
n, k = map(int, input().split())
w = n // k
q = -(-n // k)
print(min(abs(n - (w * k)), abs(n - (q * k))))
n,k = map(int,input().split())
mod = n%k
res = abs(mod-k)
print(min(mod,n,res))
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))
N, K = map(int,input().split())
r = N%K
a = min(r, K-r)
print(a)
n,k=list(map(int,input().split()))
if(n%k==0):print(0)
else:print(min(abs(n%k-k),n%k))
N, K = input().split()
N = int(N)
K = int(K)

middle = N % K
answer = middle
while True:
  if abs(answer-K) > N:
    answer = N
    break
  if abs(answer - K) < answer:
    answer = abs(answer-K)
  else:
    break
    
print(str(answer))
class atcorder:
    
    x,K=map(int,input().split())
    
    if x>K:
        ans=x%K
    else:
        ans=x
        
    print(min(ans,abs(ans-K)))
a,b=map(int,input().split())
c=a%b
d=b-c
print(min(c,d))
n,k = map(int,input().split())
a = n % k
b = abs(a-k)

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

if n > k:
  n = abs(n%k)

while True:
  n_ = abs(n-k)
  if n_ >= n:
    print(n)
    break
  else:
    n = n_
n,k = map(int, input().split())
print(min(n%k,abs(n%k - k)))
N,K=map(int, input().split()) 
y = int(N//K)
x = N - y*K
ans = min(x, abs(x-K))
print(ans)
# coding: utf-8
# Your code here!

import sys

sys.setrecursionlimit(10**9)

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

mod = N % K

if mod==0:
    print(0)
else:
    print(min(abs(mod-K), mod))
N,K = map(int,input().split())
if N > K:
    a = N % K
    ans = min(a,K-a)
    print(ans)
elif N < K:
    ans = min(N,K-N)
    print(ans)
elif N == K:
    print(0)
N,K = map(int,input().split())
if K == 1:
  print(0)
else:
  a = N%K
  b = abs(N%K - K)
  ans = min(a,b)
  print(ans)
N,K = map(int,input().split())
l = N//K
a = 0
b = 0

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

m = n % k

print(min(m, abs(m-k)))
n,k=map(int, input().split())

print(min(abs(n%k),abs(n%k-k)))
n,k = list(map(int,input().split()))

a = n%k
b = n%k - k
print(min(a,abs(b)))
N, K = map(int, input().split())
print(min(N%K, abs(N%K - K)))
n, k = (int(i) for i in input().split())
if k > n:
    print(min(n, abs(n-k)))
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = (k * (n//k+1)) - n
    print(min(p, q))
n, k = map(int, input().split())
n = n%k
while abs(n-k)<=n:
    n = abs(n-k)

print(n)
N, K = map(int, input().split())
r = N % K
ans = min(r, K-r)
print(ans)
n,k = map(int, input().split())
r = n % k
a = k -r
if a >= r:
	print(r)
else:
	print(a)
x = list(map(int,input().split(" ")))
n = x[0]
k = x[1]
import math
l = abs(n % k)
# print(l)

if l > k//2:
    print(k-l)
else:
    print(l)
N, K = map(int, input().split())

print(min(N % K, abs(N % K - K)))
def main():
    N, K = map(int, input().split())
    if abs(N-((N // K)*K)) < abs(N-(((N // K)+1)*K)):
        print(abs(N-((N // K)*K)))
    else:
        print(abs(N-(((N // K)+1)*K)))
    return 0


if __name__ == '__main__':
    main()
# -*- coding: utf-8 -*-

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

if K == 1:
    N = 0

else:
    if N // K > 0:
        i = N//K
        N = N - (K * i)
    while abs(N-K)<N:
        N = abs(N-K)
print(N)
# N = int(input())
N, K = map(int, input().split())
# S = input()
# X1_list = [int(input()) for i in range(N)]
# X2_list = [list(map(int,input().split())) for i in range(N)]
result = N % K
print(min(result,abs(result-K)))
N,K = map(int,input().split())
S = N%K
if abs(S-K)<S:
    print(abs(S-K))
else:
    print(S)
#!/usr/bin/env python3

(N, K) = map(int, input().split())
r = N % K
print(min(r, K - r))
N, K = map(int, input().split())

if N%K == 0:
    print("0")
elif abs(N%K- K)<N:
    print(abs(N%K- K))
else:
     print(N)
n,k = map(int,input().split())
n_waru = n//k
n_waru_plus = n_waru+1
print(min(abs(n-(k*n_waru)),abs(n-(k*n_waru_plus))))
N,K=list(map(int,input().split()))
a=N%K
if abs(a-K)<a:
  a=abs(a-K)
print(a)
N,K=map(int,input().split())
a=N//K
if a==0:
    x=N
    y=abs(N-K)
    if x>y:
        print(y)
    else:
        print(x)
elif N%K!=0:
    x=N-a*K
    y=abs(N-(a+1)*K)
    if x>y:
        print(y)
    else:
        print(x)
else:
    print(0)
N, K = map(int, input().split())
print(min(N%K, K-N%K))
def main():
    N, K = map(int, input().split())
    a = N % K
    b = K - a
    print(min(a, b))

if __name__ == '__main__':
    main()
n, k = map(int, input().split())
a = n%k
print(min(a, abs(a-k)))
N, K = map(int, input().split())
t = N%K

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

N = N % K
N = min(N, K - N)
print(N)
x, k = map(int,input().split())

r = x%k
ans = min(abs(r-k),r)
print(ans)
N,K = map(int,input().split())

if(N == 0):
  print(N)
elif(K == 1):
  print(0)
else:
  mod = N % K
  if(N<=K):
    if(K-mod < mod):
      print(K-mod)
    else:
      print(mod)
  else:
    print(K-mod)
def N():
    return int(input())
def L():
    return list(map(int,input().split()))
def NL(n):
    return [list(map(int,input().split())) for _ in range(n)]
import math

n,k = L()
print(min((-n)%k,n%k))
n,k = map(int,input().split())
a = n%k
b = abs((n%k)-k)
print(min(a,b))
n,k = map(int,input().split())
amari = n % k
if amari == 0:
         print(0)
         exit()
else:
         print(min(amari,abs(amari - k)))
N,K=map(int,input().split())
ans=min(N%K,K-(N%K))
print(ans)
NK_list = input().split(" ")
N = int(NK_list[0])
K = int(NK_list[1])

remainder = N%K
other_rem = K - remainder
if remainder >other_rem:
    print(other_rem)
else:
    print(remainder)
import math
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
n,k = rMap()
print(min(n%k, abs(n%k-k)))
n, k = map(int,input().split())
a = n % k
b = abs(a - k)
if a > b:
    print(b)
else:
    print(a)
N, K = map(int, input().split())
print(min(N % K, K - (N % K)))
N,K = map(int,input().split())

if N%K >= abs(N%K-K):
  print(abs(N%K-K))
else:
  print(N%K)
n, k = map(int,input().split())

flag = "No"
if k == 1:
  n = 0
else:
  n = n % k

for i in range(1, 1000000000000000000):
  if abs(n - k) > n:
    print(n)
    flag = "Yes"
    break
  else:
    n = abs(n - k)
if flag == "No":
  print(n)
N,K = map(int,input().split())

if N < K and abs(N-K) > N:
  print(N)
else:
  n = N % K
  print(min(n,abs(n-K)))
import sys
input = sys.stdin.readline
N, K = (int(i) for i in input().split())
#B = [int(x) for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]

nc = N
for i in range(nc):
    if N < K:
        break
    else:
        N = N-(N//K)*K

for i in range(nc):
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        break



print(N)
n, k = input().split(' ')
n = int(n)
k = int(k)

if k == 1 or n == 0:
  print(0)
elif n < k :
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n % k
  r2 = r1 - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
N, K = map(int, input().split())

if N%K > K/2:
	print((K-(N%K))%K)	
else:
	print(N%K)
def main():
    n, k = map(int,input().split())
    if n<k:
        amari = n
        absu = abs(n-k)
    else:
        amari = n%k
        absu = abs(amari-k)
    if amari<=absu:
        print(amari)
    else:
        print(absu)

if __name__ == '__main__':
    main()
n,k=map(int,input().split())
if n>k:
  d=n%k
  print(min(d,k-d))

else:
  print(min(n,k-n))
n,k = map(int,input().split())
if k >= n:
    print(min(n,abs(n-k)))
else :
    x = n//k
    n -= k*x
    print(min(n,abs(n-k)))
N, K = map(int, input().split())

s1 = N % K
s2 = K - s1

print(min(s1, s2))
N,K = [int(i) for i in input().split()]

positive = N % K
negative = K - positive
answer = min(positive, negative)
print(answer)
n, k = [int(x) for x in input().split()]
m = n % k
mt = abs(m - k)
if (mt < m):
    m = mt
print(m)
N, K = map(int, input().split())
m = N % K
print(min(m, K - m))
n,k = map(int,input().split())

if n >= k:
    while(n >= k):
        n = n % k
if n > k/2:
    while(n > k/2):
        n = k-n
if n <= k/2:
    print(n)
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    print(min(N%K, abs(N%K - K)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()
n,m=map(int,input().split())

c=n%m

if c<abs(c-m):
  print(c)
else:
  print(abs(c-m))
# C - Replacing Integer

n, k = map(int, input().split())
a = n % k
print(min(a, abs(a-k)))
n,k = map(int, input().split())
num=n//k
num*=k
num=n-num
print(min(num,abs(num-k)))