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



Sample input:
7 4


Sample output:
1

Submission status: Accepted

Solution code:
import sys
import math
import 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
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
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import itertools
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if K == 1:
    print(0)
    exit(0)

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

minimum = N
previous_value = 1000000000000000001
while True:
    N = abs(N - K)

    if N < minimum:
        minimum = N

    if N >= previous_value:
        break

    previous_value = N

print(minimum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    if N > K:
        print(min(N % K,abs(N % K-K)))
    else :
        N = K - N
        print(min(N % K,abs(N % K-K)))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
div = n//m
minval = div*m
maxval = (div+1)*m
print(min(abs(minval-n),abs(maxval-n)))

Submission status: Accepted

Solution code:
import sys
n, k = map(int, input().split())

if n == 0:
  print(n)
  sys.exit()

if n >= k:
  n = n % k

for i in range(10**18):
  if abs(k-n) < n:
    n = abs(k-n)
  else:
    break

print(n)

Submission status: Accepted

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

mae1 = 0
mae2 = 0
mae3 = 0

def calc(n, k):
    return abs(n-k)

if n > k:
    n = n % k

while(mae1!=n):
    n = calc(n, k)
    mae1 = mae2
    mae2 = mae3
    mae3 = n
    
print(min(mae1, mae2))

Submission status: Accepted

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

ans1 = N%K

ans2 = abs(ans1-K)

print(min(ans1,ans2))

Submission status: Accepted

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

if N<=K:
  print( min(N,K-N) )
else:
  if N%K==0:
    print(0)
  else:
    c=N%K
    print(min (c,K-c) )

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
if a > b:
    a = a % b
print("{}".format(min(a,b-a)))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
if abs(n - k) >= n:
    print(n)
    exit()
while n > k/2:
    if n > k:
        n = n%k
    else:
        n = k%n
print(n)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
if N == 0 or K == 1:
    print(0)
    exit()
if N >= K and (N - K) >= K:
    N -= (N - K) // K * K
if N >= K:
    N = abs(N-K)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def solv(N, K):
    if N % K == 0:
        return 0
    N = N % K
    return min(N, K-N)


if __name__ == "__main__":

    # N K
    #
    N, K = map(int, input().split())
    ans = solv(N, K)

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

mina = N - (N//K) * K
minb = abs(N - (N//K + 1) * K)
print(min(mina,minb,N))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-*-

from sys import stdin


def main():
    N, K = map(int, stdin.readline().split())
    # N, K = 7, 4

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
n = n%k
m =[]
while n not in m:
    m.append(n)
    n = abs(n-k)
print(min(m))

Submission status: Accepted

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

min_num = N % K

while True:
    tmp = min_num
    abs_num = abs(tmp-K)

    if abs_num < min_num:
        min_num = abs_num
    else:
        break


print(min_num)

Submission status: Accepted

Solution code:
import sys
import numpy as np
# for AtCoder
import fractions
from collections import deque 
from functools import reduce

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

mi = n
mo = n % k

if mi > mo:
    mi = mo
if mi > abs(mo - k):
    mi = abs(mo - k)    
print(mi)

Submission status: Accepted

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

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

if a < b:
    print(a)
else:
    print(b)

Submission status: Accepted

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

nn = n // k
nnn = n - nn * k

base = n
for i in range(10000):
    xxx = abs(nnn - k)
    if xxx < nnn:
        base = xxx
    
    nnn = xxx

print(base)

Submission status: Accepted

Solution code:
n,k = (int(x) for x in input().split())
a = n//k
n-=k*a
while(True):
    dif = abs(n-k)
    if dif<n:
        n=dif
    else:
        break
print(n)

Submission status: Accepted

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

alpha = N // K

print(min(N - alpha * K, (alpha + 1) * K - N))

Submission status: Accepted

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

ans, div = divmod(N, K)
div2 = abs(div-K)

print(min(div, div2))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
m = N % K
print(min([N, m, K - m]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

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

ans1 = n%k

if ans1 > k-ans1:
  ans1 = k-ans1
print(ans1)

Submission status: Accepted

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

Submission status: Accepted

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

if n == k:
    print(0)
if n < k:
    if n <= k-n:
        print(n)
    if n > k-n:
        print(k-n)
if n > k:
    n2 = n%k
    print(min(n2,abs(n2-k)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
modulo = n % k
if modulo == 0:
    ans = 0
else:
    ans = min(k - modulo, n)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

x=n%k

y=abs(x-k)

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
n,k = list(map(int, input().split()))
mod = n%k
ret = mod if mod < abs(k-mod) else abs(k-mod)
print(ret)

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
flag = True

n = n % k
ans = n

if(ans > abs(n-k)):
    ans = abs(n-k)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N_K = list(map(int, input().split()))
buf = N_K[0] % N_K[1]
print(buf) if N_K[1]-buf > buf else print(N_K[1] - buf)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
if N > K:
    mod = N % K
    ret = min (K - mod, mod)
else:
    ret = min (K - N, N)
print(ret)

Submission status: Accepted

Solution code:
import numpy as np

input_str = input()
N, K = list(map(lambda x: int(x), input_str.split(' ')))

N = N % K

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

Submission status: Accepted

Solution code:
import sys

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

pre = N
while True:
    if N % K == 0 and N // K >= 1:
        print(0)
        sys.exit()
        
    if N > K:
        N = N % K
    else:
        N = abs(N-K)
    
    if pre <= N:
        print(pre)
        sys.exit()
    else:
        pre = N

Submission status: Accepted

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

n = n%k

tmp = n
n = k - n

if tmp > n:
    print(n)
else:
    print(tmp)

Submission status: Accepted

Solution code:
n,k = (int(i) for i in input().split())
m = n%k
l =abs(n-k)
r = abs(m-k)

print(min(l,r,m))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n-=(n//k)*k
if n==0:
  print(0)
  exit()
mi=n
for i in range(100):
  if abs(n-k)<mi:
    mi=abs(n-k)
  n=abs(n-k)
print(mi)

Submission status: Accepted

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

q, mod = divmod(N, K)

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

Submission status: Accepted

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

# あまりになったタイミングで繰り返しになる
tmp = N % K
if K-tmp < tmp:
    tmp = K-tmp
print(tmp)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
while True:
    w = int(n/k)
    c = n
    n = abs(n-(w*k))
    if n==c:
        while True:
            c = n
            n = min(n,abs(n-k)) 
            if n == c:
                break
        break
print(n)

Submission status: Accepted

Solution code:
# coding: utf-8
# 標準入力 <int><int>
N, K = map(int, input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

if n <= k//2:
    print(n)
elif n <= k:
    print(k-n)
else:
    s = n//k
    print(min(n-(k*s), k*(s+1)-n))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
while True:
    if n > k:
        a = n % k
    else:
        a = abs(n-k)
    if n > a:
        n = a
    else:
        print(n)
        break

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
n, k = map(int, input().split()) # 入力が複数
n = n % k
d = set([n])
while True:
    diff = abs(n - k)
    pre = n
    n = min(diff, n)
    if n in d:
        break
    else:
        d.add(n)
print(min(d))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
N = N%K
while abs(N)>=K:
  N = abs(N-K)
check_neg = abs(N - K)
print(min(N, check_neg))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    rest = N % K

    if rest > K / 2:
        print(abs(rest - K))
    else:
        print(rest)
    
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

if abs(n-k) >= n:
    print(n)
else:
    tmp = n % k
    print(min(tmp, abs(tmp-k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n , m = map(int, input().split())

while n // m > 0:
  n  = n % m 
print(min(n, abs(n-m)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, k = map(int, input().split())

if n > k:
    r = n % k
    print(min(abs(r-k), abs(r)))
else:
    print(min(abs(n-k), abs(n)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
if N==K:
    print(0)
elif N > K:
    if N%K == 0:
        print(0)
    else:
        print(min(N%K,K-(N%K)))
elif N < K:
    if 2*N<K:
        print(N)
    else:
        print(abs(N-K))

Submission status: Accepted

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

# if k == 1:
#     print(0)
# else:
#     for i in range(1000000000000000000):
#         if n > abs(n-k):
#             n = abs(n-k)
#         elif n <= abs(n-k):
#             break
#     print(n)
amari = n % k
if abs(amari-k) < amari:
    print(abs(amari-k))
else:
    print(amari)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 161: C (2020/04/04)
N, K = map(int, input().split())
A = abs(N-K*(N//K))
B = abs(N-K*(N//K+1))
if A < B:
    print(A)
else:
    print(B)

Submission status: Accepted

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
t = [int(p) for p in s]

N = t[0]
K = t[1]

#p = int(N/K)
#a = N - p * K
a = Decimal(N % K)
b = Decimal(K - a)
#print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))
print(min(a,b))
# 1000000000000000000

Submission status: Accepted

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

n=n%k

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N>=K:
    d = N//K
    N -= K*d
if N<K:
    N = min(K-N, N)
print(N)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math
N, K = map(int, input().split(' '))

if N >= K:
    a = N // K
    print(min(N-(a*K), abs(N-(a+1)*K)))
else:
    print(min(N, K-N))

Submission status: Accepted

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

ans_1 = n%k

ans_2 = abs(ans_1-k)



print(min(ans_1,ans_2))

Submission status: Accepted

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

# x -> abs(x - K)
x = N

tmp = x // K
x -= K * tmp

if x > abs(K - x):
    x = abs(K - x)

print(x)

Submission status: Accepted

Solution code:
import numpy as np
N,K = list(map(int, input().split()))
if N==K:
    N=0
if N>K:
    N=N%K

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

Submission status: Accepted

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

amari = n % k

if k/2 < amari:
  print(k - amari)
else:
  print(amari)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

(N, K) = (int(i) for i in input().split())

# N // K = NKを求める
NK = N // K

# N - K * NKかN - K * (NK + 1)の絶対値が小さいほうを出力
ans = min(abs(N - K * NK), abs(N - K * (NK + 1)))

print(ans)

Submission status: Accepted

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

n = N % K
n_ = abs(n - K)
if n < n_:
  print(n)
else:
  print(n_)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
syo,amari = divmod(n,m)
ans = m-amari
if ans >= n:
  ans = n
if amari == 0:
  ans = 0
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#Input
n, k = map(int, input().split())

#Process
mod = n % k
if(mod > abs(mod - k)):
    Ans = abs(mod - k)
else:
    Ans = mod

#Output
print(Ans)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
if a<b:
    c=b-a
    print(min(c,a))
else:
    a=a%b
    c=b-a
    print(min(c,a))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
N%=K
while True:
    N = abs(N-K)
    a = N
    N = abs(N-K)
    b = N
    N = abs(N-K)
    if a == N:
        print(min(a,b))
        break

Submission status: Accepted

Solution code:
x,y = map(int,input().split())

if x == y or x//2==y or y == 1:
    print(0)
else:
    print(min(abs(x%y-y),x%y))

Submission status: Accepted

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

x = 1000000000000000000
if N < K:
  x = min(N, K - N)
elif N == K:
  x = 0
else:
  m = N//K
  x = min((N - m * K),((m + 1) * K - N))

print(x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ans = N
if N < K:
    if N != 0:
        q, mod = divmod(K, N)
        ans = min(N, K - N)
    elif N == 0:
        ans = 0
elif N == K:
    ans = 0
else:
    q, mod = divmod(N, K)
    ans = min(mod, abs(K - mod))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

while n > abs(n - k):
    count = n // k
    n = min(abs(n - count * k), abs(n - (count+1) * k)) 
    
print(n)

Submission status: Accepted

Solution code:
n = list(map(int, input().split()))
k = n[0] % n[1]

if abs(k - n[1]) > k:
  print(k)
else:
  print(abs(k - n[1]))

Submission status: Accepted

Solution code:
# coding: utf-8
n, k = [int(x) for x in input().split()]
ans = 0
if k > n:
    if k - n < n:
        ans = k - n
    else:
        ans = n
else:
    t = n - k * (n // k)
    # print(t)
    # print(-(t-k))
    if t > -(t-k):
        ans = -(t-k)
    else:
        ans = t
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
m=int(N/K)

min_0 = N
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)
m=int(min_2/K)
min_3 = abs(min_2-m*K)

print(min(min_0, min_1, min_2, min_3))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


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

    if N == K:
        print(0)
    else:
        amari = N % K
        if amari == 0:
            print(0)
        elif abs(amari - K) > amari:
            print(amari)
        else:
            print(abs(amari - K))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
last = 0
if n > k:
    n %= k
while last + n != k:
    last = n
    n = abs(n-k)
print(min(last,n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n, k = list(map(int, input().split()))

x = n
x = x % k
while True:
  tmp = abs(x-k)
  if x > tmp:
    x = tmp
  else:
    break

print(x)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

a = N // K

suj1 = abs(N - a*K)
suj2 = abs(N - (a+1)*K)

print(min(suj1, suj2))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

def main():
    N, K = map(int, read().split())
    ans = N
    if N >= K:
        ans = N % K
    ans = min(ans, K - ans)
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N <= K:
    while abs(N - K) < N:
        N = abs(N - K)
else:
    N1 = N % K
    N2 = abs(N - int(N // K + 1) * K)
    N = N1 if N1 < N2 else N2

print(N)

Submission status: Accepted

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

x = N%K

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

# print(n, k)

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

# print(mod, a)

if (mod > a) :
  print(a)
else:
  print(mod)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
if N%K==0:
  print("0")
elif N<K and N<abs(N-K):
  print(N)
else:
  t=N//K
  if abs(N-t*K)<abs(N-(t+1)*K):
    print(abs(N-t*K))
  else:
    print(abs(N-(t+1)*K))

Submission status: Accepted

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

ans = []
t1 = int(N/K)
for i in range(-50,50):
    t = t1 + i
    ans.append(abs(N-K*t))
    
print(min(ans))

Submission status: Accepted

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

ans = 10 ** 18
if N == K:
    ans = 0
elif N < K:
    ans = min(N, abs(N - K))
elif N > K:
    ans = min(N % K, abs(N % K - K))

print(ans)

Submission status: Accepted

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

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

print(Ans)

Submission status: Accepted

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

Min = N%K
t = abs(Min-K)

print(min(Min,t))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
if(N < K):
  print(min(N, abs(N - K)))
  exit()
if(N % K == 0):
  print(0)
  exit()
print(min(N % K, abs((N % K) - K)))

Submission status: Accepted

Solution code:
# python3
# -*- coding: utf-8 -*-
import sys
import math
#from math import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint

INF = float('inf')

ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
sys.setrecursionlimit(1000000000)


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


# main
n,k = mis()

mod = n % k

mod_k = abs(mod - k)

if mod_k < mod:
    print(mod_k)
else:
    print(mod)

Submission status: Accepted

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

Submission status: Accepted

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

r =int(N%K)
l = abs(r-K)
print(min(r,l))

Submission status: Accepted

Solution code:
## input 
N,K = map(int,input().split())
remain = N % K
num = abs(remain - K)
#print(remain)
#print(num)

if num >= remain:
  print(remain)
else:
  print(num)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

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


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


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


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


N, K = inp_list()

mod = N % K
sho = N // K

if mod == 0:
    print(0)
else:
    ans = min(abs(sho * K - N), abs((sho + 1) * K - N))
    print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

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

while True:
    if N >= K:
        d, m = divmod(N, K)
        if m == 0:
            print(0)
            exit(0)
        N = N - (K * d)
    else:
        N = K - N
    if ans > N:
        ans = N
    else:
        break
print(ans)

Submission status: Accepted

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

mod = N%K

if K-mod<mod:
    print(K-mod)
else:
    print(mod)

Submission status: Accepted

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

a = N%K
if a == 0:
        print(0)
else:
        if a < K/2:
                print(a)
        else:
                print(K-a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())		# 5 7 2

m = n % k

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
while True:
    q, r = divmod(N, K)
    if q == 0:

        print(min(abs(N - K), N))
        break
    else:
        N = r
# print(N, K, divmod(N, K))

Submission status: Accepted

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

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

Submission status: Accepted

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

if n > k:
    print(min(n % k, abs(n % k - k)))
elif n == k:
    print(0)
elif n < k:
    print(min(n, k - n))

Submission status: Accepted

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

Submission status: Accepted

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

n -= k * (n // k)

old = n
mi = n

while True:
  answer = abs(n-k)
  mi = min(mi, answer)
  if old < answer:
    break
  old = answer
  n = answer

print(mi)

Submission status: Accepted

Solution code:
nk = input().split()

n = int(nk[0])
k = int(nk[1])

j = n%k

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

Submission status: Accepted

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

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

print(min(a, k-a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, abs(N%K - K), N%K))