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

Solution (Python):
N, K = map(int, input().split())
if N % K == 0:
    print(0)

else:
    num = N//K
    delta = abs(N-num*K)
    if delta > abs(delta-K):
        print(abs(delta-K))
    else:
        print(delta)

Solution (Python):
n, k = map(int, input().split())
p = n%k
print(min(p, k-p))

Solution (Python):







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

if __name__ == "__main__":
    main()

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

if n%k > k-n%k:
  print(k-n%k)
if n%k <= k-n%k:
  print(n%k)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = map(int,input().split())
    print(min(n%k,-n%k))
    return


if __name__ == "__main__":
    solve()

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

print(min([N%K, abs(K-N%K)]))

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

n=n%k

print(min(n,k-n))

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

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

s = N % K
t = K - s

number = [s,t]
print(min(number))

Solution (Python):
N,K=map(int,input().split())
A=[N]
if N%K==0:
    print(0)
elif N<K:
    print(min(abs(N-K),N))
else:
    a=N//K
    x=N-K*a
    print(min(x,abs(K-x)))

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

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

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

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

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

ans = N

tmp = N%K

if tmp > abs(tmp-K):
    print(abs(tmp-K))
else:
    print(tmp)

Solution (Python):
def abs(a):
    if a < 0:
        return -a
    return a

def main():
    n, k = map(int, input().split())
    n = n % k

    if n < k:
        if n > abs(n-k): 
            print(abs(n-k))
        else:
            print(n)
    else:
        print(n)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

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

main()

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

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

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

a = N % K

if a <= abs(a-K):
    print(a)
else:
    print(abs(a-K))

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

Solution (Python):
import math

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

n = n % k

if(k-n >= n):
    print(n)
else:
    print(k-n)

Solution (Python):
x,K=map(int,input().split())

def sa(i,j):
    if i-j>=0:
        return i-j
    else:
        return -(i-j)

    
    
A=x%K

if A<sa(A,K):
    print(A)
else:
    print(sa(A,K))

Solution (Python):
x,K=map(int,input().split())

def d(i,j):
    if i>=j:
        i=i-j
        return i-j
    if i<j:
        i=j-i
        return j-i
    else:
        return "aho"

def sa(i,j):
    if i-j>=0:
        return i-j
    else:
        return -(i-j)

    
    
A=x%K

if A<sa(A,K):
    print(A)
else:
    print(sa(A,K))

Solution (Python):
n,k = [int(i) for i in input().split()]
print(min(n%k,k-n%k))

Solution (Python):

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


flag=True
c=N
if N%K==0:
    print(0)
    exit()
else:
    l=N//K
    N-=l*K
    while flag==True:

        N=abs(N-K)
        if c <= N:
            flag=False
        else:
            c=N
print(c)

Solution (Python):
n, k = map(int, input().split()) 
rem = n%k
rem_minus_abs = abs(rem - k)
print(min(rem,rem_minus_abs))

Solution (Python):
n,k=map(int,input().split())
r=n%k
s=min([r,k-r])
print(s)

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

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

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

s1 = N % K
s2 = K - s1

print(min(s1, s2))

Solution (Python):
n, k = map(int, input().split())
x = n % k
print(min(x, k - x))

Solution (Python):
N,K = map(int,input().split())
if(N % K == 0):
  print("0")
  exit()
if(N % K < K // 2):
  print(str(N % K))
else:
  print(str(K - (N % K)))

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

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

print(min(n%k,k-n%k))

Solution (Python):
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

else:
	n=n%k
	while n>k//2:
		n=abs(n-k)
	
	print(n)

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

Solution (Python):
N, K = map(int,input().split())
Y = N//K
print(min([N-Y*K,Y*K+K-N]))

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

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

a = n % k
print(min(abs(a - k), a))

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

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

n2=abs(n1-k)

if n1<n2:
    print(n1)
else:
    print(n2)

Solution (Python):
n,k = map(int,input().split())
s = n % k
list = [s]
for _ in range(s):
  if abs(s-k) in list:
    break
  elif abs(s-k)<s:
    list.append(abs(s-k))
print(min(list))

Solution (Python):
n,k=map(int, input().split());print(min(n%k,-n%k))

Solution (Python):

N, K = [int(i) for i in input().split()]
print(min(abs(N % K), abs(N % K - K), N))

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

Solution (Python):

N,K = map(int,input().split())
S,A = divmod(N,K)
print(min(A,abs(A-K)))

Solution (Python):
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
 
print(min(n % k, abs(n % k - k)))

Solution (Python):
N,K = map(int, input().split())
ans = 0



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

print(ans)

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

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

a = n % k

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

Solution (Python):
n, k = map(int, input().split())
rem = n % k
print(min(rem, k-rem))

Solution (Python):
def resolve():
    n, k = map(int, input().split())
    print(min(n % k, abs(n % k - k)))

resolve()

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

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

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

print(min(n%k,k-n%k))

Solution (Python):
N,K = map(int,input().split())
x=0
x=N%K
y=abs(x-K)
print(min(x,y))

Solution (Python):


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

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, k = wi()

t = n % k
if t > k / 2:
    print(k - t)
else:
    print(t)

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

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

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

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

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

Solution (Python):
n, k = map(int, input().split())
print(min(n%k, k-n%k))

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

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

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


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


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n, k = wi()
    a = n % k
    b = abs(a - k)
    print(min(a, b))


if __name__ == '__main__':
    main()

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

m = n%k

print(int(min(m,k-m)))

Solution (Python):
n,k = list(map(int, input().split()))
print(min([n%k,k-n%k]))

Solution (Python):

"""
Created on Wed May  6 02:04:30 2020

@author: shinba
"""

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

N -= (N//K)*K

n= N-K

if abs(n) >= abs(N):
    print(abs(N))
else:
    print(abs(n))

Solution (Python):
N,K = map(int,input().split())
s = N//K
ans = min(N-K*s,abs(N-K*(s+1)))
print(ans)

Solution (Python):
inp_ls = input().split(" ")
n,k = [int(e) for e in inp_ls[:2]]

pl_min = n - (n//k)*k
m_min = abs(pl_min - k)

out_n = m_min if m_min < pl_min else pl_min
print(out_n)

Solution (Python):
def input_num():
	ls = input().strip().split(" ")
	return [int(e) for e in ls]

n,k = input_num()


n = n%k
print(min(n,abs(n-k)))

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

x = n%k
x = min(x,k-x)

print(x)

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

a=N
while True:
    if a-10**16*K>=0:
        a=a-10**16*K
    elif a-10**14*K>=0:
        a=a-10**14*K
    elif a-10**12*K>=0:
        a=a-10**12*K
    elif a-10**10*K>=0:
        a=a-10**10*K
    elif a-10**8*K>=0:
        a=a-10**8*K
    elif a-10**6*K>=0:
        a=a-10**6*K
    elif a-10**4*K>=0:
        a=a-10**4*K
    elif a-10**2*K>=0:
        a=a-10**2*K
    elif a-K>=0:
        a=a-K
    elif K-a>=a:
        a=a
        break
    else:
        a=K-a
print(a)

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

mm=n%k
mmm=k-mm

if mmm >= mm:
    ans = mm
else:
    ans = mmm

print(str(ans))

Solution (Python):
N, K = map(int, input().split())
a = (N//K) * K
N -= a
while N > K//2:
  N = abs(N-K)
print(N)

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

a=n%k
b=abs(k-a)
print(min(a,b))

Solution (Python):
n,k = map(int,input().split())
a = n%k
b = abs((n%k)-k)
print(min(a,b))

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

ans = 0
if n<k:
  ans = min(n,abs(n-k))
else:
  x = n%k
  y = abs(x-k)
  ans = min(x,y)
  
print(ans)

Solution (Python):
n, k = list(map(int, input().rstrip().split()))
n = n % k
n2 = abs(n - k)
print(n if n <= n2 else n2)

Solution (Python):
lis1=list(map(int,list(input('').split(' '))))
n=lis1[0]
m=lis1[1]
if n%m==0 :
  print(0)
else :
  print(min(abs(m-n%m),abs(n%m)))

Solution (Python):
n, k = map(int, input().split())
print(min([abs(n%k), abs(n%k-k)]))

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

Solution (Python):
N, K = list(map(int, input().split()))
aoki = N % K

while aoki > abs(aoki-K):
    aoki = abs(aoki - K)
print(aoki)

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

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

if N > K:
    x = N%K
else:
    x = K-N

while  N > x :
    N = x

    if N > K:
        x = N-K

    else:
        x = K-N

print(N)

Solution (Python):
N, K = [int(_) for _ in input().split()]
N = N % K
if N * 2 > K:
    print(K - N)
else:
    print(N)

Solution (Python):
N, K = map(int, input().split())
n = N - N // K * K
print(min(n, K - n))

Solution (Python):
def main():
    N, K = [int(i) for i in input().split()]
    N = N - (K * (N // K))
    if N == 0:
        print(0)
        return

    prevs = set()
    while N not in prevs:
        prevs.add(N)
        N = abs(N - K)

    print(min(prevs))

main()

Solution (Python):
n,k =map(int, input().split())
m = n % k
print(min(m, k - m))

Solution (Python):

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

print(min(amari,abs(amari-K)))

Solution (Python):



import numpy as np

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

n_mod_k = n % k 
print(min(n_mod_k, np.abs(k-n_mod_k)))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    v = set()
    while n not in v:
        v.add(n)
        n = min(abs(n-k), n % k)

    return min(v)


print(main())

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

if n<k:
 x=min(n,k-n)
else:
 x=min(n%k,k-n%k)
print(x)

Solution (Python):
n,k = map(int,input().split())
if abs(k-n)>n:
  print(n)
elif k==1 or k==n or n==0:
  print(0)
elif n<k:
  print(k%n)
else:
  n = k+ n%k
  n1 = n%k
  while n1<n:
    n=n1
    n1 = abs(k-n1)
  print(n)

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

before = abs(N - int(N / K) * K)
before2 = N % K
after = abs(before2 - K)

if before2 > after:
    print(after)
else:
    print(before2)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n, k = MI()
    ans = n % k

    ans = min(ans, abs(ans - k))
    print(ans)


if __name__ == '__main__':
    solve()

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


d = n % k

print(min(d, abs(d-k)))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  
def Is(): return map(int, input().split())  
def LI(): return list(map(int, input().split()))  
def TI(): return tuple(map(int, input().split()))  
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  
def LS(): return list(input())  
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

N, K = Is()

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

Solution (Python):
N, K = map(int, input().split())
if N == K:
    print(0)
elif N > K:
    print(min(N%K, K-N%K))
else:
    print(min(N, K-N))

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

Solution (Python):
import math

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

print(min([t,k-t]))

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]

a = n%k
ans = min(a, k-a)

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]

l = [n]

n %= k
l.append(n)

while True:
    diff = abs(l[-1]-k)
    if diff not in l:
        l.append(diff)
    else:
        break

print(min(l))

Solution (Python):
n, k = map(int, input().split())
x = n
x = n % k

if x >= k/2:
    print(abs(x-k))
else:
    print(x)

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

Solution (Python):
n,k = map(int,input().split()) 
print(min(n%k,k-n%k))

Solution (Python):
n,k=map(int,input().split())
if n>=k:
  n=n%k

print(min(n,k-n))

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

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

amari = N%K

ans = float('inf')
for i in range(10):
    ans = min(ans, amari)
    amari = abs(amari-K)
print(ans)

Solution (Python):

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

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

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,(-n)%k))

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

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

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

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

a = n%k
b = k - a

print(min(a,b))

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

n = n % k

n = abs(n - k)

print(min(n, abs(n-k)))

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

n = n % k
ans = min(n, k-n)

print(ans)

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

Solution (Python):
N,K=map(int,input().split())
ans1=abs(N-(N//K+1)*K)
ans2=abs(N-(N//K)*K)
if ans1<=ans2:
    print(ans1)
else:
    print(ans2)

Solution (Python):
n,k=map(int,input().split())
print(min(n%k, k-n%k))

Solution (Python):
n,k=map(int,input().split())
n%=k
N=abs(n-k)
if N<n:
  n=N
print(n)

Solution (Python):
n,k=map(int,input().split())
n%=k
N=abs(n-k)
while N<n:
    n=N
    N=abs(n-k)
print(n)

Solution (Python):
N, K = map(int, input().split())
if N >= 2*K:
    s = N // K
    print(min(abs(N - s * K), abs(N- (s+1) * K)))
else:
    print(min(abs(N-K), abs(abs(N-K)-K)))

Solution (Python):
def main():
    N, K = list(map(int, input().split(' ')))
    print(min([N % K, K - (N % K)]))


if __name__ == '__main__':
    main()

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

cm = N
k = N % K
if k == 0:
    print(0)
    exit()
print(min(abs(k-K), N))

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

x = n
if n < k:
    a = abs(x - k)
    b = abs(a - k)
    print(min(a, b))
elif n == k:
    print(0)
else:
    x = n % k
    if x == 0:
        print(0)
    else:
        t = x - k
        print(abs(t))

Solution (Python):
def func():
  array = input().split(' ')
  N = int(array[0])
  K = int(array[1])
  K_2 = K / 2
  if N <= K_2:
    print(N)
    return
  while 1:
    if N >=K:
      N %= K
    else:
      N = K - N
    if N <= K_2:
      break;

  print(N)

func()

Solution (Python):
import math

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

Solution (Python):
N,K=map(int,input().split())
a=N//K
print(min(N-a*K,abs((N-a*K)-K)))

Solution (Python):
n, k = map(int, input().split()) 
print(min(n%k, k-n%k))

Solution (Python):
def main():
	n,k = map(int,input().split())
	
	mod = n % k
	if mod < k:
		print(min(mod,abs(mod-k)))
	else:
		print(mod)



if __name__ == '__main__':
	main()

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

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

Solution (Python):

import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string

from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import lru_cache, reduce

from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())

n,k = MI()
if n%k == 0:
    print(0)
else:
    n = n%k
    while abs(n-k) < n:
        n = abs(n-k)
    print(n)

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,(k-n%k)%k))

Solution (Python):
N,K=map(int,input().split())
if N%K>=K-N%K:
  print(K-N%K)
else:
  print(N%K)

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

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

Solution (Python):
N, K = map(int,(input().split()))
ans=N%K
if ans > abs(ans-K):
    ans=abs(ans-K)
print(str(int(ans)))

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

Solution (Python):
n,k = map(int,input().split())
print(min(n%k,k-n%k))

Solution (Python):
n,k=map(int,input().split())
if n>k:
    if n%k==0:
        print(0)
    else:
        a=n%k
        b=k/2
        if a<b:
            print(a)
        else:
            print(k-a)
else:

    print(min([n,k-n]))

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

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

Solution (Python):
n, k = map(int, input().split())
print(min(abs(n % k), abs(n - k * ((n // k) + 1))))

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

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

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

b = n % k

if b > k/2:
  print(k-b)
else:
  print(b)

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

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

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

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

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

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

print(min(a, b))

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

s = N % K

print(min(s, K-s))

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

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

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    N %= K
    return min(N, K-N)


if __name__ == '__main__':
    print(solve())

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

sho = n // k

a = n - k * sho
b = a - k

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

Solution (Python):
n, k = map(int,input().split())
a = n%k

if a<=abs(k-a):
  print(a)
else:
  print(abs(k-a))

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

a = n % k 
b = (k-n) % k

print(min(a,b))

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

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,k-n%k))

Solution (Python):
n, k = map(int,input().split())
a = n % k
b = k - a
if b <= a:
    print(b)
else:
    print(a)

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

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

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

x = abs(n%k)
x1 = abs(x-k)

if x <= x1:
    print(x)
else:
    print(x1)

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

mod = n%k
if mod <= k//2:
    print(mod)
else:
    print(k-mod)

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

Solution (Python):
n, k = map(int, input().split())
print(min(n % k, k - (n % k)))

Solution (Python):
from sys import stdin

n, k = [int(x) for x in stdin.readline().rstrip().split()]

print(min(n, abs(n % k), abs(n % k - k)))

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

max_i = max(n, k)


if n>=k:
    n = n%k
if n==0 or k==1:
    print(0)
else:
    for _ in range(max_i):
        tmp = abs(n-k)
        if tmp >= n:
            break
        else :
            n = tmp
    
    print(n)

Solution (Python):
import sys
N, K = map(int, sys.stdin.readline().split())
a1 = N - (N // K) * K
a2 = abs(N - ((N // K) + 1) * K)
print(min(a1, a2))

Solution (Python):
N, K = map(int, input().split())
N %= K
ans = 0
if abs(N - K) > N:
    ans = N
else:
   ans = abs(N-K)
print(ans)

Solution (Python):
n,k = map(int,input().split())
n -= n//k * k
while max(abs(n-k),n) != abs(n-k):
    n = abs(n-k)
print(n)

Solution (Python):
n, k = map(int, input().split())
print(min(n % k, k-(n % k)))

Solution (Python):
N,K = (int(x) for x in input().split())

a = N%K

if abs(K-a) > a:
    print(a)
else:
    print(abs(K-a) )

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

print(min(n%k,abs(k-(n%k))))

Solution (Python):
N,K=map(int,input().split())
a=N%K
b=K/2
 
if a<=b:
    print(a)
else:
    print(K-a)

Solution (Python):
N,K=map(int,input().split())
a=N%K
b=K/2

if a<=b:
    print(a)
else:
    print(K-a)

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

N = N % K

if N > K-N:
    print(K-N)
else:
    print(N)

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

t = N//K
N -= t*K

while abs(N-K) < N:
    N -= K
print(abs(N))

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

x = N % K

print(min(x, K-x))

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

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,k-n%k))

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

Solution (Python):
n,k = map(int, raw_input().split(' '))

seen = {}
while(n not in seen):
	seen[n] = abs(n - k)
	if n > k:
		n = n % k
	elif n <= k:
		n = abs(n-k)
print min(seen.values())

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

Solution (Python):
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	if n < 2 * k:
		n = abs(n-k)
	else:
		n %= k
		n += k		
print min(seen.values())

Solution (Python):
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	if n == k:
		break
	elif n > k:
		if n - k < k:
			n = abs(n-k)
		else:
			n %= k
			n += k
		
	elif n < k:
		n = abs(n - k)
print min(seen.values())

Solution (Python):
n, k = map(int, input().split())
n = n%k
while abs(n-k)<=n:
    n = abs(n-k)

print(n)

Solution (Python):
n,k=map(int,input().split())
if n<k:
  if k<2*n:
      print(k-n)
  else:
    print(n)
elif n==k:
  print(0)
else:
  m=n//k
  r=n-m*k
  print(min(r,abs(r-k)))

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

Solution (Python):
import sys
input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
ans1=min(n%k,k-n%k)
print(ans1)

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

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

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

n = n%k

if n < k-n:
    print(n)
else:
    print(k-n)

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

i = abs(n % k)
j = abs(n % -k)
if i == 0:
	print(0)
	exit(0)
print(min(i, j, n))