Harryxun/stf_run huggingface dataset:
from sys import stdin
def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    K = K % N
    X = 1
    C = 0
    Log = [1]
    while C <= K:
        X = A[X-1]
        C += 1
        if (X in Log) == True:
            break
      
        Log += [X]
    
    K = (K - C)%(C - Log.index(X))
 
    print(Log[Log.index(X)+K])
    
main()

### FIXED CODE ###

from sys import stdin
def main():
    N1 = stdin.readline().rstrip()
    N2 = stdin.readline().rstrip()
    if N1 == N2[:-1]:
        print('Yes')
    else:
        print('No')
    
main()
import math
n = int(input())
for i in range(1,n+1):
    c = 0
    total = n
    while 1:
        for x in range(1,math.sqrt(i)):
            for y in range(x,math.sqrt(i)):
                for z in range(y,math.sqrt(i)):
                    total = ((x+y)**2 + (z+y)**2 + (x+z)**2) / 2
                    if total == i:
                        if x==y==z:
                            c += 1
                        elif x ==y or y == z:
                            c += 3
                        else:
                            c += 6
                        break
                    elif total>i:
                        break
                if total==i:
                    break
            if total==i:
                break
        if total>i:
            break
    print(c)

### FIXED CODE ###

n = int(input())
a = [0 for i in range(n)]
c = 0
total = n
for x in range(1, n//2):
    for y in range(x, n//2):
        for z in range(y, n//2):
            total = int(((x+y)**2 + (z+y)**2 + (x+z)**2) / 2)
            try:
                if x == y == z:
                    a[total-1] += 1
                elif x == y or y == z:
                    a[total-1] += 3
                else:
                    a[total-1] += 6
            except:
                break
for i in a:
    print(i)
num=1
while True:
    maxw=input()
    if maxw==0:break
    N=input()
    ws,ps=[0]*N,[0]*N
    for i in xrange(N):
        ps[i],ws[i]=map(int,raw_input().strip().split(","))
    dp=[[0]*(maxw+1) for i in range(len(ws)+1)]
    ret=0
    for i in range(len(ws)):
        for j in range(maxw+1):
            if (j+ws[i])<=maxw:
                dp[i+1][j+ws[i]]=max(dp[i+1][j+ws[i]],dp[i][j]+ps[i])
                ret=max(dp[i+1][j+ws[i]],ret)
    print "Case %d:"%num
    print ret
    print dp[len(ws)].index(ret)
    num+=1

### FIXED CODE ###

num=1
while True:
    maxw=input()
    if maxw==0:break
    print "Case %d:"%num
    dp,w,v=[0]*1001,[0]*1001,[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(maxw,-1,-1):
            if j+w[i]<=maxw:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
    num+=1
a,b,c,d=list(map(int,input()))
print(max(a*b, c*d))

### FIXED CODE ###

a,b,c,d=list(map(int,input().split()))
print(max(a*b, c*d))
import fractions
N=int(input())
A=list(map(int,input().split()))
c=1
a=0
w=10**9+7
for i in range(N):
    c_=c*A[i]//fractions.gcd(c,A[i])
    a=a*c_/c+c_/A[i]
    c=c_
print(int(a%w))

### FIXED CODE ###

import fractions
N=int(input())
A=list(map(int,input().split()))
c=1
a=0
w=10**9+7
for A_ in A:
    c=c*A_//fractions.gcd(c,A_)
for A_ in A:
    a+=c//A_
print(int(a%w))
A,B,C,D,E,F=list(map(int, input().split()))

from fractions import gcd

highest_concentration=0

for a in range(F//(100*A) +1):
  for b in range((F-100*A*a)//(100*B) +1):
    water = 100*A*a + 100*B*b
    if not water:
      continue
    max_sugar = min(E*(A*a+B*b), F-water)
    
    lcm = C*D//gcd(C,D)
    
    #(max_sugar//lcm)*lcm
    for c in range((max_sugar%lcm)//C + 1):
      d = (max_sugar%lcm -c*C)//D
      
      sugar = (max_sugar//lcm)*lcm + c*C + d*D
      if sugar/(water+sugar) > highest_concentration:
        highest_concentration = sugar/(water+sugar)
        #print(a,b,c,d, 100*a*A+100*b*B, sugar)
        #print(water,water+sugar, sugar)
        answer = (water+sugar, sugar)
        
print(*answer)

### FIXED CODE ###

A,B,C,D,E,F=list(map(int, input().split()))

from fractions import gcd

highest_concentration=-1

for a in range(F//(100*A) +1):
  for b in range((F-100*A*a)//(100*B) +1):
    water = 100*A*a + 100*B*b
    if not water:
      continue
      
    max_sugar = min(E*(A*a+B*b), F-water)
    
    for c in range(min(D,(max_sugar//C)+1)):
      d = (max_sugar -c*C)//D
      sugar = c*C + d*D
      if sugar/(water+sugar) > highest_concentration:
        highest_concentration = sugar/(water+sugar)
        #print(a,b,c,d, 100*a*A+100*b*B, sugar)
        #print(water,water+sugar, sugar)
        answer = (water+sugar, sugar)
        
print(*answer)
# encoding:utf-8

def main():
	n = int(input())
	vers = [list(map(int, input().split())) for _ in range(n - 1)]
	q, k = list(map(int, input().split()))
	queries = [list(map(int, input().split())) for _ in range(q)]
	new_vers = mapping(n, vers)
	dist = calcMinDist(n, new_vers, k, queries)
	calcQueries(dist, queries, k)
	
def mapping(n, vers):
	new_vers = {i :[] for i in range(n)}
	for ver in vers:
		new_vers[ver[0] - 1].append((ver[1], ver[2]))
		new_vers[ver[1] - 1].append((ver[0], ver[2]))
	return new_vers

def calcMinDist(n, new_vers, k, queries):
	dist = [10000 for _ in range(n)]
	flag = [False for _ in range(n)]
	dist[k - 1] = 0
	flag[k - 1] = True
	dfs(new_vers, dist, flag, k)
	return dist

def dfs(new_vers, dist, flag, frm):
	flag[frm - 1] = True
	vs = new_vers[frm - 1]
	for v in vs:
		if flag[v[0] - 1] == False:
			dist[v[0] - 1] = dist[frm - 1] + v[1]
			new_ver, dist, flag, _ = dfs(new_vers, dist, flag, v[0])

	return [new_vers, dist, flag, frm]

def calcQuery(dist, query, k):
	print(dist[query[0] - 1] + dist[query[1] - 1])

def calcQueries(dist, queries, k):
	for query in queries:
		calcQuery(dist, query, k)

if __name__ == '__main__':
	main()

### FIXED CODE ###

# encoding:utf-8
import sys
 
def main():
	n = int(input())
	vers = [list(map(int, input().split())) for _ in range(n - 1)]
	q, k = list(map(int, input().split()))
	queries = [list(map(int, input().split())) for _ in range(q)]
 
	#n = 10 ** 5
	#vers = [[i + 1, i + 2, 10 ** 9] for i in range(n - 1)]
 
	new_vers = mapping(n, vers)
	dist = calcMinDist(n, new_vers, k, queries)
	calcQueries(dist, queries, k)
	
def mapping(n, vers):
	new_vers = {i :[] for i in range(n)}
	for ver in vers:
		new_vers[ver[0] - 1].append((ver[1], ver[2]))
		new_vers[ver[1] - 1].append((ver[0], ver[2]))
	return new_vers
 
def calcMinDist(n, new_vers, k, queries):
	dist = [- 1 for _ in range(n)]
	flag = [False for _ in range(n)]
	dist[k - 1] = 0
	flag[k - 1] = True
	dfs(new_vers, dist, flag, k)
	return dist
 
def dfs(new_vers, dist, flag, frm):
	flag[frm - 1] = True
	vs = new_vers[frm - 1]
	for v in vs:
		if flag[v[0] - 1] == False:
			dist[v[0] - 1] = dist[frm - 1] + v[1]
			new_ver, dist, flag, _ = dfs(new_vers, dist, flag, v[0])
 
	return [new_vers, dist, flag, frm]
 
def calcQuery(dist, query, k):
	print(dist[query[0] - 1] + dist[query[1] - 1])
 
def calcQueries(dist, queries, k):
	for query in queries:
		calcQuery(dist, query, k)
 
if __name__ == '__main__':
	sys.setrecursionlimit(1000000)
	main()
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    H=[int(input()) for i in range(N)]
    H.sort()
    ans=1000000000
    for i in range(N-K+1):
        temp=[H[i],H[i+1],H[i+2]]
        xam=max(temp)
        nim=min(temp)
        ans=min(xam-nim,ans)
    print(ans)
#%%submit!
resolve()

### FIXED CODE ###

#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    H=[int(input()) for i in range(N)]
    H.sort()
    #print(H)
    ans=1000000000
    for i in range(N-K+1):
        ans=min(H[i+K-1]-H[i],ans)
    print(ans)
#%%submit!
resolve()
from collections import Counter
n = int(input())
ans_list = [0]*(10**4)
for i in range(1, 100):
  for j in range(1, 100):
    for k in range(1, 100):
      ans = i**2 + j**2 + k**2 + i*j + j*k + k*i
      ans_list[ans-1] += 1
for i in ans_list[:n]:
  print(i)

### FIXED CODE ###

from collections import Counter
n = int(input())
ans_list = [0]*(10**6)
for i in range(1, 100):
  for j in range(1, 100):
    for k in range(1, 100):
      ans = i**2 + j**2 + k**2 + i*j + j*k + k*i
      ans_list[ans-1] += 1
for i in ans_list[:n]:
  print(i)
n,m = input(),input
print(n+m)

### FIXED CODE ###

string = input().split()
print(string[1]+string[0])
def mks(num):
  s=str(num)
  if len(s)<6:
    s='0'*(6-len(s))+s
  return s

n,m=map(int,input().split())
arr=[]
for i in range(m):
  p,y=map(int,input().split)
  arr.append([p,y,i])
ans=[0]*m
arr=sorted(arr,key=lambda x:x[1])
arr=sorted(arr,key=lambda x:x[0])
tmp=arr[0][0]
cnt=1
for i in range(m):
  if arr[i][0]==tmp:
    s1,s2=mks(tmp),mks(cnt)
    ans[arr[i][2]]=s1+s2
    cnt+=1
  else:
    cnt=1
    tmp=arr[i][0]
    s1,s2=mks(tmp),mks(cnt)
    ans[arr[i][2]]=s1+s2
for i in range(m):
  print(ans[i])

### FIXED CODE ###

def mks(num):
  s=str(num)
  if len(s)<6:
    s='0'*(6-len(s))+s
  return s

n,m=map(int,input().split())
arr=[]
for i in range(m):
  p,y=map(int,input().split())
  arr.append([p,y,i])
ans=[0]*m
arr=sorted(arr,key=lambda x:x[1])
arr=sorted(arr,key=lambda x:x[0])
tmp=arr[0][0]
cnt=1
for i in range(m):
  if arr[i][0]==tmp:
    s1,s2=mks(tmp),mks(cnt)
    ans[arr[i][2]]=s1+s2
    cnt+=1
  else:
    cnt=1
    tmp=arr[i][0]
    s1,s2=mks(tmp),mks(cnt)
    ans[arr[i][2]]=s1+s2
    cnt+=1
for i in range(m):
  print(ans[i])
import statistics as sta

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

sort_X = sorted(X)
a = sta.median_low(sort_X)
b = sta.median_high(sort_X)

for i in range(N):
    if X[i] <= a:
        print(b)
    else:
        print(a)

### FIXED CODE ###

import statistics as sta

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

sort_X = sorted(X)
a = sta.median_low(sort_X)
b = sta.median_high(sort_X)

for i in range(N):
    if X[i] <= a:
        print(b)
    else:
        print(a)
S = input()
K = int(input())
ans = ""

if int(S[0]) > 1:
  ans = S[0]
elif K>=100:
  for i in range(len(S)):
    if int(S[i]) > 1:
      ans = S[i]
      break
    elif i==S-1:
      ans = 1
else:
  for i in range(len(S)):
    if int(S[i]) > 1:
      if i+1 <= K:
        ans = S[i]
        break
      else:
        ans = 1
        break
    elif i==S-1:
      ans = 1
print(ans)

### FIXED CODE ###

S = input()
K = int(input())
ans = 1

for i in range(len(S)):
  if int(S[i]) == 1:
    continue
  else:
    if i+1 <= K:
      ans = S[i]
      break

print(ans)
s = input()
if ~("N" in s ^ "S" in s) and ~("E" in s ^ "W" in s):
  print("Yes")
else:
  print("No")

### FIXED CODE ###

s = input()
b, c = 0, 0
if "N" in s and "S" in s:
  b = 1
elif "N" not in s and "S" not in s:
  b = 1
if "E" in s and "W" in s:
  c = 1
elif "E" not in s and "W" not in s:
  c = 1
if b and c:
  print("Yes")
else:
  print("No")
a = input()
first = True
A = ""
B = ""
for i in range(len(a)):
    if a[i] != " " and first:
        A = A + a[i]
    else:
        first = False
        B = B + a[i]
A = int(A)
B = int(B)
count = 0
if B > A:
    while B > 0:
        B = B - A
        count = count + 1
print(count)


### FIXED CODE ###

S = input()
R = input()
count = 0
for i in range(3):
  if S[i] == R[i]:
    count = count + 1
print(count)
n, k, H = map(int, open(0).read().split())
H.sort()
ans = 10 ** 10
for h0, h1 in zip(H, H[k-1:]):
  ans = min(ans, h1-h0)
print(ans)

### FIXED CODE ###

n, k, *H = map(int, open(0).read().split())
H.sort()
ans = 10 ** 10
for h0, h1 in zip(H, H[k-1:]):
  ans = min(ans, h1-h0)
print(ans)
N=int(input())
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=map(int,input().split())
	e[a].append([b, c])
	e[b].append([a, c])
Q,K=map(int,input().split())
def bfs(now, dis):
	if d[now] != -1:
		return
	d[now] = dis
	for nxt, cos in e[now]:
		bfs(nxt, dis+cos)
bfs(K, 0)
for _ in range(Q):
	x,y=map(int,input().split())
	print(d[x]+d[y])


### FIXED CODE ###

import sys
sys.setrecursionlimit(1145141919)
N=int(input())
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=map(int,input().split())
	e[a].append((b, c))
	e[b].append((a, c))
Q,K=map(int,input().split())
def dfs(now, dis):
	d[now] = dis
	for nxt,cos in e[now]:
		if d[nxt] == -1:
			dfs(nxt, dis+cos)
dfs(K, 0)
for _ in range(Q):
	x,y=map(int,input().split())
	print(d[x]+d[y])
import numpy as np
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

A_np = np.array(A)
B_np = np.array(B)

C_np = np.append(A_np,B_np)
C_np = np.sort(C_np)
print(C_np)
x = 0
for i in range(len(C_np)):
    if K >= x + C_np[i]:
        x += C_np[i]
        y += 1
    else:
        break
print(y)

### FIXED CODE ###

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

a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
            j -= 1
    ans = max(ans, i + j)
print(ans)
#float型を許すな
#numpyはpythonで
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, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

h,w,d=MI()
mp=[LI() for i in range(h)]
lis=[[0,0] for i in range(w*h+1)]
for i in range(h):
    for j in range(w):
        lis[mp[i][j]]=[i,j]
#print(lis)
cost=[[0]*(h*w) for i in range(d)]
for i in range(d):
    cost[i][i]=0
    for j in range(1,h*w):
        if i+d*j<h*w:
            cost[i][i+d*j]=cost[i][i+d*j-d]+abs(lis[i+d*j][0]-lis[i+d*(j-1)][0])+abs(lis[i+d*j][1]-lis[i+d*(j-1)][1])
            
        
#print(cost)
q=I()
for i in range(q):
    ans=0
    l,r=MI()
    k=l%d
    print(cost[k][r]-cost[k][l])

        


    
    


    

    
            
        
        
    
    
    
    

    
    


    
    

        
        
    


    

### FIXED CODE ###

#float型を許すな
#numpyはpythonで
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, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

h,w,d=MI()
mp=[LI() for i in range(h)]
lis=[[0,0] for i in range(w*h)]
for i in range(h):
    for j in range(w):
        lis[mp[i][j]-1]=[i,j]
#print(lis)

costs=[0 for i in range(h*w+1)]
for i in range(1,d+1):
    for j in range(0,h*w):
        if i+d*(j+1)<h*w+1:
            costs[i+d*(j+1)]=costs[i+d*j]+abs(lis[i+d*(j+1)-1][1]-lis[i+d*j-1][1])+abs(lis[i+d*(j+1)-1][0]-lis[i+d*j-1][0])
        else:
            break
q=I()
for i in range(q):
    l,r=MI()
    u=l%d
    print(costs[r]-costs[l])
a,b = map(int, input().split())
if a >= 13:
    print(b):
elif 5 <= a <= 12:
    print(b//2)
else:
    print(0)

### FIXED CODE ###

a,b = map(int, input().split())
if a >= 13:
    print(b)
elif a >= 6 and a <= 12:
    print(b//2)
else:
    print(0)
def main(int_list):
  int_list.sort()
  print int_list[0], int_list[1], int_list[2] 

int_list = []
input_string = raw_input()
input_list = raw_input().split(' ')
for i in input_list:
  int_list.append(int(i))

main(int_list)

### FIXED CODE ###

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

ans = 0
t=0

while True:
    for i in range(n):  # 0 1 2 ... n-1
        if a[i] %2 ==0:
            a[i] = a[i] / 2
        else:
            t = 1
    if t != 1:
        ans += 1
    else:
        break

print(ans)


### FIXED CODE ###

s = input()
factor = [int(x) for x in s]
print(sum(factor))
import itertools
from scipy.special import comb

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

ans = [0, 0]
max_comb = 0
combs = {}
for a_i, a_j in itertools.combinations(a, 2):
    if a_j > a_i:
        tmp = a_j
        a_j = a_i
        a_i = tmp

    if (a_i, a_i - a_j) in combs:
        continue

    c = comb(a_i, a_j, exact=True)
    combs[(a_i, a_j)] = c
    # print(a_i, a_j, c)
    if c > max_comb:
        max_comb = c
        ans[0], ans[1] = a_i, a_j

print(ans[0], ans[1])


### FIXED CODE ###

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

a_i = max(a)
a_j = 0
min_d = float('inf')
for a_k in a:
    if a_k == a_i:
        continue
    d = abs(a_i / 2 - a_k)
    if d < min_d:
        min_d = d
        a_j = a_k
print(a_i, a_j)
from fractions import gcd

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

forward = []
backward = [0 for x in range(n)]

if n == 2:
    print(max(a[0], a[1]))
    exit(0)

for i in range(n):
    if i == 0:
        forward.append(a[i])
    else:
        forward.append(gcd(forward[i - 1], a[i]))


for i in range(n - 1, -1, -1):
    if i == n - 1:
        backward[i] = a[i]
    else:
        backward[i] = gcd(backward[i + 1], a[i])


ans = 1

for i in range(n):
    if i == 0:
        ans = max(ans, backward[1])
    elif i == n - 1:
        ans = max(ans, forward[n - 2])
    else:
        ans = max(ans, gcd(forward[i - 1], backward[i + 1]))

print ans


### FIXED CODE ###

from fractions import gcd

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

forward = []
backward = [0 for x in range(n)]

if n == 2:
    print(max(a[0], a[1]))
    exit(0)

for i in range(n):
    if i == 0:
        forward.append(a[i])
    else:
        forward.append(gcd(forward[i - 1], a[i]))


for i in range(n - 1, -1, -1):
    if i == n - 1:
        backward[i] = a[i]
    else:
        backward[i] = gcd(backward[i + 1], a[i])


ans = 1

for i in range(n):
    if i == 0:
        ans = max(ans, backward[1])
    elif i == n - 1:
        ans = max(ans, forward[n - 2])
    else:
        ans = max(ans, gcd(forward[i - 1], backward[i + 1]))

print(ans)
N = int(input())
M = int(input())
total = 0
 
for i in range(M):
    A = int(input())
    total = total+A
 
if total <= N:
    print (N - total)
else:
    print ("-1")

### FIXED CODE ###

import math

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

if (n-sum(a)) < 0:
    print(-1)
else:
    print(n-sum(a))
n = input()
a = n%1000
a = 1000-a
print(a)

### FIXED CODE ###

n = int(input())
a = n%1000
if a ==0:
    print(0)
    exit()
a = 1000-a
print(a)
N = int(input())
a = [sorted(list(input())) for i in range(N)]
b = set(a)
ans = len(a) - len(b)
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
a = [''.join(sorted(input())) for i in range(N)]
count = Counter(a).values()
b = 0
for i in count:
    b = b + i * (i-1)
b=int(b/2)
print(b)
n = int(input())
a = tuple(map(lambda a:int(a)-1,input()))

ans = 0
for i in range(n):
  if a[a[i]]==i:
    ans+=1
print(ans//2)

### FIXED CODE ###

n = int(input())
a = tuple(map(lambda a:int(a)-1,input().split()))

ans = 0
for i in range(n):
  if a[a[i]]==i:
    ans+=1
print(ans//2)
import sys

r = input()
print(r * r)

### FIXED CODE ###

import sys

r = input()
print(int(r) * int(r))
from collections import deque
def 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())
  s = [["#"]*(w+4) for _ in range(2)]+[["#"]*2+list(input())+["#"]*2 for _ in range(h)]+[["#"]*(w+4) for _ in range(2)]
  m1 = ((-1,0),(0,-1),(0,1),(1,0))
  m2 = [(i,j) for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]
  a = deque([(ch,cw)])
  b = deque()
  i = 0
  while a:
    while a:
      ah,aw = a.popleft()
      if s[bh][bw]!=".":
        continue
      b.append((ah,aw))
      s[ah][aw] = i
      for bh,bw in m1:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    while b:
      ah,aw = b.popleft()
      for bh,bw in m2:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    i += 1
  ans = s[dh][dw]
  print(ans if ans != "." else -1)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import deque
def 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())
  s = [["#"]*(w+4) for _ in range(2)]+[["#"]*2+list(input())+["#"]*2 for _ in range(h)]+[["#"]*(w+4) for _ in range(2)]
  m1 = ((-1,0),(0,-1),(0,1),(1,0))
  m2 = [(i,j) for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]
  a = deque([(ch,cw)])
  b = deque()
  i = 0
  while a:
    while a:
      ah,aw = a.popleft()
      if s[ah][aw]!=".":
        continue
      b.append((ah,aw))
      s[ah][aw] = i
      for bh,bw in m1:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    while b:
      ah,aw = b.popleft()
      for bh,bw in m2:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    i += 1
  ans = s[dh][dw]
  print(ans if ans != "." else -1)

if __name__ == "__main__":
    main()
n, y = map(int, input().split())
for i in range(n+1):
    for j in range(n-i+1):
        if 10000 * i + 5000 * j +1000(n-i-j) == y:
            print(i, j, n-i-j)
            exit() 
print("-1 -1 -1")

### FIXED CODE ###

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

for i in range(n+1):
    for j in range(n-i+1):
        if 10000 * i + 5000 * j + 1000 * (n-i-j)==y:
            print(i, j, n-i-j)
            exit()
print("-1 -1 -1")
import numpy as np
n = int(input())
a = np.array(range(1,n+1))
b = np.array([a[(i+1)%n] for i in range(n)])
sum(a%b)import numpy as np
n = int(input())
a = np.array(range(1,n+1))
b = np.array([a[(i+1)%n] for i in range(n)])
print(sum(a%b))

### FIXED CODE ###

import numpy as np
n = int(input())
print(n*(n-1)//2)
#!/usr/bin/env pypy

MAX = 10**4 + 1

n, k = map(int, input().split())
cost = list(map(int, input().split()))
dp = [MAX] * (n)
dp[0] = 0

for i in range(1, n):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - K), i))

print(dp[-1])


### FIXED CODE ###

def jump(i):
    memo[0] = 0
    memo[1] = abs(arr[1]- arr[0])               
    for i in range(2, n):
        memo[i] = min(memo[i-1] + abs(arr[i-1]- arr[i]) ,
                      memo[i-2] + abs(arr[i-2]- arr[i]))
    return memo[-1]    

n = int(input())
arr = list(map(int, input().split()))
memo = [-1] * n 
                   
result = jump(n)
print(result)
a,b = map(int, input(),split())

rest = a - b*2
if rest >-1:
    print(rest)
else:
    print(0)

### FIXED CODE ###

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

rest = a - b*2
if rest >-1:
    print(rest)
else:
    print(0)