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-----
7 4

-----Sample Output-----
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.

Difficulty: introductory

Starter code:

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

N, K = map(int, input().split())
s = N//K
s1 = abs(N - K*s)
s2 = abs(N - K*(s+1))
print(min(s1, s2))

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N, K = MI()
    num = N // K
    ans = min(abs(N-K*(num)), abs(N-K*(num+1)))
    print(ans)
def __starting_point():
    main()
__starting_point()

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

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

ans = 0
if N % K == 0:
    ans = 0
else:
    a = N % K
    b = abs(a - K)
    ans = min(a, b)
print(ans)

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

N = N % K
nN = abs(N - K)
while(nN < N):
    N = nN
    nN = abs(N-K)

print(N)


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())
if(N >= K):
    print(min(N%K,K-(N%K)))
else:
    print(min(N,K-N))

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

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

def N():
    return int(input())
def L():
    return list(map(int,input().split()))
def NL(n):
    return [list(map(int,input().split())) for i in range(n)]
mod = pow(10,9)+7

#import numpy as np
import sys
import math
import collections

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

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

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

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

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

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

#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc161/tasks/abc161_c

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

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


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

import sys

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

ama = n % k
ama_k =abs(ama - k)

if n % k == 0:
    print('0')
    return

    
elif ama >= ama_k:
    print(ama_k)
    return
    
elif ama <= ama_k:
    print(ama)
    return


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

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

N, K = map(int, input().split())
ans = N
a = ans // K
b = ans % K

if a > 0:
    ans = N - K * a
    a = ans // K
    b = ans % K

if a  == 0:
    if ans > abs(ans - K):
        print(abs(ans - K))
    else:
        print(ans)

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

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

# -*- coding:utf-8 -*-
N,K = map(int,input().split())

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

print(ans)

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

def i_input(): return int(input())


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


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


def i_row(N): return [int(input()) for _ in range(N)]


def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]


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

import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

def __starting_point():

    n,k = list(map(int,input().split()))
    s = set()
    n = n % k
    while True:
        tmp = abs(n-k)
        if tmp in s:
            break
        s.add(tmp)
        n = min(n,tmp)
    print(n)

__starting_point()

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

y = N%K
z = abs(K-y)

ans = min(y,z)

print(ans)

from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    n,k=list(map(int,stdin.readline().split()))
    if n%k==0:print(0)
    else:
        rem=n%k
        print(min(rem,abs(k-rem)))

x,y = map(int,input().split())

z = x % y
p = y - z
if(z < p):
    print(z)
else:
    print(p)

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

n = n % k
abs_n_minus_k = abs(n - k)
ans = n

if n % k == 0:
    ans = 0
else:
    while abs_n_minus_k < n:
        n = abs_n_minus_k
        abs_n_minus_k = abs(n - k)
        ans = n

print(ans)


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

result = N % K
if result <= abs(result-K):
  print(result)
elif result > abs(result-K):
  result = abs(result-K)
  print(result)

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


class Solution:
    def __init__(self, n, k):
        self.n = n
        self.k = k

    def answer(self):
        self.n = self.n % self.k
        abs_n_minus_k = abs(self.n - self.k)
        ans = self.n
        if self.n % self.k == 0:
            ans = 0
        else:
            while abs_n_minus_k < self.n:
                self.n = abs_n_minus_k
                abs_n_minus_k = abs(self.n - self.k)
                ans = self.n
        print(ans)


conditions = Solution(n, k)
conditions.answer()


n, k = list(map(int, input().split()))
r = n % k
l = []
while(not l.count(r)):
    l.append(r)
    r = abs(r - k)
l = sorted(l)
print((str(l[0])))


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

x = N
pre_x = x

if x // K > 0:
    x = abs(x - K * (x // K))
    pre_x = x

while True:
    pre_x = x
    x = abs(x - K)

    if pre_x < x:
        print(pre_x)
        return


# coding=utf-8
import sys

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

    if N % K == 0:
        print('0')
        return

    N = N % K

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

    print(N)
__starting_point()

def main() -> None:
    n, k = list(map(int, input().split()))

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


def __starting_point():
    main()

__starting_point()

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

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

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

N, K = list(map(int, input().split()))
P = N % K
A = [P, K-P]

if A[0]<=A[1]:
    print((A[0]))
elif A[0]>A[1]:
    print((A[1]))


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

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



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

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

# import math
# import statistics
# import itertools
# a=int(input())
# b=input()
# c=[]
# for i in a:
#     c.append(int(i))
N,K= map(int,input().split())
# f = list(map(int,input().split()))
# g = [int(input()) for _ in range(N)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen

if N%K==0:
    print(0)
else:
    et=N//K
    a1=et*K
    a2=(et+1)*K
    mi = min(abs(a1-N),abs(a2-N))
    print(mi)

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

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


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

modK = N % K

if modK <= K//2:
    print(modK)
else:
    print(K - modK)

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

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

print((min(n-k*(n//k),-n+k*(n//k+1))))


def resolve():
    n,k = map(int,input().split())
    print(min(n-k*(n//k),abs(n-k*(n//k)-k)))
resolve()

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

tem = N // K

print(min(abs(N - K*tem),abs(N - K*(tem+1))))

a,b=[int(s) for s in input().split()]
print(min([a%b,(b-a)%b]))

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

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

if N > K:
    N %= K

if (K / 2) >= N:
    print(N)
else:
    print(K - N)

n,k = map(int,input().split())
check = []
check.append(n%k)
check.append(k-check[0])
print(min(check))

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

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

a,b=map(int,input().split());print(min(a%b,b-a%b))

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

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

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

a,b=map(int,input().split());print(min(a%b,-a%b))

a, b = map(int, input().split()); print(min(a%b, b - a%b))

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

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


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

a,b=map(int,input().split());print(min(a%b,-a%b))

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

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


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

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


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

if n == k:
    print((0))
    return
elif n > k:
    n = n % k

# n < k

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


def main():
    n, k = list(map(int, input().split()))
    ans = [n % k, abs((n % k) - k)]
    return min(ans)


def __starting_point():
    print((main()))

__starting_point()

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

import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

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

tmp = 0
res = 0

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

print(res)


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

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


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

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


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

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

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

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


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


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

if N==K:
  print((0))

elif N<K:
  lis1=[N,abs(N-K)]
  print((min(lis1)))
  
else:
  lis2=[N%K,abs(N%K-K)]
  print((min(lis2)))
  


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

n,k=map(int,input().split())
x1=n%k
x2=abs(n%k-k)
if x1<x2:
    print(x1)
else:
    print(x2)

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

def absolute(x):
    x=x-K
    if x<0:
        x=-x
    return x

if N<K:
    while N>abs(N-K):
        N=absolute(N)
    print(N)

elif N%K==0:
    print((0))
else:
    b=1
    s=K
    while b>0 and N>abs(N-s):
        b=N%K
        N=K
        a=K
        K=b
    print(a)


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

N, K = map(int, input().split())
residue = N%K
if residue*2 < K:
    print(residue)
else:
    print(K-residue)

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


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

def Aoki(x, K):
        x = abs(x - K)
        return x


if N % K == 0:
        ans = 0
        print(ans)
        return
lis = []
lis.append(N)
x = N % K 
for i in range(10000):
        lis.append(x)
        x = Aoki(x, K)
print((min(lis)))


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

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

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

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

if x  < y:
    print(x)
else:
    print(y)




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

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

print(ans)

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

L = []



for i in range(10**5):
    M = N // K
    if i == 0:
        N = abs(N - K)
        L.append(N)
    else:
        if N > K:
            N = N - (K * M)
            L.append(N)
        else:
            N = abs(L[i-1] - K)
            L.append(N)


print(min(L))

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


x = n % k

y = abs(x - k)

if x >= y:
    print(y)
else:
    print(x)


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

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

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

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

import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print((min(n, m-n)))


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

mod1 = N % K
mod2 = (K - mod1)

print((mod1 if mod1 < mod2 else mod2))


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

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