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())
ans = n
if n > k:
    n = n%k
    ans = min(ans, n)
n = abs(k-n)
ans = min(ans, n)

print(ans)

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

Solution (Python):
import sys













readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, k = list(map(int, readline().split()))

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

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

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

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

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

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

n = 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):
N, K = map(int, input().split())
r1 = N % K
print(min(r1, K - r1))

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

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

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

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

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):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
INF = 1 << 60
sys.setrecursionlimit(10 ** 6)


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

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

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

    p1 = n % k
    p2 = abs(p1 - k)

    ans = min(p1, p2)

    print(ans)


if __name__ == '__main__':
    main()

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

amari = n % k

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

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

Solution (Python):
import sys
import math
import collections
from collections import deque
import heapq

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




x = n % k
y = k - x

print(min(x, y))

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

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

d,r = divmod(N,K)
print(min(r,K-r))

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

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

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

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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


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

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

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

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

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

base = n%k

print(min(base,k-base))

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

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

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

Solution (Python):
import sys

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 LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

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

main()

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

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

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

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

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

t = n//k

A = [abs(n - (t - 1) * k), abs(n - t * k), abs(n - (t + 1) * k)]

print(min(A))

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

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

res1 = N%K

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

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

Solution (Python):
N, K = [int(i) for i in input().split()]
x = N//K
print(min(N-K*x, abs(N-K*(x+1))))

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

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

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

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


if __name__ == '__main__':
    main()

Solution (Python):


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

div = n//k

temp1 = n - div * k
temp2 = abs(temp1 - k)

print(min(temp1, temp2))

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

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

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

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

count = n // k
ans1 = n - count * k
ans2 = abs(ans1 - k)
print(min(ans1, ans2))

Solution (Python):
N, K = [int(i) for i in input().split()]
n = N//K
num = N%K
if num > K - num:
    num = K - num
    n += 1

print(num)

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

Solution (Python):
import sys


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


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


if __name__ == "__main__":
    main()

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

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

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 

'''

'''

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

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

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

Solution (Python):
import sys

stdin = sys.stdin

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

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

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

arr = []
for i in range(1000):
    arr.append(abs(nn))
    nn -= k
print(min(arr))

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

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

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):
import sys
input = sys.stdin.readline

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

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

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

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

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

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

Solution (Python):
import sys

stdin = sys.stdin

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

n, k = na()

if n > k:
    n = n % k + k
mi = n
for i in range(10):
    n = abs(n-k)
    mi = min(n, mi)
print(mi)

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


main()

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

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

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

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

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


if __name__ == '__main__':
    main()

Solution (Python):
[n,k] = [int(i) for i in 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):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n,k = m()

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

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

Solution (Python):
import sys
import math
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n,k = na()
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):
a,b = map(int,input().split())
c = a%b
print(min(c,b-c))

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











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,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 fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    N%=K
    ans = min(abs(N-K), N)
    print(ans)
if __name__ == '__main__':
    main()

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

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

Solution (Python):
from sys import stdin

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

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())

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

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

ans1 = N%K
ans2 = -(ans1-K)

if ans1 >= ans2:
  ans = ans2
else:
  ans = ans1

print(ans)

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

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

while abs(N-K) < N:
    if N > K:
        N %= K
        N = min(N, abs(N-K))
    else:
        N = K%N

print(N)

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

Solution (Python):

from collections import Counter
def inputlist(): return [int(k) for k in input().split()]

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

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

Solution (Python):

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

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

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

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.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.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 = LI()
    print(min(n%k,-n%k))
    return


if __name__ == "__main__":
    solve()

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

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

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


if __name__ == '__main__':
    main()

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    a=N%K
    b=K-N%K
    print(min(a,b))

main()