Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

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

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def isOK(A):
    A.sort()
    for i in range(len(A) - 1):
        if A[i] + 1 != A[i + 1]:
            return False
    return True


def solve():
    N, K = Scanner.map_int()
    print(min(N % K, K - N % K))


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def examA():
    X, Y, Z = LI()
    X, Y = Y,X
    X,Z = Z, X
    print(X,Y,Z)
    return

def examB():
    N, M = LI()
    A = LI()
    need = (sum(A)-1)//(4*M) + 1
    ans = 0
    #print(need)
    for a in A:
        if a>=need:
            ans += 1
    if ans>=M:
        print("Yes")
    else:
        print("No")
    return

def examC():
    N, K = LI()
    ans = N%K
    if K-ans<ans:
        ans = K-ans
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examC()

"""

"""

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, K = lr()
    tmp = N%K
    print(min(abs(tmp-K), tmp))
resolve()

Submission status: Accepted

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

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

r = N % K

print(min(r, K - r))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
if n % k == 0:
    print(0)
else:
    print(min(abs((n//k)*k - n), abs((n//k + 1)*k - 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 = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

# print("Yes")
# print("No")

# print("YES")
# print("NO")


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

mm = N%K

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

Submission status: Accepted

Solution code:
import sys
import itertools
import queue
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(100000)
# mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, K = read_values()
    R = N % K
    print(min(R, K - R))

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()

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

  a=n
  b=abs(n-k)
  f1=f2=False
  for i in range(100):
    n=abs(n-k)
    if a==n:
      f1=True
    if b==n:
      f2=True

    if f1 and f2:
      return min(a,b)

# main()
print(main())

Submission status: Accepted

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

a = n%k

print(min(a,k-a))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

N = N % K

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

print (ans)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    if N % K == 0:
        print(0)
        exit()
    
    mod = N % K
    ans = min(mod, K - mod)
    print(ans)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

N,K=MI()

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

Submission status: Accepted

Solution code:
# n = int(input())
# s = input()
n,k = map(int,input().split())
# da = list(map(int,input().split()))
# a = [int(input()) for i in range(n)]
# da = [list(map(int,input().split())) for i in range(m)]

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

Submission status: Accepted

Solution code:
import math
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()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


N,K= nm()
s = N % K
if s > K//2:
    s = K -s 
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n = n % k

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, K = map(int, input().split())
x = N%K
print(min(x,abs(x-K)))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

temp=N//K

N-=K*temp

print(min(N,K-N))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main() -> None:
    N, K = map(int, input().split())
    N -= N // K * K
    print(min(N, abs(N - K)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

    a = x % k
    res = min(a, k-a)
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

N, K = map(int, input().split())
N -= (N // K) * K

tmp1 = N
tmp2 = abs(K - N)
print(min(tmp1, tmp2))

Submission status: Accepted

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

a = n//k
n=n-a*k

n_m=abs(n-k)
if n_m > n:
    print(n)
else:
    print(n_m)

Submission status: Accepted

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

p=n//k
n-=k*p

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

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
n,k = map(int,stdin.readline().rstrip().split())
print(min(n%k,k-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())
m = n % k

print(min(m, k-m))

Submission status: Accepted

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


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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

d = set()
ans = n
while n not in d:
    d.add(n)
    if n > k:
        n = n % k
    else:
        n = k - n
    ans = min(ans, n)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(1000000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


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


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K):
    m = N % K
    return min(m, K-m)


def main():
    N, K = read_int_n()
    print(slv(N, K))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % mod) % mod
        
    return CONV()



sys.setrecursionlimit(200005)

def main():
    N,K = isRaw()
    modN = N%K
    return min(modN,abs(K-modN))

if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
print(min(n % k, k - n % k))

Submission status: Accepted

Solution code:
import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter

# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, k = list(map(int, readline().split()))

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    n,k = map(int,input().split())
    ans = min(k - n%k,n%k)
    print(ans)
 
if __name__ =='__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = N - (N // K) * K

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

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = N % K
B = K - A
print(min(A,B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
INF = 1 << 60
sys.setrecursionlimit(10 ** 6)

#ここから書き始める
n, k = map(int, input().split())
ans = min(n % k, abs(n % k - k))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    p1 = n % k
    p2 = abs(p1 - k)

    ans = min(p1, p2)

    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

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

amari = n % k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
import heapq
#n = int(input())
n, k = map(int, sys.stdin.readline().split())
#s = input()
#a = list(map(int, sys.stdin.readline().split()))


x = n % k
y = k - x

print(min(x, y))

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

d,r = divmod(N,K)
print(min(r,K-r))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


n,k = [int(i) for i in read().split()]

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

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

base = n%k

print(min(base,k-base))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

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

main()

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

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

Submission status: Accepted

Solution code:
import math
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
n,k = rMap()
print(min(n%k, abs(n%k-k)))

Submission status: Accepted

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

t = n//k

A = [abs(n - (t - 1) * k), abs(n - t * k), abs(n - (t + 1) * k)]

print(min(A))

Submission status: Accepted

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

res1 = N%K

res2 = abs(N%K-K)
print(min(res1,res2))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=n%k
print(min(x,abs(x-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 = [int(i) for i in input().split()]
x = N//K
print(min(N-K*x, abs(N-K*(x+1))))

Submission status: Accepted

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

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n, k = rli()
    x = n%k
    print(min(x,abs(k-x)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

div = n//k

temp1 = n - div * k
temp2 = abs(temp1 - k)

print(min(temp1, temp2))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

count = n // k
ans1 = n - count * k
ans2 = abs(ans1 - k)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
n = N//K
num = N%K
if num > K - num:
    num = K - num
    n += 1

print(num)

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


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    x = K - N % K
    print(min(x, abs(x - 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:
n,k = (int(num) for num in input().split())
print(min(n%k, k - n%k))

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(n,k):
	return min(n%k,abs(n%k - k))
	

N,K = list(map(int, stdin.readline().rstrip().split()))
ans = solve(N,K)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

arr = []
for i in range(1000):
    arr.append(abs(nn))
    nn -= k
print(min(arr))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()

if n > k:
    n = n % k + k
mi = n
for i in range(10):
    n = abs(n-k)
    mi = min(n, mi)
print(mi)

Submission status: Accepted

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC161c


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
[n,k] = [int(i) for i in 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:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n,k = m()

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