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:
n,k=map(int,input().split())
if k==1:
  print(0)
else:
  a=n%k
  b=abs(a-k)
  print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
rr = range


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    #→1,.→0として読み込む
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '#' else 0 for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from fractions import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a * b // g


N, K = read_ints()
# Kは固定
# x=abs(x-K)
# x>Kであるならば、引き続けるのがよい
# 0付近のxを探してやればよい？

x = N
x1 = x % K  # 正
x2 = x1 - K  # 負

# x1が負より小さければいい？
print(min(x1, -x2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#x,y,z = list(map(int,input().split()))
n,k = list(map(int,input().split()))

n = n % k
f = True
ans = n
while f:
    t = abs(n-k)
    if t < ans:
        ans = t
        n = t
    else:
        f = False

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N = N%K

if N == 0:
  ans = 0
elif K-N <= N:
  ans = K-N
else:
  ans = N
print(ans)

Submission status: Accepted

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

ans_base = N % K

print(min(ans_base, K - ans_base))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C - Replacing Integer
# 標準入力の取得
N,K = list(map(int, input().split()))

# 求解処理
# "N % K"と"N % K"に対して操作を1回だけ行った際の値を比較
a = N % K
b = abs(a - K)
print(min(a, b))

Submission status: Accepted

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

Submission status: Accepted

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



print(min(n,k-n))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

left = n%k
right = k

dif = abs(k - n%k)

if left >= dif : 
    print(dif)
else : 
    print(left)

Submission status: Accepted

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

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

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


N,K = input_multiple_number()


N = N%K

N = min(N,K-N)

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

N = N%K

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(min((x[0]%x[1]),(abs((x[0]%x[1])-x[1]))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import math
import itertools
import statistics
#import numpy as np
import collections

#n = int(input())
n, k = list(map(int, input().split()))
a = n%k
b = a-k

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

Submission status: Accepted

Solution code:
import sys
from math import sqrt
from collections import Counter, defaultdict

input = sys.stdin.readline


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    mod = n % k
    if mod > abs(mod - k):
        print(abs(mod - k))
    else:
        print(mod)

    pass


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def ope(i, j):
    return abs(i - j)

appeared = set()

if n > k:
    c = n // k
    n -= k * c

while True:
    result = ope(n, k)
    if result in appeared:
        print(min(appeared))
        exit(0)
    else:
        appeared.add(result)
        n = result

Submission status: Accepted

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

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


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

Submission status: Accepted

Solution code:
def main():
    N, K = map( int, input().split())
    if N%K == 0:
        print(0)
        return
    print( min(N%K, K - N%K))
if __name__ == '__main__':
    main()

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())
q=N//K
a=min(abs(K*q-N),abs(K*(q+1)-N))
print(a)

Submission status: Accepted

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

n=n%k
n2=n%k+k
print(min(abs(n-k),n2-k))

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:
import sys

stdin = sys.stdin

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

n, k = nm()
x=n//k
n=n-(x-1)*k
while n>k/2:
    n=abs(n-k)
print(n)

Submission status: Accepted

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

if n > k:
    n = min(n%k, k-n%k)
else:
    n = min(n, abs(n-k)) #に置き換えられる

print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

n_af = n % k
if k - n_af <= n_af:
    n_af = k - n_af

print(n_af)

Submission status: Accepted

Solution code:
import string
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

N,K = nums()
x = N
m = N % K
print(min(m,abs(m-K)))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N, K = map(int, input().split())

	ans = 0

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

	print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if N % K == 0:
        print(0)
    else:
        d = gcd(N, K)
        n = N // d
        k = K // d
        cand = n % k
        print(d * min(cand, k - cand))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
inputt = lambda: range(int(input()))
inputi = lambda: map(int, input().split())
inputl = lambda: list(inputi())
from math import *
from heapq import *
from bisect import *
from functools import reduce, lru_cache
from collections import Counter, defaultdict
from builtins import *

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
#N = int(input())
#S = input()[:-1]
N,K = map(int,input().split())
#S,T = map(str,input().split())
#A = list(map(int,input().split()))
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
a = N % K
b = min(a,K-a)
print(b)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if(N <= K):
    ans = min(abs(N-K), abs(abs(N-K)-K))
    print(ans)
else:
    mod = N%K
    ans = min(abs(mod-K), mod)
    print(ans)

Submission status: Accepted

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

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

q, r = divmod(n, k)

if r > k - r:
    print(k-r)
else:
    print(r)

Submission status: Accepted

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

if n%k == 0:
    print(0)
    exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(a,b))

Submission status: Accepted

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

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

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:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

ans = 0
if N > K:
    ans = min(N % K, abs(K - N % K))
else:
    ans = min(N, abs(N-K))

print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))


n, k = load_list()


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
#import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, k = map(int, readline().split())
  a = n//k
  ans = min(n-a*k, abs(n-(a+1)*k))
  print(ans)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

min = n % k

if min > abs(min - k):
  min = abs(min - k)

print(min)

Submission status: Accepted

Solution code:
A,B=map(int,input().split())

X=A%B
print(min(X,B-X))

Submission status: Accepted

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

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

Submission status: Accepted

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

temp_res = N % K

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

x = N
if N > K:
	x = N % K

min_x = x
h = {}
h[x] = 1
while True:
	x = abs(x-K)
	min_x = min(min_x, x)
	if x in h:
		break
	h[x] = 1

print(min_x)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
temp1=n//x
temp2=n%x

temp3=x-temp2

ans=min(temp2,temp3)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

r=N%K

print(min(r,K-r))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

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

if N<=K:
    print(min(N, K-N))
else:
    M = N-(N-1)//K*K
    print(min(M, K-M))

Submission status: Accepted

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

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

Submission status: Accepted

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


def solve(N: int, K: int):
    if N % K == 0:
        print(0)
        return
    print(min(N, 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, k = map(int, input().split())
x = (n+k)%k
print(min(x, k-x))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n,m):
  p = n % m
  ans = min(p,m-p)
  return ans
l = [int(x) for x in input().split(' ')]
print(solve(l[0],l[1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    
    div = N // K
    min1 = abs(N - div*K)
    min2 = abs(N - (div+1)*K)

    print(min(min1,min2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
if n > k:
    n = n % k
m = n
lst = [n]
for i in range(1,100001):
    n = abs(n-k)
    m = min(m,n)
    lst.append(n)
    if i >= 2:
        if lst[i-2] == n:
            print(m)
            exit()

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N, K = iim()

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

d = N % K

print(min(d,K-d))

Submission status: Accepted

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

Submission status: Accepted

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

if N>K:
    tmp = (N-K)//K + 1
    N -= tmp*K

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

Submission status: Accepted

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

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

Submission status: Accepted

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

while True:
    anss=abs(val-k)
    if val<=anss:
        print(val)
        break
    val=anss

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if n > k:
    n = n % k

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

Submission status: Accepted

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

if n % k == 0:
    print("0")
else:
    sub = n % k
    sub2 = n // k
    sub3 = (k * (sub2 + 1)) - n
    print(min(sub, sub3))

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
  
def main():
  n,k = map(int,input().split())
  WANTS = n//k
  EX = n - k*WANTS
  dic = {}
  mi_ = float("inf")
  mi_ = min(mi_,EX)
  
  while True:
    if str(EX) in dic:
      break
    else:
      dic[str(EX)] = 1
      EX = abs(EX-k)
      mi_ = min(mi_,EX)
  print(mi_)
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n,k=map(int,input().split())
    a=n%k
    print(min(a,abs(a-k)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

x = N % K

if abs(x - K) > x:
    print(x)
else:
    print(abs(x - K))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, K = [int(x) for x in input().split()]
    m1 = N % K
    m2 = abs(m1 - K)
    if m1 < m2:
        print(m1)
    else:
        print(m2)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# いくつか入力
n,k = (int(i) for i in input().split())

a = n % k
c = k - a

print(min(a,c))

Submission status: Accepted

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

positive_sol = n % k

if positive_sol > k // 2:
    print(k - positive_sol)
else:
    print(positive_sol)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
#a = list(map(int, input().split()))
#s = input()
#ss= []
#for i in range(n):
#    ss.appned(int(input()))
print(min(n%k,k-(n%k)))

Submission status: Accepted

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

if n > k:
    res = min(abs(n - n//k * k), abs(n - k * (n//k + 1)))
elif n == k:
    res = 0
elif n < k:
    res = min(n , k - n)

print(res)

Submission status: Accepted

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

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

else:
    print(min(n % k, abs(n % k - k)))

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:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 21:12:41 2020

@author: Kanaru Sato
"""

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

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

Submission status: Accepted

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

if n > k:
    n = k + n % k

res = 10**19
while res > n:
    res = n
    n = abs(n - k)

print(res)

Submission status: Accepted

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

waru = N // K
N = N - waru*K

if abs(N-K)<N:
    print(abs(N-K))
else:
    print(N)

Submission status: Accepted

Solution code:
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(tmp)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq

#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

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

while True:
  if(abs(temp-K)<temp):
    temp = abs(temp-K)
  else:
    break

print(temp)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
t = 1
c = n
b = n//k
c = abs(c-k*b)
while t:
	c_p=c
	c = abs(c-k)
	if c > c_p:
		t = 0
print(c_p)

Submission status: Accepted

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

N %= K

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