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

from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

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

yes = "Yes"
no = "No"

def main():
    n,k = mi()
    print(min(abs(n-(n//k)*k),abs(n-(n//k+1)*k)))


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

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

Solution (Python):
import sys

input = sys.stdin.readline


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

    _, r = divmod(N, K)
    ans = min(abs(r - K), r, r + K)
    print(ans)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
def read():
    N, K = list(map(int, input().strip().split()))
    return N, K

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


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

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

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

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

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

Solution (Python):
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy




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

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

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

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations
from itertools import accumulate
from math import ceil, sqrt, pi

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

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

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

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

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

Solution (Python):

import sys


def solve(N: int, K: int):

    N = N%K

    s = set()

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

    print(min(s))
    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())

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

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

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

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

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

t = n % k

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

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    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):
n,k = map(int,input().split())
l = n//k
a = abs(n-k*(l-1))
b = abs(n-k*l)
c = abs(n-k*(l+1))
print(min(a,b,c))

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

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N,K = map(int,input().split(' '))
d = N%K
dif = abs(d-K)
print(min(d,dif))

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

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()

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

Solution (Python):
s=list(map(int,input().split()))
a=s[0]%s[1]
b=abs(a-s[1])
if a>b:
  print(b)
else:
  print(a)

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

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

Solution (Python):
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


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


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


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


if __name__ == '__main__':
    main()

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

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

ctr = N // K

print(min(abs(N - K * ctr), abs(N - K * (ctr + 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())

n = n % k
print(min(n,abs(n-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())
    d = n % k
    dd = abs(d - k)
    print(min(d,dd))
if __name__=='__main__':
    main()

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

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

if __name__ == '__main__':
    main()

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

mod = n % k

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

Solution (Python):
import sys

def solve():
      input = sys.stdin.readline
      N, K = map(int, input().split())
      d = N % K
      minN = N
      minN = min(minN, min(d, K - d))
      print(minN)

      return 0

if __name__ == "__main__":
      solve()

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

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

    l = N % K
    r = abs(K -l)

    print(min(l,r))

if __name__ == "__main__":
    main()

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

Solution (Python):

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

r = N%K

if 2*r <= K:
  print(r)
else:
  print(K-r)

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

def a(x, K):
    return abs(x-K)

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

pre = N + 1
while True:
    if pre <= N:
        N = pre
        break
    pre = N
    N = a(N, K)

print(N)

Solution (Python):
def solve():
    N, K = map(int, input().split())
    N = N % K
    print(min(N, abs(N-K)))
if __name__ == '__main__':
    solve()

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

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

Solution (Python):


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

    N = N%K

    while True:
        preN = N
        N = abs(N-K)
        if N > preN:
            print(preN)
            return

if __name__ == "__main__":
    solve()

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

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

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

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

cnt = n//k

print(min(abs(n-cnt*k),abs(n-(cnt+1)*k)))

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

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

if (n % k == 0):
    print(0)
else:
    l = n // k
    print(min(n, abs(n-k*l), abs(n-k*(l+1))))

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

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

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

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

print(min(K - N, N))

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

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

if N >= K:
    a = N%K
    print(min(a,K-a))
else:
    print(min(N,K-N))

Solution (Python):

import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from heapq import heappop,heappush
import time
import os
import sys
import datetime
from functools import lru_cache
readline=sys.stdin.readline
sys.setrecursionlimit(2000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s


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 <= k//2:
     print(n % k)
else:
     print(k-n%k)

Solution (Python):
N, K = [int(i) for i in input().split()]
if N>=K:
    N %= K
print(min(N, K-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
print(min(abs(N-K*n),abs(N-K*(n+1))))

Solution (Python):
import copy

import math
import fractions

def j(n):
    if n:print("Yes")
    else:print("No")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printrow(a):
    for i in a:
        print(i)

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

n,k = ips()
a = n//k
b = n//k + 1
a*=k
b*=k
ca = abs(n-a)
cb = abs(n-b)
print(min(ca,cb))

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

x = abs(N - div * K)
y = abs(N - (div + 1) * K)
print(min(x, y))

Solution (Python):
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
if n>=k:
    x=n//k
    n-=x*k
ans=min(n,k-n)
print(ans)

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

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

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

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

Solution (Python):
N,K=input().split()
N=int(N)
K=int(K)
if (0<=N<=10**18)and(1<=K<=10**18):
  a=N%K
  b=K-a
  if a>b:
    print(b)
  if a<=b:
    print(a)

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

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def isOK(A):
    A.sort()
    for i in range(len(A) - 1):
        if A[i] + 1 != A[i + 1]:
            return False
    return True


def solve():
    N, K = Scanner.map_int()
    print(min(N % K, K - N % K))


def main():
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
def examA():
    X, Y, Z = LI()
    X, Y = Y,X
    X,Z = Z, X
    print(X,Y,Z)
    return

def examB():
    N, M = LI()
    A = LI()
    need = (sum(A)-1)//(4*M) + 1
    ans = 0
    
    for a in A:
        if a>=need:
            ans += 1
    if ans>=M:
        print("Yes")
    else:
        print("No")
    return

def examC():
    N, K = LI()
    ans = N%K
    if K-ans<ans:
        ans = K-ans
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examC()

"""

"""

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, K = lr()
    tmp = N%K
    print(min(abs(tmp-K), tmp))
resolve()

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

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

r = N % K

print(min(r, K - r))

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

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 == 0:
    print(0)
else:
    print(min(abs((n//k)*k - n), abs((n//k + 1)*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())

mm = N%K

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

Solution (Python):
import sys
import itertools
import queue
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, K = read_values()
    R = N % K
    print(min(R, K - R))

if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):
n,k=list(map(int, input().split()))
x=n%k
if x>(k+1)//2:
  print(k-x)
else:
  print(x)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
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 [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()

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

  a=n
  b=abs(n-k)
  f1=f2=False
  for i in range(100):
    n=abs(n-k)
    if a==n:
      f1=True
    if b==n:
      f2=True

    if f1 and f2:
      return min(a,b)


print(main())

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

a = n%k

print(min(a,k-a))

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

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

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

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

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

N = N % K

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

print (ans)

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())
    if N % K == 0:
        print(0)
        exit()
    
    mod = N % K
    ans = min(mod, K - mod)
    print(ans)
    


if __name__ == "__main__":
    main()

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

N,K=MI()

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

Solution (Python):


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




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

Solution (Python):
import math
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()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


N,K= nm()
s = N % K
if s > K//2:
    s = K -s 
print(s)

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

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

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

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

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

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

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

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())
x = N%K
print(min(x,abs(x-K)))

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

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

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

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

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

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

Solution (Python):


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

temp=N//K

N-=K*temp

print(min(N,K-N))

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

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

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

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

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

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


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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

    a = x % k
    res = min(a, k-a)
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

tmp1 = N
tmp2 = abs(K - N)
print(min(tmp1, tmp2))

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

a = n//k
n=n-a*k

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

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

p=n//k
n-=k*p

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

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

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

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

n %= k

print(min(k-n,n))

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

print(min(m, k-m))

Solution (Python):

import sys


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

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

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

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

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

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

d = set()
ans = n
while n not in d:
    d.add(n)
    if n > k:
        n = n % k
    else:
        n = k - n
    ans = min(ans, n)

print(ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(1000000)
input = sys.stdin.readline
INF = 2**62-1

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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K):
    m = N % K
    return min(m, K-m)


def main():
    N, K = read_int_n()
    print(slv(N, K))


if __name__ == '__main__':
    main()

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


resolve()

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

Solution (Python):
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


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


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % mod) % mod
        
    return CONV()



sys.setrecursionlimit(200005)

def main():
    N,K = isRaw()
    modN = N%K
    return min(modN,abs(K-modN))

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

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

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

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

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