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:
# -*- coding: utf-8 -*-
import numpy as np

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

if n > k:
    tmp = n % k
    if tmp >= k - tmp:
        ans = k - tmp
    else:
        ans = tmp
elif n < k:
    if n >= k - n:
        ans = k - n
    else:
        ans = n
else:
    ans = 0

print(ans)

Submission status: Accepted

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

if N % K == 0:
    print(0)
else:
    N = N % K
    print(min([N, abs(N-K)]))

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def solve():
    n,k = map(int,input().split())
    if n>k:
        print(min(n%k,k-n%k))
    else:
        print(min(n,abs(n-k)))
if __name__=='__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

t = N % K
t2 = K - t

if t > t2:
  print(t2)
else:
  print(t)

Submission status: Accepted

Solution code:
a=input().split(" ")
n=int(a[0])
k=int(a[1])
s=n%k
t=k-s
if s>=t:
    print(t)
else:
    print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#スペース区切りの入力
N, K = (int(x) for x in input().split())
# 変換
n = abs(N)
k = abs(K)

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

Submission status: Accepted

Solution code:
class atcorder:
    
    x,K=map(int,input().split())
    
    if x>K:
        ans=x%K
    else:
        ans=x
        
    print(min(ans,abs(ans-K)))

Submission status: Accepted

Solution code:
# coding: utf-8
DATA = """
1000000000000000000 1
""".split("\n")

def get_debug_line():
	for i in DATA:
		yield i

if 1:
	get_line = input
else:
	get_line = get_debug_line().__next__
	get_line()

N, K = [int(i) for i in get_line().strip().split(" ")]

N %= K
_min = N
while(1):
	if N > K:
		N -= K
	else:
		N = K - N
	
	if _min == N:
		break

	if N < _min:
		_min = N

print (_min)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
ans = N%M
while True:
   if abs(ans-M) < ans:
        ans = abs(ans-M)
   else:
    print(ans)
    sys.exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=[int(_) for _ in input().split()]
d=n//k
print(min(abs(n-k*d), abs(n-k*(d+1))))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans1 = n % k
ans2 = abs(ans1 - k)

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

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

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

Submission status: Accepted

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

A = N % K
B = K - N % K

print(min(A,B))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n%=k
print(min(n,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 abs(n%k) == 0:
  ans = 0
else:
  while n > k:
    n = abs(n - (n // k) * k)
  ans = min(abs(n-k),abs(abs(n-k)-n),n)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Nmin=K+(N%K)
while Nmin>abs(Nmin-K):
    Nmin=max(Nmin-K,K-Nmin)

print(Nmin)

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:
from __future__ import print_function

import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd
# fractions.gcd() を用いること
# math.log2()はatcoderでは対応していない．留意せよ．

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / gcd(n, m))


def coprimize(p, q):
    common = fractions.gcd(p, q)
    return (p // common, q // common)


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#C again

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

if abs(k-ans) < ans:
    ans = abs(k-ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import sys
# input = sys.stdin.readline
sys.setrecursionlimit(100000)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7

#N = int(input())
#A = [int(x) for x in input().split()]
#V = [[0] * 100 for _ in range(100)]

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

def f(x):
    ret = x % K
    ret = min(ret, abs(K-ret))
    return ret
    
print(f(N))

Submission status: Accepted

Solution code:
N ,K =map(int,input().split()) 
if N%K ==0:
    ans = 0
elif K%N ==0:
    ans = N
elif K>N:
    ans =min(N,K-N)
elif N>K:
    ans =min(N%K,-(N%K-K))
print(ans)

Submission status: Accepted

Solution code:

def main():
    N, K = map(int, input().split(' '))
    
    # out = min(float('inf'), N)
    prev = N % K
    
    isFinish = False
    
    if prev == 0:
        print(0)
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next >= prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


def run():
    x, k = map(int, input().split())
    x = x % k
    while True:
        _x = abs(x - k)
        if x < _x:
            break
        x = _x
    print(x)


if __name__ == '__main__':
    run()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = N // K
b = abs(N - (K*a))
c = abs(N - (K*(a+1)))
print(min(b, c))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def myAnswer(N:int, K:int) -> int:
   mod = N % K
   return min(mod,abs(mod - K))
   

def modelAnswer():
   tmp=1
def main():
   N,K = map(int,input().split())
   print(myAnswer(N,K))

if __name__ == '__main__':
   main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
data = input().split()
n = int(data[0])
k = int(data[1])
 
if n > k :
  res = min(k - n % k, n % k)
else:
  res = min(abs(n - k), n, k - n % k)

print(res)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N,K=map(int,readline().split())
    a=N%K
    b=abs(a-K)
    print(min(a,b))
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
ans=max(N,K)
for i in range(10000):
    N=N%K
    ans=min(ans,N)
    N=abs(N-K)
    ans=min(ans,N)
print(ans)

Submission status: Accepted

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

# In[10]:


N,K = map(int,input().split())
new = N % K
n2 = K - new

print(min(new,n2))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
n_k = [int(i) for i in input().split()]
n = n_k[0]
k = n_k[1]
r = n % k
r_1 = abs(r - k)
min = np.array([r, r_1]).min()
print(min)

Submission status: Accepted

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

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

res = 0
if n % K > abs((n % K) - K):
    res = abs((n % K) - K)
else:
    res = n % K
    
print (res)

Submission status: Accepted

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

r=N%K
if r==0:print(0)
elif r<math.fabs(r-K):print(int(r))
else:print(int(math.fabs(r-K)))

Submission status: Accepted

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

Submission status: Accepted

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

n1 = n % k
n2 = k - n1
print(n1 if n1 < n2 else n2)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

candidate1 = N % K
candidate2 = K - candidate1
answer = min(candidate1, candidate2)

print(answer)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
ans = 0
while True:
    if n > k:
        i = n // k + 1
        n = abs(n - k*i)
        continue
    elif n == k:
        ans = 0
        break
    elif n < k:
        if abs(n - k) > n:
            ans = n
            break
        else:
            ans = k - n
            break

print(ans)

Submission status: Accepted

Solution code:
#x>Kの時、x => x-k =>x-2k => x-3k ... : xをkで割った余りになる
#x<Kの時、x　=> k-x => x : max(x,k-x)
n,k = map(int,input().split())
if n >= k:
    n = n%k
print(n if n%k <= k-n else k-n)

Submission status: Accepted

Solution code:
def ans(n, k):
    if n <= k:
        return min(n,k-n)
    d = (n-k-1)//k+1
    return ans(n-d*k,k)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split(' ')]
d = n // k
ne = n - d * k
if abs(ne - k) < ne:
  print(abs(ne - k))
else:
  print(ne)

Submission status: Accepted

Solution code:
#/usr/bin/env python

import sys

N, K = [int(x) for x in sys.stdin.readline().strip().split()]

mod = N % K

if mod < (K / 2.):
    print(mod)
else:
    print(K - mod)

Submission status: Accepted

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

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

Submission status: Accepted

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

rem = n % k

print(min(rem, k - rem))

Submission status: Accepted

Solution code:
import math

N , K = map(int, input().split())
flag = "F"
if N !=0:
    while 1:
        lenN = int (math.log10(N) + 1)
        lenK = int (math.log10(K) + 1)
        N = abs(N-K*(10**abs(lenN-lenK-1)))
        lenN = int (math.log10(N) + 1)
        if abs(lenN-lenK)<2:
            break



        
while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Accepted

Solution code:
val= input()
val_list=list(map(int,val.split()))
n=val_list[0]
k=val_list[1]
n= n % k
judge=[]
while True:
    n = abs(n-k)
    if n in judge:
        break
    else:
        judge.append(n)
min= judge[0]
for i in range(0,len(judge)):
    if min > judge[i]:
       min= judge[i]

print(min)

Submission status: Accepted

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

if(n == k):
    result = 0
elif (n > k):
    result = n % k
    result = min(result, abs(result - k))
else:
    result = min(n, k-n)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int, input().split())

if a > b:
    if a % b == 0:
        print(0)
        exit()
    else:
        print(min(a%b, b-(a%b)))
        exit()

while True:
    x = abs(a-b)
    if x < a:
        a = x
    else:
        print(a)
        break

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
a = N % K
b = K - a
print(min([a, b]))

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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
p = N%K
if p < K-p:
  print(p)
else:
  print(K-p)

Submission status: Accepted

Solution code:
num=list(map(int, input().split()))
if num[0]%num[1]<=num[1]-(num[0]%num[1]):
  ans=num[0]%num[1]
else:
  ans=num[1]-(num[0]%num[1])
print(ans)

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 = map(int,input().split())
r = N%K
s = K-r
print(min([r,s]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inte, sub = map(int, input().split(" "))
inte = inte % sub
t = [inte]
count = 0
while True:
    ab = abs(inte - sub)
    t.append(ab)
    if t[count] <= t[count + 1]:
        print(t[count])
        break
    else:
        count += 1

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
m=a%b
if abs(b-m)<m:
    m=abs(b-m)
print(m)

Submission status: Accepted

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

remainder = n%k
if remainder > k/2:
    ans = k-remainder
else:
    ans = remainder

print(ans)

Submission status: Accepted

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

input_line = input()
inputs = input_line.split(' ')
N, K = [int(s) for s in inputs]

c1 = (N%K)
c2 = abs(c1-K)

print(min(c1,c2))

Submission status: Accepted

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

Submission status: Accepted

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

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return abs(N - K) if abs(N - K) < abs(abs(N-K) - K) else abs(abs(N-K) - K)

print(res())

Submission status: Accepted

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

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 = [int(x) for x in input().split()]

N_1 = N % K
N_2 = K - N_1

print(min(N_1, N_2))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(" ")))
n = n - k * (n // k)
n_last = abs(n - k)
if n_last < n:
    n = n_last
print(n)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
list_input = [int(n) for n in input().split()]

N = list_input[0]
K = list_input[1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N=N%K
        if M==N:
            M = N
            N-=K
            if N<=0:
                if abs(N)<abs(M):
                    print(abs(N))
                    break
                else:
                    print(abs(M))
                    break

Submission status: Accepted

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

Submission status: Accepted

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

x = N-(N//K)*K
print(min(abs(x-K), x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())


z = a % b


if z > b/2:
    print(abs(z-b))
else:
    print(z)

Submission status: Accepted

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

resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = list(map(int,input().split()))
mod = A[0]%A[1]
minus = abs(mod-A[1])
if mod < minus:
  print (mod)
else:
  print (minus)

Submission status: Accepted

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

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

while x > xi:
    x=xi
    xi=abs(x-k)

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#minimum value
value, deno = map(int,input().split())
if value%deno == 0:
    print(0)
else:
    print(min(value%deno, abs((value%deno) - deno)))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a, b = map(int, input().split(" "))

x = a%b
y = b - x

print(min([x,y]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Problem C - Replacing Integer

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

# initialization
tmp_1 = N % K
tmp_2 = abs(tmp_1 - K)
ans = min(tmp_1, tmp_2)

# output
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


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


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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min(a,b))

Submission status: Accepted

Solution code:
#author : dokueki

import sys
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n, k = map(int, sys.stdin.readline().split())
    if n % k == 0:
        print(0)
    elif n < k:
        print(min(abs(n - k), n))
    else:
        a = n // k
        f, s = (a + 1) * k, a * k
        print(min(abs(f-n), abs(s-n)))


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Submission status: Accepted

Solution code:
def Main(N, K):
    if K == 1:
        return 0
    mo = N%K
    if K//2 <= mo:
        return K-mo
    else:
        return mo
         
    

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

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

Submission status: Accepted

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

Submission status: Accepted

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

#余りを計算　n > k
minimum = n % k

# n < k を計算　n=51, k=100を考える
while n%k > k-n%k:
    minimum = k - n % k 
    n = k - n % k  

print(minimum)

Submission status: Accepted

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

if k - n < min:
  min = k-n

print(min)

Submission status: Accepted

Solution code:
N, K = map(int, open(0).read().split())
a = N % K
print(min(K - a, a))

Submission status: Accepted

Solution code:
list_NK = list(map(int, input().split())) 
N = list_NK[0]
K = list_NK[1]

number = N - K*(N//K)
abs_now  = number 
while all:
    abs_past = abs(abs_now - K)
    if(abs_past >= abs_now):
        break
    abs_now = abs_past
print(abs_now)

Submission status: Accepted

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

minimum=N%K
while N%K>K-N%K:
    minimum=K-N%K
    N=K-N%K
print(minimum)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

while True:
    tmp = N % K
    if tmp >= N:
        if tmp > (abs(tmp - K)):
            N = abs(tmp - K)
        break
    N = tmp
print(N)

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, m = map(int, input().split())
l = []
ans = 1
if(n%m == 0):
  ans=0
else:
  for x in (0,1000000000000000000):
    n = abs(n%m)
    l.append(n)
  l.sort()
  ans = min(m-n,l[0])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
tmp = N%K
a = K-tmp
b = tmp
if a < b:
  print(a)
elif a > b:
  print(b)
else:
  print(a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
 
if __name__ == "__main__":
    N, K = map(int, input().split())    
    ret =  N - N // K *K
    if ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = list(map(lambda x:int(x), input().split()))
n = a[0]%a[1]
k = a[1]

print(min(n, k-n))

Submission status: Accepted

Solution code:
import math
def I(): return(list(map(int,input().split())))
n,m=I()
print(min(n%m,m-n%m))

Submission status: Accepted

Solution code:
import sys
# 未解決(最後だけREになる)


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


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - c))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs((a[0] % a[1]) - a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs((a[0] % a[1]) - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1]) - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(((a[0] % a[1]) - a[1]) % a[1]))
                sys.exit(0)