AtCoder Beginner Contest 161 - Replacing Integer

Given any integer x, Aoki can do the operation below.
Operation: Replace x with the absolute difference of x and K.
You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

-----Constraints-----
 - 0 ≤ N ≤ 10^{18}
 - 1 ≤ K ≤ 10^{18}
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K

-----Output-----
Print the minimum possible value taken by N after Aoki does the operation zero or more times.

-----Sample Input-----
7 4

-----Sample Output-----
1

Initially, N=7.
After one operation, N becomes |7-4| = 3.
After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Difficulty: EASY

Starter code:

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


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


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


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


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


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


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


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


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


(N, K) = map(int, input().split())
result = N % K
if result <= abs(result - K):
	print(result)
elif result > abs(result - K):
	result = abs(result - K)
	print(result)


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


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


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


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


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


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


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


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


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


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


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


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


(n, m) = map(int, input().split())
x = n % m
c = min(x, m - x)
print(c)


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


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


import sys
(n, k) = list(map(int, input().split()))
ama = n % k
ama_k = abs(ama - k)
if n % k == 0:
	print('0')
	return
elif ama >= ama_k:
	print(ama_k)
	return
elif ama <= ama_k:
	print(ama)
	return


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


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


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


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


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


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


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


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


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


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


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


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


(N, K) = list(map(int, input().split()))
mod1 = N % K
mod2 = K - mod1
print(mod1 if mod1 < mod2 else mod2)


(N, k) = map(int, input().split())
X = N // k
print(min(N - k * X, k * (X + 1) - N))


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


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


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


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

class Solution:

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

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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


(N, K) = list(map(int, input().split()))
P = N % K
A = [P, K - P]
if A[0] <= A[1]:
	print(A[0])
elif A[0] > A[1]:
	print(A[1])


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


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


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


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


(N, K) = list(map(int, input().split()))
ans = 0
if N % K == 0:
	ans = 0
else:
	a = N % K
	b = abs(a - K)
	ans = min(a, b)
print(ans)


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


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


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

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


[N, K] = list(map(int, input().split(' ')))
N = N % K
nN = abs(N - K)
while nN < N:
	N = nN
	nN = abs(N - K)
print(N)


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


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


(n, k) = list(map(int, input().split()))
if n == k:
	print(0)
	return
elif n > k:
	n = n % k
m = k - n
print(min(n, m))


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


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


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


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


(N, k) = [int(x) for x in input().split()]
print(min(N % k, k - N % k))


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


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


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


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


(N, K) = list(map(int, input().split()))
if N == K:
	print(0)
elif N < K:
	lis1 = [N, abs(N - K)]
	print(min(lis1))
else:
	lis2 = [N % K, abs(N % K - K)]
	print(min(lis2))


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


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


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


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

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

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

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

def i_row_list(N):
	return [list(map(int, input().split())) for _ in range(N)]
(n, k) = i_map()
print(min(n % k, k - n % k))


(n, k) = [int(i) for i in input().split()]
print(min(n % k, k - n % k))


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


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


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


import sys

def __starting_point():
	(N, K) = map(int, input().split())
	if N % K == 0:
		print('0')
		return
	N = N % K
	while N > abs(N - K):
		N = abs(N - K)
	print(N)
__starting_point()


(N, K) = map(int, input().split())
L = []
for i in range(10 ** 5):
	M = N // K
	if i == 0:
		N = abs(N - K)
		L.append(N)
	elif N > K:
		N = N - K * M
		L.append(N)
	else:
		N = abs(L[i - 1] - K)
		L.append(N)
print(min(L))


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


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


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


(N, K) = list(map(int, input().split()))
if N == 0:
	print(N)
else:
	div = N // K
	print(min(N - div * K, (div + 1) * K - N))


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


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


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


def N():
	return int(input())

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

def NL(n):
	return [list(map(int, input().split())) for i in range(n)]
mod = pow(10, 9) + 7
import sys
import math
import collections
(n, m) = L()
print(min(n % m, -n % m))


(n, k) = map(int, open(0).read().split())
print(min(n % k, k - n % k))


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


(x, y) = map(int, input().split())
z = x % y
p = y - z
if z < p:
	print(z)
else:
	print(p)


(n, k) = [int(x) for x in input().split()]
print(min(n % k, k - n % k))


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

def absolute(x):
	x = x - K
	if x < 0:
		x = -x
	return x
if N < K:
	while N > abs(N - K):
		N = absolute(N)
	print(N)
elif N % K == 0:
	print(0)
else:
	b = 1
	s = K
	while b > 0 and N > abs(N - s):
		b = N % K
		N = K
		a = K
		K = b
	print(a)


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


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


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


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


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

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

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

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

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

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	(N, K) = MI()
	num = N // K
	ans = min(abs(N - K * num), abs(N - K * (num + 1)))
	print(ans)

def __starting_point():
	main()
__starting_point()


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


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

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


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


(N, K) = list(map(int, input().split()))
x = N
pre_x = x
if x // K > 0:
	x = abs(x - K * (x // K))
	pre_x = x
while True:
	pre_x = x
	x = abs(x - K)
	if pre_x < x:
		print(pre_x)
		return


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


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


(x, y) = map(int, input().split())
print(min(x % y, abs(x % y - y)))


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


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


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


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


(A, B) = map(int, input().split())
X = A % B
print(min(X, B - X))


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
(n, k) = [int(i) for i in input().split()]
tmp = 0
res = 0
n = n % k
res = min(n, abs(n - k))
print(res)