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

h, w, d = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]

pos = [0] * (h*w + 1)
for i in range(h):
    for j in range(w):
        pos[a[i][j]] = (j, i)

res = []
q = int(input())
for i in range(q):
    l, r = map(int, input().split())

    ans = 0
    px, py = pos[l]
    for j in range(l, r+1, d):
        x, y = pos[j]
        ans += abs(px - x) + abs(py - y)
        px, py = pos[j]

    res.append(ans)

print("\n".join(map(str,res)))


### FIXED CODE ###

h, w, d = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]

pos = [0] * (h*w + 1)
for i in range(h):
    for j in range(w):
        pos[a[i][j]] = (j, i)

dis = [0] * (h*w + 1)
for i in range(h*w+1):
    if i > d:
        x1, y1 = pos[i-d]
        x2, y2 = pos[i]
        dis[i] = abs(x1-x2) + abs(y1-y2) + dis[i-d]

res = []
q = int(input())
for i in range(q):
    l, r = map(int, input().split())

    res.append(dis[r]-dis[l])

print("\n".join(map(str,res)))
### BUGGY CODE ###

N = int(input())
S = input()
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      diff_ji = j - i
      for k in range(j+1, N):
        if S[i] != S[k] and S[j] != S[k] and diff_ji != k - j:
          count += 1
print(count)


### FIXED CODE ###

N = int(input())
S = input()
rcount = 0
gcount = 0
bcount = 0
for i in range(N):
  if S[i] == 'R':
    rcount += 1
  elif S[i] == 'G':
    gcount += 1
  elif S[i] == 'B':
    bcount += 1

total = rcount * gcount * bcount
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2*j - i
    if k < N and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      count += 1
print(total - count)
### BUGGY CODE ###


import itertools

K,S = map(int, input().split())
X = list(range(K+1))
count=0

for x,y in list(itertools.product(X, X)):
    if x+y <= S and x+y+K>=S:
        count+=1

print(count)

### FIXED CODE ###

k,s = map(int, input().split())
cnt = 0

for x in range(k+1):
    for y in range(k+1):
        if 0 <= s-(x+y) <= k:
            cnt += 1




print(cnt)
### BUGGY CODE ###

n = int(input())

a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))

sum = 0
for i in range(n):
    bs = [bi for bi in b if a[i] < bi]
    if len(bs) == 0:
        continue
    for j in range(len(bs)):
        cs = [ci for ci in c if bs[j] < ci]
        sum += len(cs)

print(sum)



### FIXED CODE ###

import bisect

n = int(input())

a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))

sum = 0
for i in range(len(b)):
    ai = bisect.bisect_left(a, b[i])
    ci = bisect.bisect_right(c, b[i])
    sum += ai * (n - ci)

print(sum)
### BUGGY CODE ###

n = int(input())
mat = [[0] * n for _ in range(n)]
d, f = [0] * n, [0] * n
for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        mat[i - 1][j - 1] = 1

time, current = 1, 0
stack = []
d[current] = time
while 0 in f:
    if not stack:
        stack.append(0)
    time += 1
    current = stack[-1]
    for i in range(len(mat[current])):
        if mat[current][i] and not d[i]:
            current = i
            d[i] = time
            stack.append(i)
            break
    else:
        f[current] = time
        current = stack.pop()

for l in zip(range(1, n + 1), d, f):
    print(*l)

### FIXED CODE ###

n = int(input())
mat = [[0] * n for _ in range(n)]
d, f = [0] * n, [0] * n
for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        mat[i - 1][j - 1] = 1

time, current = 0, 0
stack = []
while 0 in f:
    if not stack:
        for i, v in enumerate(f):
            if v == 0:
                stack.append(i)
                time += 1
                d[i] = time
                break
    time += 1
    current = stack[-1]
    for i in range(len(mat[current])):
        if mat[current][i] and not d[i]:
            current = i
            d[i] = time
            stack.append(i)
            break
    else:
        f[current] = time
        current = stack.pop()

for l in zip(range(1, n + 1), d, f):
    print(*l)
### BUGGY CODE ###

N,K=map(int,input().split())
P=list(map(int,input().split()))
A=[0]
for k in range(0,N-K+1):
  On=0
  for l in range(0,K):
    On+=P[k+l]
    A.append(On)

print((max(A)+K)/2)

### FIXED CODE ###

#模範解答の通りにやってみる
N,K=map(int,input().split())
P=list(map(int,input().split()))
if K==1:
	print((max(P)+1)/2)
elif N==K:
	print((sum(P)+N)/2)
else:
	T=sum(P[0:(K)])
	S=sum(P[0:(K)])
	for k in range(0,N-K):
		S=S-P[k]+P[k+K]
		if T<=S:
			T=S
		else:
			continue
	print((T+K)/2)
### BUGGY CODE ###

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

now = 1
flag = 1
memo = []
cnt = 0
while flag:
    next = a_lis[now-1]
    if next in memo:
        print(-1)
        break
    else:
        memo.append(next)
        now = next
    cnt+=1
    if next ==2:
        print(cnt)
        break


### FIXED CODE ###

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

now = 1
flag = 1
cnt = 0
while cnt<N and next!=2:
    cnt+=1
    next = a_lis[now-1]
    now = next
if next==2:print(cnt)
else:print(-1)
### BUGGY CODE ###

import math
n = int(input())
k = int(math.sqrt(n))
for i in range(k,n+1):
  if n%i == 0:
    a = i
    break
s = int(math.log(a,10))+1
print(s)

### FIXED CODE ###

import math
n = int(input())
k = int(math.sqrt(n))
for i in range(k+2,0,-1):
  if n%i == 0:
    a = n//i
    break
s = len(str(a))
print(s)
### BUGGY CODE ###

n = int(input())
a = [int(input()) for i in range(n)]

x = 1
y = []

while x not in y and x != 2:
    y += [x]
    x = a[x-1]

if x == 2:
    print(len(y))
else:
    print(-1)


### FIXED CODE ###

n = int(input())
a = [int(input()) for i in range(n)]

x = 1
i = 0

while i < 100000 and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)
### BUGGY CODE ###

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

a = list(map(int, input().split()))

visited = [0]
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if now not in visited:
        visited.append(now)
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)


### FIXED CODE ###

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

a = list(map(int, input().split()))

visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if is_visited[now-1] == 0:
        is_visited[now-1] = 1
        visited.append(now)
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)
### BUGGY CODE ###

# python3だとTLE,pypy3だとAC
def main():
    N, W = map(int, input().split())
    w = [None for _ in range(N)]
    v = [None for _ in range(N)]
    for i in range(N):
        w[i], v[i] = map(int, input().split())
    MAX_V = sum(v)
    dp = [[10**9 for _ in range(MAX_V+1)] for _ in range(N)]
    dp[0][0] = 0
    for i in range(N-1):
        if i == 0:
            for sum_v in range(1, v[i]+1):
                dp[i][sum_v] = w[i]
        for sum_v in range(MAX_V+1):
            if sum_v - v[i+1] >= 0:
                dp[i+1][sum_v] = min(w[i+1] + dp[i]
                                     [sum_v-v[i+1]], dp[i][sum_v])
            else:
                dp[i+1][sum_v] = min(w[i+1], dp[i][sum_v])
    print(len(list(filter(lambda sum_v: sum_v <= W, dp[-1])))-1)


if __name__ == "__main__":
    main()


### FIXED CODE ###

# python3だとTLE,pypy3だとAC
def main():
    N, W = map(int, input().split())
    w = [None for _ in range(N)]
    v = [None for _ in range(N)]
    for i in range(N):
        w[i], v[i] = map(int, input().split())
    MAX_V = sum(v)
    dp = [[10**9 for _ in range(MAX_V+1)] for _ in range(N)]
    dp[0][0] = 0
    for i in range(N-1):
        if i == 0:
            for sum_v in range(1, v[i]+1):
                dp[i][sum_v] = w[i]
        for sum_v in range(MAX_V+1):
            if sum_v - v[i+1] >= 0:
                dp[i+1][sum_v] = min(w[i+1] + dp[i]
                                     [sum_v-v[i+1]], dp[i][sum_v])
            else:
                dp[i+1][sum_v] = min(w[i+1], dp[i][sum_v])
    print(len(list(filter(lambda sum_v: sum_v <= W, dp[-1])))-1)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

import math
A,B,N = map(int,input().split())
ret = 0
ans = 0
for x in range(1,N+1):
  temp = math.floor(A*x/B) - A*math.floor(x/B)
  if temp > ret:
    ret = temp
    ans = x
print(ret)

### FIXED CODE ###

import math
A,B,N = map(int,input().split())
ret = 0
ans = 0
if B == 0:
  print(0)
  exit()
if B > N:
  base = N
else:
  base = B - 1
ret = math.floor(A*base/B) - A*math.floor(base/B)
print(ret)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(0)
for i in range(n):
    p = a[i + 1]
    del a[i + 1]
    ans = 0
    for j in range(len(a) - 1):
        ans += abs(a[j] - a[j + 1])
    a.insert(i + 1, p)
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(0)
ans = 0
for j in range(len(a) - 1):
    ans += abs(a[j] - a[j + 1])
    
for i in range(n):
    p = abs(a[i + 2] - a[i])
    q = abs(a[i + 1] - a[i])
    r = abs(a[i + 1] - a[i + 2] )
    print(ans + p - q - r)
### BUGGY CODE ###

N = int(input())
S = input()

s = 0
for a in range(N):
    s1 = S[a]
    for b in range(a+1, N):
        s2 = S[b]
        if s1 == s2:
            continue
        n = S[b+1:]
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = n.count(pat[0])
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)


### FIXED CODE ###

N = int(input())
S = input()

s_l = {}
for i in range(N-1):
    t = {}
    s = S[i+1:]
    t['s'] = s
    t['R'] = s.count('R')
    t['G'] = s.count('G')
    t['B'] = s.count('B')
    s_l[i] = t

s = 0
for a in range(N-2):
    s1 = S[a]
    for b in range(a+1, N-1):
        s2 = S[b]
        if s1 == s2:
            continue
        n = s_l[b]['s']
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = s_l[b][pat[0]]
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)
### BUGGY CODE ###

from bisect import bisect_right as br
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]

AB.sort()
A=[a for a,b in AB]
B=[b for a,b in AB]
works=[]
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  #worksにlatest日以内に報酬を貰える仕事を追加する
  for _ in range(br(A,latest)):
    A.pop(0)
    works.insert(br(works,B[0]),B[0])
    B.pop(0)
  if len(works)==0:
    continue
  ans+=works.pop(-1)

print(ans)









### FIXED CODE ###

import heapq
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]

AB.sort(reverse=True)
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-1][0]<=latest:
    a,b=AB.pop()
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)
### BUGGY CODE ###

from collections import deque

def bfs(idx,k):
  q = deque()
  visited[idx] = True
  q.append(idx)
  color = 0
  while q:
    r = q.pop()
    col = graph[r].values()
    for n in set(graph[r].keys()):
      if not visited[n]:
        if color not in col:
          graph[r][n] = color
          graph[n][r] = color
          color = (color+1)%k
        else:
          color = (color+1)%k
          graph[r][n] = color
          graph[n][r] = color
          color = (color+1)%k
        visited[n] = True
        q.append(n)

        
N = int(input())
graph = {}
visited = [False]*(N+1)
for i in range(N+1):
  graph[i] = {}
ab = []
for i in range(N-1):
  a,b = map(int,input().split())
  ab.append((a,b))
  graph[a][b] = -1
  graph[b][a] = -1
  
k = 0
for i in range(1,N+1):
  k = max(k,len(graph[i]))

bfs(1,k)
print(k)
for i in range(N-1):
  a,b = ab[i]
  print(graph[a][b]+1)

  


  


### FIXED CODE ###

from collections import deque

def bfs(idx,k,colored):
  q = deque()
  visited[idx] = True
  q.append(idx)
  while q:
    r = q.pop()
    color = 0
    for n in graph[r].keys():
      if not visited[n]:
        if color == colored[r]: color+=1
        colored[n] = color
        graph[r][n] = color
        graph[n][r] = color
        color += 1

        visited[n] = True
        q.append(n)

        
N = int(input())
graph = {}
colored = [-1]*(N+1)
visited = [False]*(N+1)
for i in range(N+1):
  graph[i] = {}
ab = []
for i in range(N-1):
  a,b = map(int,input().split())
  ab.append((a,b))
  graph[a][b] = -1
  graph[b][a] = -1
  
k = max([len(graph[i]) for i in range(1,N+1)])

bfs(1,k,colored)
print(k)
for a,b in ab:
  print(graph[a][b]+1)
### BUGGY CODE ###

from collections import Counter 
n = int(input())
a = sorted(list(map(int, input().split())))
s = sorted(set(a))
c = Counter(a)
t = [0] * (10**6 + 1)
for i in a:
    for j in range(i, 10**6+1, i):
        t[j] += 1
print(sum(1 for i in a if t[i] == 1 and c[i] == 1))



### FIXED CODE ###

from collections import Counter 
n = int(input())
a = list(map(int, input().split()))
s = set(a)
c = Counter(a)
t = [0] * (10**6 + 1)
for i in s:
    for j in range(i, 10**6+1, i):
        t[j] += 1

ans = len([1 for i in s if t[i] == 1 and c[i] == 1])
print(ans)
### BUGGY CODE ###

import math


N,H = map(int, input().split())
A = []
B = []
for _ in range(N):
    a,b = map(int, input().split())
    A.append(a)
    B.append(b)

nage_count = 0
huri_count = 0
for i in range(N):
  if B[i] > max(A):
    nage_count += 1
    H -= B[i]
  if H <= 0:
    break

# 今度は一番強いやつを振り続ける. 
if H>0:
  huri_count = math.ceil(H/max(A))
print(huri_count + nage_count)

### FIXED CODE ###

import math


N,H = map(int, input().split())
A = []
B = []
for _ in range(N):
    a,b = map(int, input().split())
    A.append(a)
    B.append(b)

nage_count = 0
huri_count = 0
A_max = max(A)
for b in sorted(B, reverse = True):
  if b > A_max:
    nage_count += 1
    H -= b
    if H <= 0:
      break
  else: 
    break

# 今度は一番強いやつを振り続ける. 
if H>0:
  huri_count = math.ceil(H/A_max)
print(huri_count + nage_count)
### BUGGY CODE ###

n=int(input())
a=input().split()
for i in range(n):
    c=[]
    for j in range(n):
        if i!=j:
            c.append(a[j])
    sum=0
    for k in range(n):
        if k==0:
            sum+=abs(int(c[k]))
        elif k==n-1:
            sum+=abs(int(c[k-1]))
        else:
            sum+=abs(int(c[k-1])-int(c[k]))
    print(sum)

### FIXED CODE ###

n=int(input())
a=[0]+input().split()+[0]
sum=0
for i in range(n+1):
    sum+=abs(int(a[i])-int(a[i+1]))
for j in range(n):
    print(sum-(abs(int(a[j])-int(a[j+1]))+abs(int(a[j+1])-int(a[j+2]))-abs(int(a[j])-int(a[j+2]))))
### BUGGY CODE ###

from itertools import product
 
N = int(input())
S = [int(input()) for _ in range(N)]
 
mmax = 0
for pair in product(range(2), repeat=N):
  sum = 0
  for i in range(len(pair)):
    sum += S[i] * pair[i]
  if sum % 10 == 0:
    continue
  mmax = max(mmax, sum)
print(mmax)

### FIXED CODE ###

N = int(input())
S = sorted([int(input()) for _ in range(N)])
 
m = 0
for s in S:
  if s % 10 != 0 and m == 0:
    m = s
    break
ans = sum(S)
if ans % 10 != 0:
  print(ans)
elif m != 0:
  print(ans-m)
else:
  print(0)
### BUGGY 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
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

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

    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n-1)]

    adj = [[] for _ in range(n)]
    for a,b in ab:
        adj[a-1].append(b-1)
        adj[b-1].append(a-1)

    k = 0
    for i in adj:
        k = max(k, len(i))
    print(k)


    c = [[0]*n for _ in range(n)]
    d = deque([[0, 0]])
    visited = [0]*n
    visited[0] = 1
    c_l = [i for i in range(1, k+1)]
    while d:
        node, color = d.popleft()
        if color == 0:
            color_list = c_l
        else:
            color_list = c_l[:color-1] + c_l[color:]
        cnt = 0
        for x in adj[node]:
            if visited[x]:
                continue
            visited[x] = 1
            c[node][x] = color_list[cnt]
            d.append([x, color_list[cnt]])
            cnt += 1

    for a,b in ab:
        print(c[a-1][b-1])



if __name__ == '__main__':
    main()

### FIXED 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
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

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

    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n-1)]

    adj = [[] for _ in range(n)]
    order = []
    for a,b in ab:
        adj[a-1].append(b-1)
        order.append(b-1)

    

    c = [0]*n
    d = deque([0])
    while d:
        v = d.popleft()
        cnt = 1
        for nv in adj[v]:
            if c[v] == cnt:
                cnt += 1
            c[nv] = cnt
            cnt += 1
            d.append(nv)

    print(max(c))

    for i in order:
        print(c[i])

if __name__ == '__main__':
    main()
### BUGGY CODE ###

# -*- coding: utf-8 -*-
# 整数の入力
import sys
n = int(input())
li = list(map(int, input().split()))

count = 0
table = {}

for i in range(len(li)) :
  sum = 0
  if i in table.keys() :
    count += table[i]
    continue

  match_i = list()

  for j in range(len(li)-i) :
    sum = sum + li[i+j]
    if sum == 0:
      count = count + 1
      for k in match_i:
        table[k] = table[k] + 1
      table[i+j+1] = 0
      match_i.append(i+j+1)

print(count)
#print(table)

### FIXED CODE ###

def tri(n):
  return n * (n+1) // 2

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

SA = [0] * (N+1)
for i in range(N):
  SA[i+1] = SA[i] + A[i]
  
SA.sort()

ans = 0
cnt = 0

for i in range(N+1):
  if i == 0:
    now = SA[i]
    continue
  
  if now == SA[i]:
    cnt += 1
    continue
  elif cnt > 0:
    ans += tri(cnt)
    cnt = 0
  else:
    pass
  now = SA[i]

if cnt > 0:
    ans += tri(cnt)
    
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
d=input().split()
stopcode=False
while stopcode==False:
  for i in range(len(str(n))):
    if str(n)[i] in d:
      n+=1
      break
    elif i==len(str(n)):
      stopcode=True
      break
    else:
      break
print(n)

### FIXED CODE ###

n,k=map(int,input().split())
d=input().split()
stopcode=False
while stopcode==False:
  for i in range(len(str(n))):
    if str(n)[i] in d:
      n+=1
      break
    elif i+1==len(str(n)):
      stopcode=True
      break
print(n)
### BUGGY CODE ###

a,b,c,d = map(int,input().split())

maxxy=a*c

for x in range(a,b+1):
    for y in range(c,d+1):
        if maxxy < x*y :
            maxxy=x*y
        
print(maxxy)

### FIXED CODE ###

a,b,c,d = map(int,input().split())

maxxy=max(a*c,a*d,b*c,b*d)

print(maxxy)
### BUGGY CODE ###

import numpy as np
n = int(input())
s = np.array([i for i in input()])

r = np.sum(s == 'R')
b = np.sum(s == 'B')
g = np.sum(s == 'G')

    
ct = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ct += 1                

print(r*g*b-ct)

### FIXED CODE ###

n = int(input())
s = input()

r = s.count("R")
g = s.count("G")
b = s.count("B")
 
ans = r*g*b
    
ct = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ct += 1                

print(r*g*b-ct)
### BUGGY CODE ###

n,k=map(int,input().split())
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

time=[]
def measureTime(i,regX,nowTime,nowPlace,direct,turnFlg):
    if i<k:
        if len(regX)>1:
            newRegX=regX[:]
            newRegX.pop(nowPlace)
            if (nowPlace>0) & ((direct==0)|(turnFlg!=1)):
                newTimeM=nowTime+abs(regX[nowPlace]-regX[nowPlace-1])
                # print('{},place={},time={}'.format(newRegX,nowPlace-1,newTimeM))
                if direct==1:
                    turnFlg=1
                measureTime(i+1, newRegX, newTimeM, nowPlace-1, 0, turnFlg)
            if (nowPlace<len(regX)-1) & ((direct==1)|(turnFlg!=1)):
                newTimeP=nowTime+abs(regX[nowPlace]-regX[nowPlace+1])
                # print('{},place={},time={}'.format(newRegX,nowPlace,newTimeP))
                if direct==0:
                    turnFlg=1
                measureTime(i+1, newRegX, newTimeP, nowPlace, 1, turnFlg)
        else:
            if i==k:
                time.append(nowTime)
    else:
        time.append(nowTime)

measureTime(0,x,0,x.index(0),-1,0)
time.sort()
print(time[0])


### FIXED CODE ###

n,k=map(int,input().split())
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

zeroPos=x.index(0)
leftLen=zeroPos
rightLen=len(x)-zeroPos-1
# print(leftLen,zeroPos,rightLen)
# print(x)
left=[]
right=[]
for turn in range(leftLen+1):
    if(turn>k):
        break
    if(k-turn<=rightLen):
        left.append(x[zeroPos-turn]*-2 + x[zeroPos+k-turn])
for turn in range(rightLen+1):
    if(turn>k):
        break
    if(k-turn<=leftLen):
        right.append(2*x[zeroPos+turn] + -1*x[zeroPos-k+turn])
minL=min(left)
minR=min(right)
# print(left,right)
print(minL if minL<minR else minR)
### BUGGY CODE ###

import copy

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
A_Left = copy.copy(A)
duplicatedList = []
while len(A_Left) > 0:
    if A_Left[0] in A_Left:
        kinds += 1
        duplicatedList.append(A.count(A_Left[0]))
        for j in range(A_Left.count(A_Left[0])):
            A_Left.remove(A_Left[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    while kinds > K:
        kinds -= 1
        tmp += duplicatedList[i]
        i += 1
    print(tmp)

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
l=[]
for i in range(n):
    try:
        l[d[a[i]]]+=1
    except:
        d[a[i]]=len(l)
        l.append(1)
ans=0
l.sort()
for i in range(len(l)-k):
    ans+=l[i]
print(ans)
### BUGGY CODE ###

N=int(input())
D=list(map(int,input().split()))
if D[0]==0:
  c=1
  for i in range(max(D)-1):
    c*=D.count(i+1)**D.count(i+2)
  ans=c%998244353
  print(ans)
else:
  print(0)
  

### FIXED CODE ###

import collections


N=int(input())
D=list(map(int,input().split()))
A=collections.Counter(D)
if D[0]==0 and len(set(D))==max(D)+1 and A[0]==1:
  c=1
  for i in range(max(D)):
    c*=A[i]**A[i+1]
  ans=c%998244353  
  print(ans)
else:
  print(0)