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: Runtime Error

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]:
      break
    else:
      num[0] = difference
      break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  if i = 0:
    num[0] = num[0] % num[1]
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Runtime Error

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]
      break
    else:
      num[0] = difference
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Runtime Error

Solution code:
import numpy as np


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

i =N
while i>0:
    i = i-K
    i_ = i #一時避難場所
print (i if i<np.abs(i_) else np.abs(i_))

Submission status: Runtime Error

Solution code:
K, N = map(int,input().split())
a = list(map(int,input().split()))
x = [a[0]-a[-1]+K]
for i in range(1,N):
    x.append(a[i]-a[i-1])
print(x)
print(K-max(x))

Submission status: Runtime Error

Solution code:
import fractions
n,k=map(int,input().split())
if k==1:print(0)
else:print(gcd(n,k))

Submission status: Runtime Error

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

m=n%k
d=abs(m-k)

if m<k:
    print(m)
elif k<m:
    print(k)
else:
    print(k)

Submission status: Runtime Error

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


m=n%k
d=abs(m-k)

if m<k:
    print(m)
elif k<m:
    print(k)
else:
    print(k)

Submission status: Runtime Error

Solution code:
n, k = (int(x) for x in input().split())
i = int(n/k)
f = (n/k) - i
if f <= 0.5:
  print(abs(n - (i*k))
else:
  print(abs(n - ((i + 1)*k))

Submission status: Runtime Error

Solution code:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define mod 1000000007
int main()
{
    ll n,k;
    cin>>n>>k;
    ll t=n%k;
    ll t1=k-t;
    cout<<min(t,t1)<<endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
S = N // K
Answer = abs(N - K*S)
if abs(Answer - K) <= Answer:
    Answer = abs(Answer - K)
print(Answer)

Submission status: Runtime Error

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

def replace(a,b):
    if a < abs(a - b):
        return a
    else:
        return replace(abs(a - b), b)

if K == 0:
    print(N)
elif N % K == 0:
    print(0)
else:
    print(replace(N, K))

Submission status: Runtime Error

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

def abs_diff(n, k):
  first_time = abs(n - k)
  second_time = abs(first_time - k)
  if second_time <= first_time:
    abs_diff(first_time, k)
  elif first_time <= n:
    print(first_time)
  else:
    print(n)

abs_diff(n, k)

Submission status: Runtime Error

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

def abs_diff(n, k):
  first_time = abs(n - k)
  second_time = abs(first_time - k)
  if second_time < first_time:
    abs_diff(first_time, k)
  elif first_time < n:
    print(first_time)
  else:
    print(n)

abs_diff(nk[0], nk[1])

Submission status: Runtime Error

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

def abs_diff(n, k):
  first_time = abs(n - k)
  second_time = abs(first_time - k)
  if second_time < first_time:
    abs_diff(first_time, k)
  elif first_time < n:
    print(first_time)
  else:
    print(n)

abs_diff(n, k)

Submission status: Runtime Error

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


while:
	tmp = N
	if(abs(tmp - K) < tmp):
      	tmp = abs(tmp - K)
   	else:
      	break
 	
print(tmp)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
int tmp = 0
 
while:
	tmp = N
	if(abs(tmp - K) < tmp):
      	tmp = abs(tmp - K)
   	else:
      	break
 	
print(tmp)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
int tmp = 0
 
while();
	tmp = N
	if(abs(tmp - K) < tmp);
      	tmp = abs(tmp - K)
   	else;
      	break
 	
print(tmp)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
int tmp = 0
 
while();
  N = tmp
  	if(abs(tmp - K) < tmp);
      tmp = abs(tmp - K)
  	else;
      break

    
print(tmp)

Submission status: Runtime Error

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

while(){
  N = tmp
  	if(abs(tmp - K) < tmp){
      tmp = abs(tmp - K)
    }
  	else{
      break;
    }
}

print(tmp)

Submission status: Runtime Error

Solution code:
n = int(input())
k = int(input())
if n >= k:
  n = n % k

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

Submission status: Runtime Error

Solution code:
n = int(input())
k = int(input())

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

Submission status: Runtime Error

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

if abs(N-K) < N:
    if N >= K:
        if K/(N%K) >= 1:
            print(K%(N%K))
        else:
            print(N % K)
    else:
        if N/(K%N) >= 1:
            print(N%(K%N))
        else:
            print(K % N)
else:
    print(N)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

mo = n%k

mok = abs(mo - k)

if mo = mok:
    print(mo)
else:
    print(mok)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
string s;
	



int main()
{
 ll n,k,x;
 cin>>n>>k;
 if(n%k!=0)
x=(n+k)/k;
else
x=n/k;
 cout<<min(n,abs(n-k*x));


}

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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



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

Submission status: Runtime Error

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

if N%K==0:
    print(0)
else:
    x = abs(N-K)
    for i in range(N/K):
        x = min(x,abs(x-K))
    print(x)

Submission status: Runtime Error

Solution code:
from math import gcd
import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            ans += gcd(gcd(i,j), k)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=input().split()
if int(n)<int(k):
    l=int(n)
while int(n)>int(k):
    l=int(n)-int(k)
    n=str(l)
l=int(k)-l
print(l)

Submission status: Runtime Error

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

def calc(x, y):
  if y == 1:
    print(0)
    return
  z = abs(x-y)
  if z > abs(z-y):
    calc(z, y)
  else:
    print(z)
  
calc(N, K)

Submission status: Runtime Error

Solution code:
import numpy as np


x, k = (int(x) for x in input().split())
while True:
    y = np.min(x, abs(x-k))
    if x == y:
        print(y)
        break
    else:
        x = y

Submission status: Runtime Error

Solution code:
n,k = map(int,input())
pn = n
while(abs(n-k)<n):
  pn = n
  n = abs(n-k)
print(pn)

Submission status: Runtime Error

Solution code:
i = input().split()
i_int = [int(n) for n in i]
a = input().split()
A = [int(m) for m in a]
N = i_int[0]
M = i_int[1]

th = sum(A)/(4 * M)

##print(threshhold)

c = 0
for val in A:
    if val >= th:
    ##print(val)
        c += 1

if M >= c:
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(n)>1):
    n = n//k
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Runtime Error

Solution code:
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
def dif(n, k):
    if abs(n - k) <= n:
        return dif(abs(n-k), k)
    else:
        return n

print(dif(N,K))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
if n > k:
  n = n//k
  if k = 1:
    n = 0
if n < k:
  n = n
  if n > k/2
  n = n-k
print(n)

Submission status: Runtime Error

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
while a < n:
  a = abs(n-k)
  n = a
print(n)

Submission status: Runtime Error

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(n[1])
a = abs(n-k)
while a < n:
  n = a
  a = abs(n-k)
print(n)

Submission status: Runtime Error

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(n[1])
a = abs(n)-abs(k)
while a < n:
  n = a
  a = abs(n) - abs(k)
while a > n:
  n = n
print(n)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
if n > 0:
  n = n-k
elif n <= 0:
  break
m = abs(n)
l = abs(m-k)
print(min(m,l))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())

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

Submission status: Runtime Error

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

# In[10]:


N,K = int(input()), int(input())
new = abs(N-K)
while(new<N):
    N = new 
    new = abs(new-K)
print(N)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
n1 = N%K
n2 = K -n1
print(min(n1,n2))

Submission status: Runtime Error

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

# In[10]:


N,K = int(input()), int(input())
new = abs(N%K)
while(new<N):
    N = new
    new = abs(new%K)
print(N)

Submission status: Runtime Error

Solution code:
N,K = int(input()), int(input())
new = abs(N-K)
while(new<N):
    N = new
    new = abs(new-K)
print(N)

Submission status: Runtime Error

Solution code:
n_k = [int(i) for i in input().split()]
n = n_k[0]
k = n_k[1]
r = n % k
r_1 = abs(r - k)
min = np.array([r, r_1]).min()
print(min)

Submission status: Runtime Error

Solution code:
from math import ceil
n, k = map(int,input().split())
array = [n]
p = ceil(max(n/k,k/n))+1
n_r = 10**18
if k == 1:
    array.append(0)
else:
    for i in range(p):
        n_r = n
        n = abs(n-k)
        array.append(n)
        if n > n_r:
            break
min_n = min(array)
print(min_n)

Submission status: Runtime Error

Solution code:
from math import ceil
n, k = map(int,input().split())
array = []
p = ceil(max(n/k,k/n))+2
n_r = 0
if k == 1:
    array.append(0)
else:
    for i in range(p):
        n_r = n
        n = abs(n-k)
        array.append(n)
        if n > n_r:
            break
min_n = min(array)
print(min_n)

Submission status: Runtime Error

Solution code:
from math import ceil
n, k = map(int,input().split())
array = []
p = ceil(max(n/k,k/n))+2
if k == 1:
    array.append(0)
else:
    for i in range(p):
        n = abs(n-k)
        array.append(n)
min_n = min(array)
print(min_n)

Submission status: Runtime Error

Solution code:
def ss(a, b):
  if a > b:
    return a - b
  else:
    return b - a

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

p = None
while n - k != p if not p is None else True:
	m = ss(n , k)
    p = n
    n = m
    
print(n)

Submission status: Runtime Error

Solution code:
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

candidate1 = N - K * math.floor(N / K)
candidate2 = K - remainder
answer = min(candidate1, candidate2)

print(answer)

Submission status: Runtime Error

Solution code:
import math

N , K = map(int, input().split())
flag = "F"

while 1:
    lenN = int (math.log10(N) + 1)
    lenK = int (math.log10(K) + 1)
    N = abs(N-K*(10**abs(lenN-lenK-1)))
    lenN = int (math.log10(N) + 1)
    if abs(lenN-lenK)<2:
        break
        
while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Runtime Error

Solution code:
import math

N , K = map(int, input().split())
flag = "F"

lenN = int (math.log10(N) + 1)
lenK = int (math.log10(K) + 1)

N = abs(N-K*(10**abs(lenN-lenK+1)))
while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Runtime Error

Solution code:
import math

N , K = map(int, input().split())
flag = "F"

lenN = int (math.log10(N) + 1)
lenK = int (math.log10(K) + 1)
N = abs(N-K*(abs(lenN-lenK))) 

while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Runtime Error

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

lenN = len(N)
lenK = len(K)
N = abs(N-K*(abs(len(N)-len(K)))) 

while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Runtime Error

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

if N >= K:
	N %= K

if ( K / 2 ) >= N:
	print ( N )

else:
    print ( K - N )

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int, input().split())
n = N
while 1:
	a = np.minumum(n, np.abs(n - K))
	if a == n:
		break
print(a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
N, K = map(abs, (N,K))
amari = N%K
if amari == 0:
    print(0)
else:
    amari2 = K - amari
    print(max(amari, amari2)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
A = list(map(int, input().split()))
all_vote = sum(A)
cnt = 0
for a in A:
    if a * 4 * m >= all_vote:#1/4mを4m倍したときとおきかえて計算(整数にするため)
        cnt += 1
if cnt >= m:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

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

prev = N

if N == 0:
  print( N)
else:
  while True:
    if prev == 0:
      print(prev)
      break
    elif abs() >= prev:
      print(prev)
      break
    else
      N = abs()
      prev = N

Submission status: Runtime Error

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

min_value = 0

count = 0
while True:
  if min_value < abs(N - K):
    count =+ 1
    if count > 1:
	    break
    else:
		N = abs(N - K)
		min_value = N		
  else:
	N = abs(N - K)
	min_value = N
    
print(min_value)

Submission status: Runtime Error

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

min1 = 0
prev = 0

while True:
  if prev < abs(N - K):
    break
  else:
    min1 = abs(N - K)
	N = abs(N - K)
	prev = min1


print(prev)

Submission status: Runtime Error

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

min = 0
prev = 0

while abs(N - K) > prev:
  min = abs(N - K)
  prev = min

print(min)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split(" ")))
a = abs(n - k)
n = n - a * (n // a)
n_last = abs(n - k)
if n_last < n:
    n = n_last
print(n)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np
N,K=list(map(int,(stdin.readline().strip().split())))
print(np.gcd(N,K))

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
N,K=list(map(int,(stdin.readline().strip().split())))
print(math.gcd(N,K))

Submission status: Runtime Error

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

z = a % b

if z > b/2:
    print(abs(z-k))
else:
    print(z)

Submission status: Runtime Error

Solution code:
x = int(input())
y = int(input())

if y > x:
  tmp = x
  x = y
  y = tmp

minn = 1e9
  
while True:
  x = abs(x - y)
  tmp = x
  x = y
  tmp = x
  
  if y > minn:
    break
   
  minn = minn(y, minn)
  
print(minn)

Submission status: Runtime Error

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

a = N % K
if a == 0:
    print(0)
else:
    print(min(a, abs(a-k)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import deque
k = int(input())
a = deque([1,2,3,4,5,6,7,8,9])
count=0
while count !=k:
  b = a.popleft()
  count += 1
  if b%10==0:
    a.append(b*10)
    a.append(b*10+1)
  elif b%10==9:
    a.append(b*10+8)
    a.append(b*10+9)
  else:
    a.append(b*10+b%10-1)
    a.append(b*10+b%10)
    a.append(b*10+b%10+1)
print(b)

Submission status: Runtime Error

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

if n != k:
  x1 = n % k
  x2 = k - x1
  print(min(x1, x2))
else n == k:
  print(0)

Submission status: Runtime Error

Solution code:
N,K=input().split()
N,K=[int(N),int(K)]
 
minimum=0
while True:
  minimum=N
  if minimum > = N:
    minimum=N
  else:
    minimum=minimum
    break
  
  
print(minimum)

Submission status: Runtime Error

Solution code:
N,K=input().split()
N,K=[int(N),int(K)]
 
minimum=N
while N < =minimum:
  N=abs(N-K)
  minimum=N
  
  
print(minimum)

Submission status: Runtime Error

Solution code:
k=int(input())

a=[]
i=1
d=0
while True:
    if len(str(i))==1:
        a.append(i)
    elif len(str(i))==2:
        I=str(i)
        c=abs(int(I[0])-int(I[1]))
        if c<=1:
            a.append(i)   
            
    elif len(str(i))==3:
        I=str(i)
        for j in range(2):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==2:
            a.append(i)
        d=0
        
    elif len(str(i))==4:
        I=str(i)
        for j in range(3):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==3:
            a.append(i) 
        d=0
        
    elif len(str(i))==5:
        I=str(i)
        for j in range(4):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==4:
            a.append(i)   
        d=0
        
    elif len(str(i))==6:
        I=str(i)
        for j in range(5):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==5:
            a.append(i)
        d=0
    
    elif len(str(i))==7:
        I=str(i)
        for j in range(6):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==6:
            a.append(i)
            
        d=0    
        
    elif len(str(i))==8:
        I=str(i)
        for j in range(7):
            c=abs(int(I[j])-int(I[j+1]))
            if c<=1:
                d=d+1
        if d==7:
            a.append(i)
            
        d=0    
        
    i=i+1
    if i==10000000:
        break

print(a[k-1])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
# 未解決(最後だけREになる)


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


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs((a[0] % a[1]) - a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs((a[0] % a[1]) - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1]) - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(((a[0] % a[1]) - a[1]) % a[1]))
                sys.exit(0)

Submission status: Runtime Error

Solution code:
a,b = input().split()
c = a % b
if b - c <= c :
  print(b - c)
else :
  print(c)

Submission status: Runtime Error

Solution code:
1000000000000000000 1

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = n
s = [l]
while l > 0:
    l = l-k
    s += [l]
s.remove(l)
print(min(s))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
l = []
if(n%m = 0):
  ans=0
else:
  n = n-m
  l.append(n)
  
print(ans)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())
x = n//k
if(n<k):
    x = k//n
    print(min(abs(k*x-n),abs(k*(x+1)-n)))
else:
    print(min(abs(k*x-n),abs(k*(x+1)-n)))

Submission status: Runtime Error

Solution code:
n,k=[int(x) for x in input().split()]
#Sequence decreases first then starts increasing.This point has to be identified.
prev,cur=n+1,n
if k<=0:
  print(n)
else:
	rem = n%k
    print(abs(rem-k))

Submission status: Runtime Error

Solution code:
#スペース区切りの整数の入力
N, K = map(int, input().split())
print(min(N%K),K-N%K)

Submission status: Runtime Error

Solution code:
#スペース区切りの整数の入力
N, K = map(int, input().split())

aa = int(N/K)
ans = N - K*aa
ans_2 = ans - K

if(N<K):
  print(N)
elif(N%K == 0):
  print("0")
elif(ans<ans_2)
  print(ans)
else:
  print(ans_2)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
i = [int(i) for i in input().split()]
n = i[0]
k = i[1]
n2 = n % k
print(n2 if k < n2 else k % n2)

Submission status: Runtime Error

Solution code:
buf = input().split()
n = int(buf[0])
k = int(buf[1])

c1 = n % k
c2 = -1 * (c1 - k)
return min([c1, c2])

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input("enter value of n and k : ").split())
m = []
for i in range(n):
    l = abs(n-k)
    m.append(l)
    n = l
print(min(m))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=abs(n-m)
if n>m:
	n = n-(ceil((n-m)/m))*m
z=(min(n,m-n))
print(min(z,x))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=abs(n-m)
n = n-(ceil((n-m)/m))*m
z=(min(n,m-n))
print(min(z,x))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=abs(n-m)
n = n-(ceil((n-m)/m))*m
 
z=(min(n,m-n))
print(min(z,x))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=abs(n-m)
if n>m:
	n = n-(ceil((n-m)/m))*m

z=(min(n,m-n))
print(min(z,x))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
#入力受け取り
N, K=map(int, input().split())

#N=aK+bとした時のbを求める
B=N%K

#bとK-bの小さい方を出力
print(min(b, K-b))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
num = 0
while True:
	num = n-k
    if num < 0:
      print(abs(num))

Submission status: Runtime Error

Solution code:
n, k = int(input())
print(n % k)

Submission status: Runtime Error

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

min_num = 10**18+1
flag = True

while flag:
  N = abs(N-K)
  if N < min_num: min_num = N
  else: break
    
return min_num

Submission status: Runtime Error

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

min = 10**18+1
flag = True

while flag==True:
  N = abs(N-K)
  if N < min: min = N
  else: flag = False
    
return min

Submission status: Runtime Error

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

min = 10**18+1
flag = True

while flag==True:
  N = abs(N-K)
  if N < min: min = N
  else: flag == False
    
return min

Submission status: Runtime Error

Solution code:
def main():
    n,k,c = map(int,input().split())
    s = list(input())
    s = list(map((lambda x: -1 if x=='o' else 0), s))
    r = s[::-1]
    ni = 0
    cnt = 0
    while(ni<n):
        if cnt>=k:
            break
        if s[ni] == -1:
            s[ni] = 1
            ni += c+1
            cnt += 1
        else:
            ni += 1
    ni = 0
    cnt = 0 
    while(ni<n):
        if cnt>=k:
            break
        if r[ni] == -1:
            r[ni] = 1
            ni += c+1
            cnt += 1
        else:
            ni += 1
    r = r[::-1]

    for i in range(n):
        if s[i] == 0:
            continue
        s_len = len([i for i in s[:i] if i==1])
        r_len = len([i for i in r[i+1:] if i==1])
        total = s_len+r_len
        # print(i)
        # print(s_len,r_len)
        if total < k:
            print(i+1)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def gcd(a,b):
  if(b==0):
    return a
  return gcd(b,a%b)

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

Submission status: Runtime Error

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

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

print(result)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
using namespace std;
#include "bits/stdc++.h"
long gcd08(long a, long b) {
    long nod = 1L;
    long tmp;
    if (a == 0L)
        return b;
    if (b == 0L)
        return a;
    if (a == b)
        return a;
    if (a == 1L || b == 1L)
        return 1L;
    while (a != 0 && b != 0) {
        if (((a & 1L) | (b & 1L)) == 0L) {
            nod <<= 1L;
            a >>= 1L;
            b >>= 1L;
            continue;
        }
        if (((a & 1L) == 0L) && (b & 1L)) {
            a >>= 1L;
            continue;
        }
        if ((a & 1L) && ((b & 1L) == 0L)) {
            b >>= 1L;
            continue;
        }
        if (a > b) {
            tmp = a;
            a = b;
            b = tmp;
        }
        tmp = a;
        a = (b - a) >> 1L;
        b = tmp;
    }
    if (a == 0)
        return nod * b;
    else
        return nod * a;
}

int main(){
    long long a,b;
    cin >> a >> b;
    cout << gcd08(a,b);
}

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
while true:
    if abs(n - k) < n:
        n = abs(n - k)
    else:
        break
print(n)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
for range(n):
	if abs(n - k) < n:
        n = abs(n-k)
print(n)

Submission status: Runtime Error

Solution code:
import math
a,b=map(int, input().split())
print(math.gcd(a,b))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n,k=int(input().split())
i=n//k
j=n-k*i
if j>=abs(j-k):
    print(abs(j-k))
else:
    print(j)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

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

abs_value = abs(N - K)
value = N//abs_value
value2 = N%K

if value==0:
    print (N)
elif value2==0:
    print(0)
else:
    print (N - abs_value * value)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def solve(n, k):

    if k == 1:
        return 0

    if max(n, k) % min(n, k) == 0:
        return min(n, k)
    prev = set()
    tmp_val = n
    while True:
        tmp_val = abs(tmp_val - k)
        if tmp_val in prev:
            break
        prev.add(tmp_val)
    return min(prev)


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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

# N = 1000000000000000000
# K = 1
amari = N%K
bmari = abs(amari-K)
if amari >= bmari:
    print(bmari)
else:
    print(amari)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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