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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc161/tasks/abc161_c
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())
if K > N :
  print(min(abs(N-K),N))
else:
  first = N // K 
  first_1 = N % K 
  print(min(first_1, K - first_1))

Submission status: Accepted

Solution code:
n, k = tuple(map(lambda x: int(x), input().split(" ")))
if k < n:
    n = n % k

if k / 2 < n:
    print(k-n)
else:
    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
 
N,K = list(map(int,input().split()))	

ans=N%K

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

Submission status: Accepted

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

print(min(plus_min, abs(plus_min - 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:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools
import heapq
#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n,k=map(int,input().split())
    a=[]
    a.append(n)
    ans=n
    while True:
        if n>=k:
            dif=n-k
            kai=dif//k
            n-=kai*k
            if n>=k:
                n=abs(n-k)
        if n>k:
            tempn=n
            aftn=abs(n-k)
            if tempn<=aftn:
                break
            else:
                n=aftn
                break
        else:
            tempn = n
            aftn = abs(n - k)
            if tempn <= aftn:
                break
            else:
                n=aftn
                break
    print(n)


if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,k = map(int,input().split())
if k > n:
    print(min(n,k-n))
else:
    print(min(n%k,k-(n%k)))

Submission status: Accepted

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

Submission status: Accepted

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

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

x = N % K
done_list = [x]
min_x = x
while True:
    x = abs(x - K)
    done_list.append(x)
    min_x = min(x, min_x)
    if x in done_list:
        break
ans = min_x

print(ans)

Submission status: Accepted

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

if N%K == 0:
  ans = 0
elif N%K != 0:
  _ = N%K
  __ = (K-_)
  ans = min(_,__)
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

p = n % k
q = k - p

if p >= q:
  print(q)
else:
  print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
nk=list(map(int, input().split()))
n=nk[0]
k=nk[1]
r=n%k
rd=-(r-k)
answer=min(r,rd)
print(answer)

Submission status: Accepted

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

ans1 = N%K
ans2 = -1 * (N % (-1 * K))

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = N % K # 余りに対して
if a < abs(a - K):
    print(str(a))
else:
    print(str(abs(a - K)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
  # import bisect
  # from collections import Counter
  # from collections import defaultdict
  # from collections import deque
  # from copy import deepcopy
  # from fractions import gcd
  # from heapq import heappop,heappush
  # from itertools import accumulate
  # from itertools import combinations
  # from itertools import permutations
  # from itertools import product
  # from math import ceil,floor, pi
  import sys
  sys.setrecursionlimit(10**6)
  readline = sys.stdin.readline
  ints = lambda: map(int, readline().split())
  floats = lambda: map(float, readline().split())
  pyes = lambda: print("Yes")
  pno = lambda: print("No")
  lli = lambda I, J: [[0] * J for _ in range(I)]
  mod = 10**9+7

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

a = N % K
b = abs(a - K)
ans = min(a, b)
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())
a=n%k
if n<k:
    print(min(n,abs(n-k)))
    exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

if N>K:
    num = N//K
    N = N - K*num
else:
    pass

spr = K//2 if K%2==0 else K//2+1
ans = N if N<spr else K-N
print(ans)

Submission status: Accepted

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

if a > b:
    tmp = a % b
    tmp2 = -1 * (tmp - b)
    if tmp > tmp2:
        print(tmp2)
    else:
        print(tmp)
elif a < b:
    tmp = -1 * (a - b)
    if a < tmp:
        print(a)
    else:
        print(tmp)
elif a == b:
    print(0)

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

Submission status: Accepted

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

n = N % K
n_another = K - n

ans = min(n, n_another)
print(ans)

Submission status: Accepted

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

temp1=a%b
temp2=abs(a%b-b)
print(min(temp1,temp2))

Submission status: Accepted

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

n %= k
temp = {n}
ans = n

while True:
    n = abs(n - k)
    if n in temp:
        break
    temp.add(n)
    # print(temp)

    if n < ans:
        ans = n

print(ans)

Submission status: Accepted

Solution code:
inp = list(map(lambda x: int(x), input().split(" ")))
N = inp[0]
K = inp[1]

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

Submission status: Accepted

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


if n >= k:
  tmp = n % k
else:
  tmp = n
if tmp > k/2:
  print(int(tmp-k)*(-1))
else:
  print(tmp)

Submission status: Accepted

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

Submission status: Accepted

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

if n < k:
    a = n
else:
    a = n % k

print(min([k - a, a]))

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    if n > k:
        m = n % k
        a = abs(m - k)
        b = abs(a - k)
        print(min(a, b))
        return
    else:
        a = abs(n - k)
        b = abs(a - k)
        print(min(a, b))



if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

# N = aK + b
b = N % K
while 2*b > K:
    b = K - b

print(b)

Submission status: Accepted

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

    ans_num = N % K

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

s = n % k

if s > k // 2:
    s = k - s

print(s)

Submission status: Accepted

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

if 2*n <= k:
    print(n)

elif n < k:
    print(k-n)

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

else:
    n = n%k
    if 2*n <= k:
        print(n)
    else:
        print(k-n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N, K = map(int, sys.stdin.readline().strip().split())

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

Submission status: Accepted

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

a = n

n %= k

b = n

n = abs(n-k) % k

c = n

print(min(a,b,c))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!python3.4.3

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

a = N // K

x = (N - a * K)

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

Submission status: Accepted

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

x = a % b
y = b - x

if x == 0 :
    print(0)
    
else:
    print(min([x,y]))

Submission status: Accepted

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

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

if(n==k):
    print(0)
elif(n>k):
    n=n-k*(n//k)
    print(min(n,abs(n-k)))
else:
    print(min(n,abs(n-k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
import fractions
import copy
import bisect
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
from operator import itemgetter
from sys import stdin
#input = sys.stdin.readline#こっちの方が入力が早いが使える時に使っていこう
from operator import mul
from functools import reduce
from collections import Counter


N=3
M=3
i=0
j=0
k=0
n=3
r=1
a=[0]*5
b=[]
#n=int(input())
#r=int(input())
#print(M)
#A=int(input())
#B=int(input())
#print(N)
"1行1つの整数を入力を取得し、整数と取得する"

N, K=map(int, input().split())
q=N//K
ans1=abs(N-K*(q+1))
ans2=abs(N-K*(q))
ans3=abs(N-K*(q-1))
print(min(ans1, ans2, ans3))
#number_list=list(map(int, input().split(" ")))#数字の時
#print(number_list)
"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#メモ
for i in number_list:#こっちの方がrage使うより早いらしい
    print(number_list[i-1])#

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''
#array=[input().split()for i in range(N)]
#print(type(array[0][0]))
#print(array)
"正方行列にstr型の値を入力"
'''

#brray=[list(map(int, input().split(" ")))for i in range(N)]
#print(type(brray[0][0]))
#print(brray)
'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"
#以下に別解を記載
#N, M = [int(i) for i in input().split()]
'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''




#s=input()
#a=[int(i) for i in s]
#print(a[0])
#print([a])
#単数値．桁ごとに分割したい．入力と出力は以下の通り
#イメージとして1桁ごとにリストに値を入れているかんじ

'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()#str style 何個でもいけることが多い
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''


#a= stdin.readline().rstrip()
#print(a.upper())
"aという変数に入っているものを大文字にして出力"


#a,b=map(int, input().split())  #int style 複数数値入力　「A B」みたいなスペース空いた入力のとき

#なんかうまく入力されるけど
#a=[[int(i) for i in 1.strip()]for 1 in sys.stdin]
#a = [[int(c) for c in l.strip()] for l in sys.stdin]]
#print(a)
#複数行の数値を入力して正方行列を作成


##############################################################################################
##############################################################################################
#under this line explains example calculation


#nCr combination
'''
def cmb(n,r):
    #When n < r , this function isn't valid
    r= min(n-r,r)
    #print(n,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    #flochart mul(n,n-1)=x
    #next     mul(x,n-2)........(n-r+1,n-r)
    #mul read a,b and returns a*b

    under = reduce(mul, range(1, r+1))
    #print(over, under)
    #reduce is applied mul(1,2)=2
    #next   mul(2,3)=6
    #next   mul(6,4)=4.........last(r!,r+1)=r+1!

    return over // under
    #// is integer divide
    #calc example 5C2
    #over=5*4*3
    #under=3*2*1
a = cmb(n, r)
#print(a)
'''

'''
#集計
#example
#a=[2,2,2,3,4,3,1,2,1,3,1,2,1,2,2,1,2,1]
#a=Counter(a)
for i in a.most_common(n):print(i)
#most_common()メソッドは、出現回数が多い要素順にCounterオブジェクトを並び替えます。
#引数にint型の数字nを設定した場合は、出現回数が高い上位ｎ個の要素を返します。
#何も設定しなければ、コンテナ型にあるすべての要素を出現回数の順番に並び替えたタプル型オブジェクトを返します。
#out put
#(2, 8)
 #(1, 6)
#(3, 3)
#(4, 1)
'''


#二部探索(binary search)
#A = [1, 2, 3, 3, 3, 4, 4, 6, 6, 6, 6]
#print(A)
#index = bisect.bisect_left(A, 5) # 7 最も左(前)の挿入箇所が返ってきている
#A.insert(index, 5)
#print(index)
#print(A)
'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''





########################################################################
########################################################################
#b2=a[:]  #1次元のときはコピーはこれで良い
#print(b2)

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b3=copy.deepcopy(a)  #2次元配列はこうコピーする
#print(b3)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = n
r = 0
x = n - (n // k) * k
pre_x = x
while True:
	if k == 1 or n == 0:
		break
	pre_x = x
	x = abs(x - k)
	if pre_x < x:
		r = pre_x
		break
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import *
N, K = map(int, stdin.readline().split())
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())
if n <= k:
  print(min(k-n, n))
else:
  print(min(n%k, abs(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:
#!/usr/bin/env python3

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

Submission status: Accepted

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

if N >= K:
    div = N // K
    diff = N - div * K
    print(min(diff, abs(diff-K)))
else:
    print(min(N, abs(N-K)))

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
N,K = (int(x) for x in input().split())
while(N>abs(N-K)):
    if(N > K):
        a = N//K
        N -= K *a
    else:
        N = abs(N-K)
print(N)

Submission status: Accepted

Solution code:
d = input().split(" ")
n = int(d[0])
k = int(d[1])
a = n % k
b = k - a
print(min([a, b]))

Submission status: Accepted

Solution code:
def solve(N, K):
    tmp = N % K

    if abs(tmp - K) < tmp:
        return abs(tmp - K)
    else:
        return tmp

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

plus_N = n - ((n//k)*k) 
minus_N = abs(n - ((n//k+1)*k))

print(min(plus_N,minus_N))

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

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-=k*(n//k)
ans=n
while True:
  tmp=abs(n-k)
  if tmp>=ans:
    break
  else:
    ans=min(ans,tmp)
    n=tmp
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = n%k
mn = x
for _ in range(2): # Should be 1 I think, but whatever
    x = abs(x - k)
    mn = min(x, mn)
print(mn)

Submission status: Accepted

Solution code:
N, K = [int(s) for s in input().split()]
if N > K:
  print(min(N%K, abs(K-N%K)))
elif N == K:
  print(0)
else:
  print(min(K-N, N))

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = [int(n) for n in 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
d = a % k

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

Submission status: Accepted

Solution code:
for _ in range(1):
	# n = int(input())
	n,k = map(int,input().split())
	# arr = list(map(int,input().split()))
	# s = input()
	print(min(-n%k,n%k))

Submission status: Accepted

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

cnt = n // k
a = n - k * cnt
b = abs(n - k * (cnt + 1))

print(min(a,b))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = (int(i) for i in input().split())
#B = [int(x) for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]

nc = N
for i in range(nc):
    if N < K:
        break
    else:
        N = N-(N//K)*K

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



print(N)

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

rem = n % k 

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    N, K = map(int, input().split())
    Norg = N

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

    if N > K:
        N = N % K

    if N == 0:
        print(0)
        exit()
    else:
        if N <= K // 2:
            print(N)
        else:
            print(abs(N - K))

    # print(Norg, K)
    # print(N, K)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def c1 ( N , K ) :
	if N == K :
		x = 0 ;
	elif K > N : 
		x = min ( N , abs ( N - K ) ) ; 
	else :
		x = c1 ( N % K , K ) ;
	return x ;

N , K   = map( int , input().strip().split(" ") ) ;
print ( c1 ( N , K ) ) ;

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    x = N % K
    print(min(x, abs(x - K)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

if k <= n:
  num = n//k
  n = n - k*num

temp = k - n
if n <= temp:
  ans = n
elif temp < n:
  ans = temp

print(ans)

Submission status: Accepted

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

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

m1 = abs(m0 - k)
m2 = abs(m1 - k)

print(min(m0,m1,m2))

Submission status: Accepted

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

if R <= K / 2:
  Ans = R
else:
  Ans = K - R
  
print(Ans)

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
    N, K = mulinputs()
    d = (N+K-1)//K

    # 出力
    ans = int(abs(N-d*K))
    if N < ans:
        print(N)
    else:
        print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(N)

Submission status: Accepted

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

r = n % k

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

Submission status: Accepted

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


def solve(N: int, K: int):
    q = N // K
    r = N % K
    count = q 
    if abs(r - K) < r:
        r = abs(r - K)
    print(r)
    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:
# coding: utf-8


def solve(*args: str) -> str:
    n, k = map(int, args[0].split())

    return str(min(n % k, abs(n % k-k)))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if N < K:
    print(min(abs(N-K),N))
    sys.exit()
if N % K == 0:
    print(0)
else:
    print(min(abs((N%K)-K),N%K))

Submission status: Accepted

Solution code:
# Code for C - Replacing Integer
# Use input() to fetch data from STDIN
[n, k] = map(int, input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a=N%K

b=abs(K-a)

print(min(a,b))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
m=n//k
n=n-k*m
if 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(a-k)
print(min(a,b))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def sousa(x, k):
    return abs(x-k)

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

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
  _n, _k = map(int, input().split())
  candidate = _n % _k
  ans = min(candidate, abs(candidate-_k))
  print(int(ans))