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

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

Submission status: Accepted

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

c=n%m

if c<abs(c-m):
  print(c)
else:
  print(abs(c-m))

Submission status: Accepted

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

if n>=k:
    n%=k

ans=min(n,k-n)

print(ans)

Submission status: Accepted

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

tmp = N // K

ans = N - K * tmp

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

if K-a<=a:
    print(K-a)
else:
    print(a)

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:
n,m=map(int,input().split())
s=n-((n//m)*m)
c=abs(s-m)
print(min(s,c))

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())
cad = N // K
n = N - K * cad
m = K * (cad + 1) - N
print(min(n, 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:
N, K = map(int,input().split())

rem = N % K
res = K - rem

result = [rem, res]

print(min(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N, K = MI()
    a = N % K
    b = K - a
    print(min(a, b))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,K=mi()
    
    n = N//K
    ans = INF
    for m in [n-1,n,n+1]:
        ans = min(ans,abs(N-K*m))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(abs(a%b-b))

Submission status: Accepted

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

Submission status: Accepted

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

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:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

tmp = n//k

a = abs(n-k*tmp)
b = abs(a-k)

print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if n < k:
    ans = min(n, abs(n - k))
else:
    q = n // k
    ans = min(abs(n - (q * k)), abs(n - (q * k) - k))

print(ans)

Submission status: Accepted

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

while True:
    if abs(N - K) >= N:
        break
    else:
        if N >= K:
            N = N - (N // K) * K
        else:
            N = N - (N // (2 * N - K)) * (2 * N - K)

print(N)

Submission status: Accepted

Solution code:
def solve():
    N, K = list(map(int, input().split()))
    if(N >= K):
        tmp = N - ((N // K) * K)
        ans = min(tmp, abs(abs(tmp) - K))
    elif(N < K):
        if(K <= 2*N):
            ans = abs(N - K)
        else:
            ans = abs(N)
    print(ans)

if __name__ == "__main__":
    solve()

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

Submission status: Accepted

Solution code:
# coding: utf-8
def main():
    n, k = map(int, input().split())
    if n == k:
        return 0
    elif n < k:
        tmp = abs(n - k)
        if tmp < n:
            return tmp
        else:
            return n
    else:
        tmp = n // k
        n -= k * tmp
        tmp = abs(n - k)
        if tmp < n:
            return tmp
        else:
            return n
            
    
print(main())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
n = (n - (n//k-1)*k) if n > k else n
n_min = 0
while n_min <= n:
    n_min = abs(n-k)
    if n_min >= n and n < k:
        print(min(n_min, n))
        break
    n = n_min

Submission status: Accepted

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

if amari>k:
  print(k)
else:
  print(min(amari,abs(amari-k)))

Submission status: Accepted

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

Submission status: Accepted

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

if n % k == 0:
  print(0)
else: 
  ans = n % k
  #print(ans)
  while ans > k//2:
    n = abs(ans - k)
    ans = min(n, ans)
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = list(map(lambda n: int(n), input().split(" ")))
 
while N > int(K/2):
  N = min([abs(N-K), N % K])

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N,K = [int(i) for i in input().split()]
    min_N = N
    if min_N > K:
        min_N = min_N - (min_N//K)*K
    min_N = min(min_N, abs(min_N-K))
    print(min_N)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

N = N % K

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

Submission status: Accepted

Solution code:
def repint(n, k):
	if n % k == 0:
		return 0
	res = abs((n % k) - k)
	if (n % k) > res:
		return res
	return (n % k)
n, k = map(int, input().split())
print(repint(n, k))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,k = readInts()
print(min(n%k,k - (n%k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
v = N//K
print(min(N-v*K, K-(N-v*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())
m = N%K
print(min(m,K-m))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

Submission status: Accepted

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

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

mod = N%K

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

Submission status: Accepted

Solution code:
import sys
N, K = [int(x) for x in input().split()]
if(N>K):
  ans = N % K
else:
  ans = N
while True:
  n_ans = abs(ans - K)
  if(ans<n_ans):
    print(ans)
    sys.exit()
  else:
    ans = n_ans

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
ok=0
n=n%k
def f(x):
    if abs(x-k)<x:
        f(abs(x-k))
    else:
        print(x)
f(n)

Submission status: Accepted

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

Submission status: Accepted

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

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

x=N%K

if x ==0:
    print(0)
elif abs(x - K) > x:
    print(x)
else:
    print(abs(x - K))

Submission status: Accepted

Solution code:
import sys
import math

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

# n_start = math.floor(n / k)
# # print(n_start)
# n = n - n_start*k
# # print(n)

n = n % k

while True:
    min_n = n
    n = abs(n-k)
    # print("n, k =", n, k)
    if min_n > n:
        min_n = n
    else:
        break
print(min_n)

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

for i in range(N):
    if N % K == 0:
        N = 0
        break
    N = N % K
    if abs(N-K) < N:
        N = abs(N-K)
    else :
        break
print(N)

Submission status: Accepted

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

minimum = x = N % K

cache = set()
while True:
    x = abs(x - K)
    minimum = min(x, minimum)
    if x in cache:
        break
    else:
        cache.add(x)

print(minimum)

Submission status: Accepted

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

q_1 = n % k
q_2 = k - q_1
if q_1 < q_2:
  print(q_1)
else:
  print(q_2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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())
a = N%K

print(min(a,K-a))

Submission status: Accepted

Solution code:
# coding: utf-8
n, k = map(int, input().split())
x1 = n % k
x2 = k - x1
if x1 < x2:
    print(x1)
else:
    print(x2)

Submission status: Accepted

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

S=[]
n1=n%k
S.append(n)
S.append(n1)
for i in range(10):
    n1=abs(n1-k)
    S.append(n1)
  
# print( n, n1, S )
print( min(S) )

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    r1 = n % k
    r2 = abs(r1 - k)
    r = min(r1, r2)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

amari = score[0] % score[1]
kouho = score[1] - amari

if amari < kouho:
    print(amari)
else:
    print(kouho)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if (k == 1):
    print(0)
    sys.exit()

ans = 0
v_ans = 0
if (n > k):
    v_ans = n % k
else:
    v_ans = n

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

print(ans)

Submission status: Accepted

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

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

ans = N

if N ==M or N%M ==0:
    print(0)

elif N < M:
    if abs(ans-M) > ans:
        print(ans)
    else:
        print(abs(ans-M))

else:
    print(min(N % M,abs((N % M) - M)))

Submission status: Accepted

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

a = N//K
b = abs(N-(a+1)*K)

if N - a*K <= b:
  print(N-a*K)
elif N - a*K > b:
  print(b)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main(n,k):
    if n>k:
        n=n%k+k
        n=min(n,abs(n-k))
    return min(n,abs(n-k))

if __name__=='__main__':
    n,k=readinput()
    ans=main(n,k)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

while N > K:
    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())
print(min(n % k, k - n%k))

Submission status: Accepted

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

if N == 0 or K == 1:
    N = 0
elif N == K:
    N = 0
elif N % K == 0:
    N = 0
else:
    N = N % K
    while N != 0:
        m = abs(N - K)
        if N < m:
            break
        else:
            N = m

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

t = N % K

res = min(t, K - t)

print(res)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n, k = map(int, sys.stdin.buffer.read().split())
rem = n % k
print(min(rem, k - rem))

Submission status: Accepted

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

n = N%K
m = K - n

print(min(abs(n), abs(m)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

N,k = inpm()

def solve(n):
    if n <= k:
        return min(n,k-n)
    else:
        return solve(n % k)

ans = solve(N)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
def replacing_integer():
    # 入力
    N, K = map(int, input().split())
    # 処理
    now_num = N % K
    while True:
        if abs(now_num - K) <= now_num:
            now_num = abs(now_num - K)
        else:
            return now_num

result = replacing_integer()
print(result)

Submission status: Accepted

Solution code:
# abc161_c.py
# https://atcoder.jp/contests/abc161/tasks/abc161_c

# C - Replacing Integer /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 青木君は任意の整数 xに対し、以下の操作を行うことができます。
# 操作: xを x と Kの差の絶対値で置き換える。
# 整数 Nの初期値が与えられます。この整数に上記の操作を 0 回以上好きな回数行った時にとりうる Nの最小値を求めてください。

# 制約
#     0≤N≤1018
#     1≤K≤1018
#     入力は全て整数

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N K

# 出力
# 操作を 0回以上好きな回数行った時にとりうる Nの最小値を出力せよ。

# 入力例 1
# 7 4

# 出力例 1
# 1

# 最初、 N=7です。
# 1回操作を行うと、N は |7−4|=3となります。
# 2回操作を行うと、N は |3−4|=1となり、これが最小です。

# 入力例 2
# 2 6

# 出力例 2
# 2

# 1回も操作を行わなかった場合の N=2が最小です。

# 入力例 3
# 1000000000000000000 1

# 出力例 3
# 0


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    # N = int(lines[0])
    N, K = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    tmp = N % K
    result = min(tmp, K-tmp)

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['7 4']
        lines_export = [1]
    if pattern == 2:
        lines_input = ['2 6']
        lines_export = [2]
    if pattern == 3:
        lines_input = ['1000000000000000000 1']
        lines_export = [0]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

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

# flag = True
# while flag:
#   if N > K:
#     N = abs(N-K)
#   else:
#     ans = N if N <= N -K else N-K
#     flag = False

Submission status: Accepted

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


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

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


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import itertools
import math
import sys

INF = float('inf')


def solve(N: int, K: int):
    return min(N % K, abs(N % K - K))


def main():

    sys.setrecursionlimit(10 ** 6)

    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
    print(f'{solve(N, K)}')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

div = N//K

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
from functools import reduce

n,k = [int(x) for x in stdin.readline().rstrip().split()]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = (int(x) for x in input().split())
p = N % K
if p < (K-p):
    print(p)
else:
    print(K-p)

Submission status: Accepted

Solution code:
inputValues = [int(i) for i in input().split()]
n = inputValues[0]
k = inputValues[1]
res = n%k
res = min(k-res,res)
print(res)

Submission status: Accepted

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

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
line = sys.stdin.readlines()[0]
n, k = [int(s) for s in line.split(" ")]
t = n // k
a1 = abs(n - (k * t))
a2 = abs(n - k * (t + 1))
answer = min(a1, a2)
print(answer)

Submission status: Accepted

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

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

remainder = N % K

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

Submission status: Accepted

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

num_count = (N-N%K)//K
ans = min(abs(N - num_count*K), abs(N - (num_count+1)*K))
if num_count > 0:
    ans = min(abs(N - (num_count-1)*K), ans)
print(ans)

Submission status: Accepted

Solution code:
l=input("").split(" ")
n=int(l[0])
k=int(l[1])
s=n%k
ss=k-s
if(s<ss):
    print(s)
else:
    print(ss)

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
t=N%K
print(min(t,abs(t-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 = map(int, input().split())
if N % K == 0:
    print(0)
elif N > K:
    hako1 = N%K
    hako2 = abs(hako1-K)
    if hako1 > hako2:
      print(hako2)
    else:
      print(hako1)
else:
    if N >= abs(N-K):
        print(abs(N-K))
    else:
        print(N)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
N = N % K
if N - K <= 0:
    M = K - N
else:
    M = N - K
print(min(M, 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 %= k

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

Submission status: Accepted

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

if n > 2*k :
    n = n%(2*k)

while True:
    mae = n
    n = abs(k-n)
    if mae <= n:
        break

print(mae)

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,k = map(int,input().split())
v = n//k
ans = float('inf')
for i in (1,-1):
    for j in (1,-1):
        for l in (v,v+1):
            ans = min(ans,abs(i*n+j*l*k))
print(ans)

Submission status: Accepted

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

ans=n % k

if abs(ans-k)<ans:
    print(abs(ans-k))
else:
    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:
n,k=map(int,input().split())
while n>k:
  n=n%k
if n>k//2:
  n=abs(n-k)
print(n)

Submission status: Accepted

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


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

Submission status: Accepted

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