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):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	n,k = MI()
	tmp = 0
	n = n%k
	while True:
		tmp = n
		n = min(n, abs(n-k))
		if(tmp == n):
			break
	print(tmp)


if __name__ == '__main__':
	main()

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

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

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

Solution (Python):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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,abs(n%k-k)))

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    print(min(abs(N//K*K-N),abs((N//K+1)*K-N)))

if __name__ == '__main__':
    main()

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

x1 = N % K
x2 = K - x1

if x1 < x2:
	print(x1)
else:
	print(x2)

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

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

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

if N%K==0:
    out=0

else:
    amari=N%K
    out = min(amari, abs(amari-K))

print(out)

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

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

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

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


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


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


if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    if n == k or n%k == 0:
        print(0)
        return
    n = n%k
    print(min(n,abs(n-k)))
    
    
if __name__ == "__main__":
    main()

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

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

k2 = n % k

print(min(k2, k - k2))

Solution (Python):
N,K = [int(i.strip()) for i in input().split(' ')]

M = N % K
result = M if K -M > M else K-M
print(result)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

    if N % K == 0:
        print(0)
    else:
        i = N // K
        print(min(abs(N - i * K), abs(N - (i+1) * K)))

if __name__ == '__main__':
    main()

Solution (Python):
mod = 10**9+7
import sys
from collections import Counter, defaultdict, deque
from itertools import product, permutations, combinations
from itertools import accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from math import ceil,floor
from copy import deepcopy
from heapq import heappop,heappush,heapify
import heapq


sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1


p2D = lambda x: print(*x, sep="\n")

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

def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())




def LLI(rows_number): return [LI() for _ in range(rows_number)]


def LI(): return list(map(lambda x:int(x), sys.stdin.readline().split()))
def HI(): return list(map(lambda x:int(x)*(-1), sys.stdin.readline().split()))


N,K = MI()
a = N%K
print(min(a,K-a))

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

x = N % K

ans = min(x,K-x)

print(ans)

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())

if n>k:
    n-=n//k*k


print(min(k-n,n))

Solution (Python):
def get_ints():
    return map(int, input().split())

n, k = get_ints()

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

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

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

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):
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    m1=n%k
    m2=k-m1
    print(min(m1,m2))
if __name__ == '__main__':
    main()

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

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N, K = list(map(int, sys.stdin.buffer.readline().split()))


def solve(n, k):
    n = n % k
    return min(n, k - n)
ans = solve(N, K)
print(ans)

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

Solution (Python):
N,K = map(int,input().split())
if(N>=K):
    N = N%K
while(1):
    new_N = abs(N-K)
    if(new_N<N):
        N = new_N
    else:
        break
print(N)

Solution (Python):

def resolve():
    N,K = map(int,input().split())
    a = N % K

    print(min(a,K-a))




if __name__ == "__main__":
    resolve()

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

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

Solution (Python):
from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque
def data(): return sys.stdin.readline().strip()
def mdata(): return map(int, data().split())


mod=int(1e9+7)

n,k=mdata()
n=n%k
print(min(n,k-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())
a = n%k
b = abs(a-k)
print(min(a,b))

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

if N >= K:
    N = N%K

print(min(N,K-N))

Solution (Python):
import math
from collections import defaultdict
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:input()

"""========main code==============="""

x,y=ml()
a=x%y
print(min(a,y-a))

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

ans = n%k

while True:
    if ans > abs(ans-k):
        ans = abs(ans -k)
    else:
        break

print(ans)

Solution (Python):

n, k = map(int, input().split())
print(min(n, n-n//k*k, (n+k-1)//k*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())
rem = n%k
print(min(rem, k-rem))

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

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

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

Solution (Python):

import sys


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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = [int(i) for i in input().split()]
N = N%K + K
a = abs(N-K)
cand = [a, K-a]
print(min(cand))

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n,k=mii()

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

Solution (Python):
a,b=[int(i) for i in input().split()]
p=a//b
print(min(abs(a-p*b-b),a-p*b))

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

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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

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

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,k = mi()
if n == 0:
    print(0)
    exit()
ans = n
temp1 = n % k
temp2 = k - temp1
ans = min(temp1,ans)
ans = min(temp2,ans)
print(ans)

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

if __name__ == '__main__':
    main()

Solution (Python):



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


def main():
    s = N % K
    ans = min(s, K - s)
    print(ans)    


if __name__ == "__main__":
    main()

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

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

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().strip().split())

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

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

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
if n>k:
    n=n-n//k*k
ans=10**18+10
for i in range(10000):
    ans=min(ans,n)
    n=abs(n-k)
print(ans)

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

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

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

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

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

Solution (Python):
def main():

    N, K = map(int, input().split())
    return min([N, N%K, K-N%K])

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

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

h = n%k

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

Solution (Python):

import sys


def solve(N: int, K: int):
    n = N % K
    for _ in range(100):
        n = min(n, abs(n - K))
    print(n)



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

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

Solution (Python):

from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def nextLine(): return next(stdin)
def nextStrList(): return nextLine().split()
def nextIntList(): return [int(_) for _ in nextStrList()]


def main():
	n,k=nextIntList()
	x=n%k
	x1=abs(x-k)
	print(min(x,x1))

if __name__=="__main__":
	main()

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, abs(n-(n//k)*k), abs(n-(n//k+1)*k)))

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

Solution (Python):
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, K = read_int()

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

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

Solution (Python):
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq


def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def main():
    N, K = iip(True)
    N = N % K
    N = min(N, (K-N))
    print(N)


def solve():
    N = iip(False)
    S = input()




def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_=mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



n, k = LI()
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+1)*K)))

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

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

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

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

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

n %= k

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

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

Solution (Python):
from collections import deque
from copy import copy,deepcopy
from itertools import permutations,combinations
from pprint import pprint

def myinput():
    return map(int,input().split())

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

n,k = myinput()

r = n%k
r2 = abs(r-k)

ans = min(r,r2)

print(ans)

Solution (Python):
N, M = [int(x) for x in input().split()]
a = N // M
b = N%M
c = abs(N - a*M)
d = abs(N - (a+1)*M)
print(min(c,d))

Solution (Python):

n, k = map(int, input().split())
q = n // k
print(min(n - q * k, abs(n - (q + 1) * k)))

Solution (Python):
def main():
    n,k=map(int,input().split())
    dis=n//k
    n-=k*dis
    ans=n if abs(n-k)>n else abs(n-k)
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):

"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""

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

ans=N%K
if ans!=0:
    ans=K-ans
ans=min(ans,N)
print(ans)

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

Solution (Python):
import sys, 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, groupby
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
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

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

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

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->N-K
K<N
N->K-N->N
'''
S={N}
while(1):
    if N>K:
        N=N%K
    elif N==K:
        N=0
    else:
        N=K-N
    if N in S:
        break
    S.add(N)
print(min(S))

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

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

amari = n%k
amari2 = abs(amari-k)

print(min(amari, amari2))

Solution (Python):



 
import sys
import math



 

fileoperation=0
if(fileoperation):
    orig_stdout = sys.stdout
    orig_stdin = sys.stdin
    inputfile = open('W:/Competitive Programming/input.txt', 'r')
    outputfile = open('W:/Competitive Programming/output.txt', 'w')
    sys.stdin = inputfile
    sys.stdout = outputfile


mod=1000000007
 

def nospace(l):
    ans=''.join(str(i) for i in l)
    return ans
 
 
 

t=1
for tt in range(t):
    
    n,k= map(int, sys.stdin.readline().split(' '))
    if(n==k):
        print(0)
        continue
    if(n<k):
        print(min(n,k-n))
        continue
    else:
        n=(n%k)
        print(min(n,k-n))
        continue
    
    
    

if(fileoperation):
    sys.stdout = orig_stdout
    sys.stdin = orig_stdin
    inputfile.close()
    outputfile.close()

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

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

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()

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

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

Solution (Python):
def main():
    n,k = tuple([int(t)for t in input().split()])

    t = n%k

    s = k-t
    print(min(t,s))

if __name__ == "__main__":
    main()

Solution (Python):



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

Solution (Python):
n,k=map(int,input().split())
n=n%k
print(min(n,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):


def solve():
    N, K = map(int, input().split())
    return str(min(N%K, (-N)%K,))

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

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
def even(n): return 1 if n%2==0 else 0

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


res = n//k

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

print(ans)

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

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

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

x = N // K

ans = N - K * x

temp = abs(ans - K)

if ans >= temp:
    print(temp)
else:
    print(ans)

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

Solution (Python):
N, K = map(int, input().split())
temp = N // K
ans = N - temp*K

temp2 = abs(ans - K)
print(min(ans, temp2))

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

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

Solution (Python):
import math
N,K = map(int,input().split())
anss = []
q = N % K
anss.append(q)
anss.append(abs(q - K))
print(min(anss))

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

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

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

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

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

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

if __name__ == '__main__':
    resolve()

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

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

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

ans=n%k

if ans<abs(ans-k):
    print(ans)
else:
    print(abs(ans-k))

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

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

ans1 = n%k
ans2 = k - n%k
print(min(ans1,ans2))

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,abs(N%K-K) ) )

Solution (Python):
N, K = [int(x) for x in input().split()]
t = N % K
print(min(t, abs(t-K)))

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

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

C()

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()))

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

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

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

Solution (Python):

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

tmp = n // k
ans = min(abs(n - k * tmp), abs(n - k * (tmp + 1)))
print(ans)

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

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

print(min(a,b))

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

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

print(min(a,b))

Solution (Python):
N,K = list(map(int,input().split()))
m = N
N -= N//K*K
for i in range(100):
    N = abs(N-K)
    m = min(m,N)
print(m)

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):
x, k = map(int, input().split())

if x > k:
    
    x = x - k * (x//k)

print(min(x, k-x))

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

if n > k:
    n = n - (n // k) * k

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

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    a = N - (N//K)*K
    print(min(a, abs(a-K)))


if __name__ == '__main__':
    main()

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

ans = n % k

if ans > (k-ans):
    print(k-ans)
else:
    print(ans)

Solution (Python):

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

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

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

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

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

if N > K:
    N -= K * (N // K)

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

print(N)

Solution (Python):
(N, K) = [int(x) for x in input().split()]
a=N%K
print(min(a, abs(a-K)))

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]

    x = N // K


    print(min(N % K, (x + 1) * K  - N))


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):



from math import *
from copy import *
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize
from operator import *				
from itertools import *
from collections import Counter		
from collections import defaultdict 

'''
s
'''

def solve(n,k):
	return min(n%k,abs(n%k - k))
	

N,K = list(map(int, stdin.readline().rstrip().split()))
ans = solve(N,K)
print(ans)

Solution (Python):

import sys


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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

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

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

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