Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from collections import deque


if __name__ == "__main__":
    H, W = map(int, input().split())
    ch, cw = map(lambda x: int(x)-1, input().split())
    dh, dw = map(lambda x: int(x)-1, input().split())
    maze = [list(input()) for _ in range(H)]
    INF = 10**8
    mc = [[INF]*W for _ in range(H)]
    walk = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    warp = [
        (2, 2), (2, 1), (2, 0), (2, -1), (2, -2),
        (1, 2), (1, 1), (1, -1), (1, -2),
        (0, 2), (0, -2),
        (-1, 2), (-1, 1), (-1, -1), (-1, -2),
        (-2, 2), (-2, 1), (-2, 0), (-2, -1), (-2, -2)
    ]
    walk_que = deque()
    warp_que = deque()
    walk_que.append((ch, cw, 0))
    while walk_que:
        y, x, w = walk_que.popleft()
        mc[y][x] = w
        exit_wall = False
        for dy, dx in walk:
            ny = y + dy
            nx = x + dx
            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] == INF:
                if maze[ny][nx] == ".":
                    walk_que.append((ny, nx, w))
                else:
                    exit_wall = True
        if exit_wall:
            warp_que.append((y, x, w))
        if not walk_que:
            while warp_que:
                y, x, w = warp_que.popleft()
                for dy, dx in warp:
                    ny = y + dy
                    nx = x + dx
                    if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] == INF and maze[ny][nx] == ".":
                        walk_que.append((ny, nx, w+1))
    if mc[dh][dw] == INF:
        print(-1)
    else:
        print(mc[dh][dw])


### FIXED CODE ###

import sys
from collections import deque


input = lambda: sys.stdin.readline().rstrip()


if __name__ == "__main__":
    H, W = map(int, input().split())
    ch, cw = map(lambda x: int(x)-1, input().split())
    dh, dw = map(lambda x: int(x)-1, input().split())
    maze = [list(input()) for _ in range(H)]
    INF = 10**8
    mc = [[INF]*W for _ in range(H)]
    mc[ch][cw] = 0
    walk_que = deque()
    walk_que.append((ch, cw, 0))
    while walk_que:
        y, x, w = walk_que.popleft()
        for ny, nx in [(y+1, x), (y-1, x), (y, x+1), (y, x-1)]:
            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w and maze[ny][nx] == ".":
                mc[ny][nx] = w
                walk_que.appendleft((ny, nx, w))
        for dy in range(-2, 3):
            ny = y + dy
            for dx in range(-2, 3):
                nx = x + dx
                if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w+1 and maze[ny][nx] == ".":
                    mc[ny][nx] = w + 1
                    walk_que.append((ny, nx, w+1))
    if mc[dh][dw] == INF:
        print(-1)
    else:
        print(mc[dh][dw])
### BUGGY CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

a = [1]*(10**5+1)
a[0] = a[1] = 0

for i in range(2, ceil(sqrt(10**5)+1)):
    if a[i] == 0:
        continue
    else:
        if i == 2:
            a[i] = 0
        for j in range(i*2, len(a), i):
            a[j] = 0
            if j*2 - 1 < len(a):
                a[j*2-1] = 0

q = INT()
p = [LIST() for i in range(q)]

for i in range(q):
    print(sum(a[p[i][0]:p[i][1]+1]))

### FIXED CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

a = [1]*(10**5+1)
a[0] = a[1] = a[2] = 0

for i in range(2, ceil(sqrt(10**5)+1)):
    for j in range(i*2, len(a), i):
        a[j] = 0
        if j*2 - 1 < len(a):
            a[j*2-1] = 0

q = INT()
p = [LIST() for i in range(q)]
s = list(accumulate(a))

for i in range(q):
    print(s[p[i][1]]-s[p[i][0]-1])
### BUGGY CODE ###

def main():
    N = int(input())
    for h in range(1, 3501):
        for n in range(1, 3501):
            if (4*h*n-N*h-N*n) == 0: continue
            if (N*h*n)%(4*h*n-N*h-N*n) == 0:
                w = (N*h*n)//(4*h*n-N*h-N*n)
                if w > 0:
                    print(h,n,w)
                    return

main()


### FIXED CODE ###

def main():
    N = int(input())
    for h in range(1, 3501):
        for n in range(1, 3501):
            if (4*h*n-N*h-N*n) > 0:
                if (N*h*n)%(4*h*n-N*h-N*n) == 0:
                    w = (N*h*n)//(4*h*n-N*h-N*n)
                    print(h,n,w)
                    return

main()
### BUGGY CODE ###

import sys
N = int(input())
P = list(map(int, input().split()))

ans = 0

for i in range(N):
    count = 0
    for j in P[0:i]:
        if P[i] <= j:
            count = count + 1
        
    if count == len(P[0:i]):
        ans = ans + 1  

print(ans)

### FIXED CODE ###

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

result = 0
m = P[0]
for i in range(N):
    if P[i] <= m:
        result += 1
        m = P[i]
print(result)
### BUGGY CODE ###

# coding: utf-8
# from pdb import set_trace
from collections import Counter

N = int(input())
L = list(map(int, input().split(" ")))
C = dict(Counter(L))
L = list(set(L))

if N<3 or len(L)<3:
    print(0)
    exit()
li = []
for xa, a in enumerate(L[:-2]):
    for xb, b in enumerate(L[xa+1:-1]):
        for xc, c in enumerate(L[xa+2:]):
            if a+b>c and b+c>a and c+a>b and a!=b and b!=c and c!=a:
                _li = sorted([xa, xb+xa+1, xc+xa+2])
                if _li in li:
                    continue
                li.append(_li)
cnt = 0
for l in li:
    a = C[L[l[0]]]
    b = C[L[l[1]]]
    c = C[L[l[2]]]
    _cnt = a * b * c
    cnt += _cnt
print(cnt)

### FIXED CODE ###

# coding: utf-8
from pdb import set_trace
#from collections import Counter
import itertools

N = int(input())
L = list(map(int, input().split(" ")))
# print(L)

# C = dict(Counter(L))
# L = sorted(list(set(L)), reverse=True)

# if N<3 or len(L)<3:
#     print(0)
#     exit()
# li = []
# for xa, a in enumerate(L[:-2]):
#     for xb, b in enumerate(L[xa+1:-1]):
#         for xc, c in enumerate(L[xa+2:]):
#             if a+b<c:
#                 break
#             if a+b>c and b+c>a and c+a>b and a!=b and b!=c and c!=a:
#                 _li = sorted([xa, xb+xa+1, xc+xa+2])
#                 if _li in li:
#                     continue
#                 li.append(_li)
# cnt = 0
# for l in li:
#     a = C[L[l[0]]]
#     b = C[L[l[1]]]
#     c = C[L[l[2]]]
#     cnt += (a * b * c)
# print(cnt)
cnt = 0
TRI = list(itertools.combinations(L, 3))
for tri in TRI:
    a, b, c = tri
    if a==b or b==c or c==a:
        continue
    elif a+b>c and a+c>b and b+c>a:
        cnt += 1
print(cnt)
### BUGGY CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
for i in range(N):
  temp = A[:i] + A[i+1:]
  print(max(temp))

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]

maxv = max(A)
max_id = A.index(maxv)
second = max(A[:max_id] + A[max_id+1:])

for i in range(N):
  if i == max_id:
    print(second)
  else:
    print(maxv)
### BUGGY CODE ###

import collections

N = int(input())
listA = list(map(int, input().split()))
# 各数字の出現回数をカウント
countA = collections.Counter(listA)
countA_calc = {}
countA_calc2 = {}
for i in listA:
    count = 0
    for x, y in countA.most_common():
        if x == i:
            if x not in countA_calc2:
                countA_calc2[x] = (y -1) * (y - 2) // 2
            count += countA_calc2[x]
        else:
            if x not in countA_calc:
                countA_calc[x] = y * (y -1) // 2 
            count += countA_calc[x]
    print(count)

### FIXED CODE ###

import collections
import math
def combination(a, b):
    tmp = 1
    for i in range(a - b + 1, a + 1):
        tmp *= i
    return tmp // math.factorial(b)
 
N = int(input())
listA = list(map(int, input().split()))
# 各数字の出現回数をカウント
countA = dict(collections.Counter(listA).most_common())
countA_calc = {k: v * (v -1) // 2 for (k, v) in countA.items()}
countA_calc2 = {k: (v - 1) * (v -2) // 2 for (k, v) in countA.items()}
summery = sum(countA_calc.values())
for i in listA:
    print(summery - countA_calc[i] + countA_calc2[i])
### BUGGY CODE ###

from collections import Counter

N = int(input())
A_list = [int(e) for e in input().split()]

#約数を列挙
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

divisors_list = [make_divisors(n) for n in A_list]
all_divisors_list = []
for i in divisors_list:
  all_divisors_list += i
  
#print(all_divisors_list)
collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  #print(a)
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)

### FIXED CODE ###

from collections import Counter
from functools import lru_cache

N = int(input())

#約数を列挙
@lru_cache(maxsize=1000)
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

A_list = [make_divisors(int(e)) for e in input().split()]

all_divisors_list=[]

for i in A_list:
  all_divisors_list += i

collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(set(a))
x=len(b)-k
b=list(map(lambda x:a.count(x),b))
b.sort()
if x<=0:
  print(0)
else:
  print(sum(b[:x]))

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
n=[0]*200000
x=[]
for i in a:
    n[i-1]+=1
for i in n:
    if i!=0:
        x+=[i]
y=len(x)
z=y-k
if z<=0:
    print(0)
else:
    x.sort()
    print(sum(x[:z]))
### BUGGY CODE ###

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

ab = []
for i in range(n):
  a,b = map(int, input().split())
  ab.append([a, b])


dp = [10**9 for i in range((h+1)+10000)]
dp[0] = 0
for i in range(1, (h+1)+10000):
  for j in range(n):
    a, b = ab[j]
    if a > i:
      continue
    dp[i] = min(dp[i], dp[i-a]+b)

x = min(dp[h:])
print (x)

### FIXED CODE ###

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

ab = []
for i in range(n):
  a,b = map(int, input().split())
  ab.append([a, b])


dp = [10**9 for i in range((h+1)+10000)]
dp[0] = 0
for i in range(1, (h+1)+10000):
  for j in range(n):
    a, b = ab[j]
    if a > i:
      continue
    dp[i] = min(dp[i], dp[i-a]+b)

x = min(dp[h:])
print (x)
### BUGGY CODE ###

def main():
  _max = max
  h, w = map(int, input().split())
  s = [input() + '#' for _ in range(h)]
  s.append('#' * (w + 1))

  rt = [[-1] * w for _ in range(h + 1)]

  tstart = [-1] * (w + 1)
  for i in range(h + 1):
    si = s[i]
    rti = rt[i]
    ystart = -1
    for j in range(w + 1):
      tj = tstart[j]
      if si[j] == '#':
        if ystart != -1:
          jy = j - ystart
          for k in range(ystart, j):
            rti[k] += jy
          ystart = -1
        if tj != -1:
          itj = i - tj
          for k in range(tj, i):
            rt[k][j] += itj
          tstart[j] = -1
      else:
        if ystart == -1:
          ystart = j
        if tj == -1:
          tstart[j] = i

  print(_max(_max(l) for l in rt))

main()

### FIXED CODE ###

h, w = map(int, input().split())
s = [input() + '#' for _ in range(h)]
s.append('#' * w)

yoko = [[0] * w for _ in range(h)]
for i in range(h):
  if s[i][0] == '.':
    start = 0
  else:
    start = -1
  for j in range(1, w + 1):
    if s[i][j] == '#':
      if start != -1:
        for k in range(start, j):
          yoko[i][k] = j - start
        start = -1
    else:
      if start == -1:
        start = j

tate = [[0] * w for _ in range(h)]
for i in range(w):
  if s[0][i] == '.':
    start = 0
  else:
    start = -1
  for j in range(1, h + 1):
    if s[j][i] == '#':
      if start != -1:
        for k in range(start, j):
          tate[k][i] = j - start
        start = -1
    else:
      if start == -1:
        start = j
result = 0
for i in range(h):
  for j in range(w):
    t = yoko[i][j] + tate[i][j] - 1
    if t > result:
      result = t

print(result)
### BUGGY CODE ###

import math
from functools import reduce
import itertools

def gcd(numbers):
    return reduce(math.gcd, numbers)


k = int(input())
koho = list(range(1,k+1))*3
ans = 0
for i in set(list(itertools.combinations(koho, 3))):
    ans += gcd(list(i))
      
print(ans)

### FIXED CODE ###

k = int(input())
hoge = [1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081, 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463, 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 89349, 97881, 103342, 111528, 120141, 128047, 134580, 146316, 154177, 164817, 174438, 185836, 194157, 207927, 218812, 233268, 245277, 257857, 268182, 288216, 299257, 313635, 330204, 347836, 362973, 383709, 397042, 416448, 434025, 456967, 471948, 499740, 515581, 536073, 559758, 583960, 604833, 633651, 652216, 683712, 709065, 734233, 754734, 793188, 818917, 846603, 874512, 909496, 933081, 977145, 1006126, 1041504, 1073385, 1106467, 1138536, 1187112, 1215145, 1255101, 1295142, 1342852, 1373253, 1422195, 1453816, 1502376, 1553361, 1595437, 1629570, 1691292, 1726717, 1782111, 1827492, 1887772, 1925853, 1986837, 2033674, 2089776, 2145333, 2197483, 2246640, 2332104, 2379085, 2434833, 2490534, 2554600, 2609625, 2693919, 2742052, 2813988, 2875245, 2952085, 3003306, 3096024, 3157249, 3224511, 3306240, 3388576, 3444609, 3533637, 3591322, 3693924, 3767085, 3842623, 3912324, 4027884, 4102093, 4181949, 4270422, 4361548, 4427853, 4548003, 4616104, 4718640, 4812789, 4918561, 5003286, 5131848, 5205481, 5299011, 5392008, 5521384, 5610705, 5739009, 5818390, 5930196, 6052893, 6156139, 6239472, 6402720, 6493681, 6623853, 6741078, 6864016, 6953457, 7094451, 7215016, 7359936, 7475145, 7593865, 7689630, 7886244, 7984165, 8130747, 8253888, 8403448, 8523897, 8684853, 8802826, 8949612, 9105537, 9267595, 9376656, 9574704, 9686065, 9827097, 9997134, 10174780, 10290813, 10493367, 10611772, 10813692]
print(hoge[k-1])
### BUGGY CODE ###

n=int(input())
List=list(map(int,input().split()))
for i in range(n):
    count=0
    for j in range(n-1):
        if List[j]==i+1:
            count+=1
    print(count)

### FIXED CODE ###

n=int(input())
List=list(map(int,input().split()))
numbers=[]
for i in range(n):
    numbers.append(0)
    
for j in range(n-1):
    a=List[j]
    numbers[a-1]+=1

for i in range(n):
    print(numbers[i])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
def add_link(a, b):
    la = links[a]
    i, j = [(0, 1), (1, 0)][s[b] == 'B']
    if not la[i] and la[j]:
        able[a] = True
    la[i].add(b)

n, m = map(int, input().split())
s = input()
links = [[set(), set()] for _ in range(n)]
able = [False] * n
for line in sys.stdin.readlines():
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    add_link(a, b)
    add_link(b, a)

def erase(v):
  if s[v] == "A":
    l = 0
  else:
    l = 1
  for q in links[v][0]:
    if not able[q]:
      continue
    links[q][l].remove(v)
    if links[q][l] == set():
      able[q] = False
      erase(q)
  for q in links[v][1]:
    if not able[q]:
      continue
    links[q][l].remove(v)
    if links[q][l] == set():
      able[q] = False
      erase(q)  
for i in range(n):
  if not able[i]:
    continue
  for k in range(2):
    for j in links[i][k].copy():
      if not able[j]:
        links[i][k].remove(j)
  if (links[i][0] == set() or links[i][1] == set()):
    flag =1
    able[i] = False
    erase(i)
if any(able):
  print("Yes")
else:
  print("No")

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)
n, m = map(int, input().split())
s = input()
links = [[] for _ in range(2*n)]
visit = [0] * n *2
for line in sys.stdin.readlines():
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    if s[a] == s[b]:
      links[a].append(b+n)
      links[b].append(a+n)
    else:
      links[a+n].append(b)
      links[b+n].append(a)
def dfs(v):
  visit[v] = 1
  for i in links[v]:
    if visit[i] == 1:
      print("Yes")
      exit()
    elif visit[i] == 0:
      dfs(i)
  visit[v] = -1
  
for i in range(n):
  if visit[i] == 0:
    dfs(i)
print("No")
### BUGGY CODE ###

def main():
    import queue
    import sys
    input = sys.stdin.readline

    H, W = list(map(int, input().split()))
    S = list(map(int, input().split()))
    G = list(map(int, input().split()))
    S = [S[0] - 1, S[1] - 1]
    G = [G[0] - 1, G[1] - 1]

    maze = []
    for i in range(H):
        maze.append(input())

    MAX = 1000001
    cost_list = [[MAX] * W for i in range(H)]


    to_walk = queue.Queue()
    to_warp = queue.Queue()


    def walk(z):
        h, w = z
        cost = cost_list[h][w]
        to_warp.put([h, w])
        for [p, q] in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
            a,b=h+p,w+q
            if (0 <= a < H) and (0 <= b < W):
                if maze[a][b] == "." and cost_list[a][b] > cost:
                    cost_list[a][b] = cost
                    to_walk.put([a,b])
                    to_warp.put([a,b])


    def warp(z):
        h, w = z
        cost = cost_list[h][w] + 1
        for [p, q] in [[-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], [-1, -2], [-1, -1], [-1, 1], [-1, 2], [0, -2],
                    [0, 2], [1, -2], [1, -1], [1, 1], [1, 2], [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]]:
            a,b=h+p,w+q
            if (0 <= a < H) and (0 <= b < W):
                if maze[a][b] == "." and cost_list[a][b] > cost:
                    cost_list[a][b] = cost
                    to_walk.put([a, b])


    to_walk.put(S)
    cost_list[S[0]][S[1]] = 0

    while True:
        while not to_walk.empty():
            walk(to_walk.get())
        while not to_warp.empty():
            warp(to_warp.get())
        if to_walk.empty() and to_warp.empty():
            break

    if cost_list[G[0]][G[1]] == MAX:
        print(-1)
    else:
        print(cost_list[G[0]][G[1]])

if __name__ == "__main__":
    main()



### FIXED CODE ###

import queue
h,w=map(int,input().split())
sy,sx=map(int,input().split())
gy,gx=map(int,input().split())
s=[input() for _ in range(h)]
q=queue.deque([[sy-1,sx-1]])
q2=[[sy-1,sx-1]]
q3=[]
inf=10**7
dist=[[inf for _ in range(w)] for _ in range(h)]
d=[[1,0],[0,1],[-1,0],[0,-1]]
dist[sy-1][sx-1]=0

while q:
    y,x=q.pop()
    for dy,dx in d:
        ny,nx=y+dy,x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and dist[ny][nx]==inf:
            dist[ny][nx]=min(dist[y][x],dist[ny][nx])
            q.append([ny,nx])
            q2.append([ny,nx])
    if not q:
        for y,x in q2:
            for dy in range(-2,3):
                for dx in range(-2,3):
                    ny,nx=y+dy,x+dx
                    if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and dist[ny][nx]==inf:
                        dist[ny][nx]=min(dist[y][x]+1,dist[ny][nx])
                        q.append([ny,nx])
                        q3.append([ny,nx])
        q2=q3
        q3=[]
print(dist[gy-1][gx-1] if dist[gy-1][gx-1]!=inf else -1)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
A  =sorted(A)
for i in range(1,N):
    k = A.count(i)
    print(k)
    A = A[k:N]

print(0)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
l = [0]*N
for i in range(1,N):
  l[A[i-1]-1] += 1
for i in range(N):
  print(l[i])
### BUGGY CODE ###


import numpy as np
N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)
#minA = np.amin(A)
min2A = A[A.argsort()[1]]
for i in range(min2A+1)[::-1]:
    if np.sum((A%i)==0)>=N-1:
        print(i)
        break


### FIXED CODE ###


import numpy as np
N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)

min2A = A[A.argsort()[1]]
minA = np.amin(A)
if np.sum((A%min2A)==0)>=N-1:
    print(min2A)
elif np.sum((A%minA)==0)>=N-1:
    print(minA)
else:
    for i in range(2,min2A+1):
        if (min2A%i == 0) or (minA%i == 0):
            if(min2A%i == 0):
                j = int(min2A/i)
            elif(minA%i == 0):
                j=int(minA/i)
            if np.sum((A%j)==0)>=N-1:
                print(j)
                break
### BUGGY CODE ###

N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    while True:
        if N > K*10000:
            N = N-(K*10000)
        elif N > K*1000:
            N = N-(K*1000)
        elif N > K*100:
            N = N-(K*100)
        else:
            break

    x0 = N #7
    x1 = abs(x0-K) #3
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue

### FIXED CODE ###

N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    for i in range(18):
        while True:
            if N > K*10**(18-i):
                N = N-(K*10**(18-i))
            else:
                break

    x0 = N #7
    x1 = abs(x0-K) #3
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue
### BUGGY CODE ###

x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
for i in range(k):
  if x>0:
    x=x-d
  else:
    x=x+d
print(abs(x))

### FIXED CODE ###

x,k,d=input().split()
x=abs(int(x))
k=int(k)
d=int(d)
a=x//d
b=x-a*d
c=(x-b)/d
i=int(c)
if k<=i:
  print(abs(x)-d*k)
else:
  if (k-(i))%2==0:
    print(abs(x)-d*(i))
  else:
    print(abs(abs(x)-d*(i+1)))
### BUGGY CODE ###

# tokiomarine2020_b.py
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())
if V<=W:
    print('NO')
    exit()

if A<B:
    for i in range(T):
        A += V
        B += W
        if A>=B:
            print('YES')
            exit()
else:
    for i in range(T):
        A -= V
        B -= W
        if A<=B:
            print('YES')
            exit()
print("NO")


### FIXED CODE ###

# tokiomarine2020_b.py
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())
if V<=W:
    print('NO')
    exit()

if A<B:
    A = A+T*V
    B = B+T*W
    if A>=B:
        print('YES')
        exit()
else:
    A = A-T*V
    B = B-T*W
    if A<=B:
        print('YES')
        exit()
print("NO")