Harryxun/stf_run huggingface dataset:
import numpy as np
N = int(input())
Hs = np.array(list(map(int, input().split())))
As = np.tile(Hs, (N, 1))
h = As.T.copy()
L = np.array([i for i in range(N)])
idxs = np.tile(L, (N, 1))
tidxs = idxs.T.copy()
try:
    co = As + h - np.abs(idxs-tidxs)
except:
    co = As
ans = N**2-np.count_nonzero(co)
print(ans//2)

### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [A[i]+i for i in range(N)]
R = [i-A[i] for i in range(N)]
c = Counter(R)

ans = 0
for l in L:
    ans += c[l]

print(ans)
import math

# inputList=[]
# for i in range(6):
#     inputNum = input()
#     inputList.append(inputNum)
inputa = input().split()
# inputb = input().split()

# inputa = [int(n) for n in inputa]
# inputa.sort()

# inputa = list(inputa[0])

a = int(inputa[0])
# b = int(inputa[1])
# c = int(inputa[2])

# x = int(inputb[0])
# y = int(inputb[1])

if a <= 8 and b <= 8:
    print("Yay!")
else:
    print(":(")
    


### FIXED CODE ###

import math

# inputList=[]
# for i in range(6):
#     inputNum = input()
#     inputList.append(inputNum)
inputa = input().split()
# inputb = input().split()

# inputa = [int(n) for n in inputa]
# inputa.sort()

# inputa = list(inputa[0])

a = int(inputa[0])
b = int(inputa[1])
# c = int(inputa[2])

# x = int(inputb[0])
# y = int(inputb[1])

if a <= 8 and b <= 8:
    print("Yay!")
else:
    print(":(")
jj

### FIXED CODE ###

x,y,z=map(int,input().split())
i,sum=0,0
while True:
    sum+=y+z
    i+=1
    if sum>x-z:
        print(i-1)
        break
import sys
def f(y,x):
    f=0
    if m[y][x]==1:
        m[y][x]=c
        f=1
    return f
def LR(y,x,dx):
    P=[]
    x+=dx
    while 0<=x<12 and m[y][x]!=0:
        if f(y,x):P+=[(y,x)]
        x+=dx
    return P
def UD(y,x,dy):
    P=[]
    y+=dy
    while 0<=y<12 and m[y][x]!=0:
        if f(y,x):P+=[(y,x)]
        y+=dy
    return P
def v1(buf):
    f=0
    while len(buf)>0:
        f=1
        y,x=buf.pop()
        m[y][x]=c
buf=list(set(buf+LR(y,x,-1)+LR(y,x,1)+UD(y,x,-1)+UD(y,x,1)))
    return f
def solve():
    global c
    buf=[]
    c=2
    for i in A:
        x=m[i]
        while x.count(1)>0:
            buf+=[(i,x.index(1))]
            if v1(buf):c+=1
    return c-2
A=range(12)
m=[[0 for x in A] for y in A]
y=0
for s in sys.stdin:
    if y<12:
        x=0
        for e in map(int,list(s[:-1])):
            m[y][x]=e
            x+=1
        y+=1
    else:
        print solve()
        y=0
print solve()

### FIXED CODE ###

import sys
A=range(14)
m=[[0 for i in A]for j in A]
def f(y,x):
    if m[y][x]==1:
        m[y][x]=0
        f(y,x+1)
        f(y,x-1)
        f(y+1,x)
        f(y-1,x)
    return 1
def solve():
    c=0
    for i in A:
        while m[i].count(1)>0:c+=f(i,m[i].index(1))
    print c
    return 0
y=0
for s in sys.stdin:
    if y<12:
        y+=1
        m[y]=[0]+map(int,list(s[:-1]))+[0]
    else:y=solve()
solve()
N,X,T=map(int,input(),split())
if N/X==0:
  print(N/X*T)
else:
  print((N//X+1)*T)


### FIXED CODE ###

n,x,t=map(int,input().split())
if n%x==0:
  print(n//x*t)
else:
  print((n//x+1)*t)
N,M = map(int, input().split())

A = []
B = []
for i in range(N):
    A.append(input())
for i in range(M):
    B.append(input())


for i in range(N-M):
    x = A[i].find(B[0])
    if x != -1:
        result = True
        break
    result = False

for j in range(1,M):
    if (j >= M or j >= N ):
        result = False
        break
    idx = A[j][x:].find(B[j])
    if idx != x:
        result = False
        break

if result == True:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import sys
if __name__ == '__main__':

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

    A = []
    B = []
    for i in range(N):
        A.append(input())
    for i in range(M):
        B.append(input())

    char = B[0][0]
    matc = False
    for i in range(N-M+1):
        for j in range(N-M+1):
            if A[i][j] == char:
                matc = True
                for k in range(M):
                    for l in range(M):
                        if A[i+k][j+l] == B[k][l]:
                            matc = True
                        else:
                            matc = False
                            break
                    if not matc:
                        break
                if matc == True:
                    print("Yes")
                    sys.exit(0)

    if matc == True:
        print("Yes")
    else:
        print("No")
S = input()
answer = "WA"

if S[0] == 'A' and S[2:-1].count('C') == 1:
    C = S.index('C')
    exception = [0]
    exception.append(C)

if len(exception) == 2:
    for j in range(len(S)):
        if j not in exception and S[j].islower():
            answer = "AC"

print(answer)


### FIXED CODE ###

S = input()
count = 0
exception = [0]
answer = "WA"

if S[0] == 'A' and S[2:-1].count('C') == 1:
    C = S.index('C')
    exception.append(C)

if len(exception) == 2:
    for j in range(len(S)):
        if j not in exception and S[j].islower():
            count += 1

if count == len(S)-2:
    answer = "AC"

print(answer)
N,M=map(int,input().split())
ks=[list(map(int,input())) for _ in range(M)]
p=list(map(int,input().split()))
ans=0
for s in range(1<<M):
    for j in range(M):
        cnt=0
        for h in ks[j][1:]:
            h-=1
            if (s>>h)&1:
                cnt+=1
                cnt%=2
        if cnt!=ks[j][0]:
            break
    else:
        ans+=1
print(ans)

### FIXED CODE ###

N,M=map(int,input().split())
ks=[list(map(int,input().split())) for _ in range(M)]
p=list(map(int,input().split()))
ans=0
for s in range(1<<N):
    for j in range(M):
        cnt=0
        for h in ks[j][1:]:
            h-=1
            if (s>>h)&1:
                cnt+=1
                cnt%=2
        if cnt!=p[j]:
            break
    else:
        ans+=1
print(ans)
s = 'pink'
if a >= 3200:
    print(s)
elif a < 3200:
    print('red')

### FIXED CODE ###

a = int(input())
s = input()

if a >= 3200:
    print(s)
elif a < 3200:
    print('red')
import sys
N,M,P = map(int,input().split())
N-=1 #頂点番号を0スタートに
INF = float("inf")
edge = []
to = [[] for _ in range(N+1)] #普通に移動できる点
rto = [[] for _ in range(N+1)] #逆に戻れる点
reachablefrom = [False for _ in range(N+1)] #始点0から終点N-1へ行けるかチェック
reachableto = [False for _ in range(N+1)] #終点N-1から始点0へ戻れるかチェック
OK = [False for _ in range(N+1)] #どちらからもいける点を検出。ここ以外の点はどうでもよい。
for i in range(M):
  a,b,c = map(int,input().split())
  a-=1;b-=1 #頂点番号を0スタートに
  c-=P #ここで先に最後に払う分を引いておく。
  c = -c #最短経路にしたいのでコストを負にする。
  edge.append((a,b,c))
  to[a].append(b)
  rto[b].append(a)

def dfs(v):
  if reachablefrom[v]:
    return
  reachablefrom[v] = True
  for u in to[v]:
    dfs(u)

def rdfs(v):
  if reachableto[v]:
    return
  reachableto[v] = True
  for u in rto[v]:
    rdfs(u)
    
dfs(0)
rdfs(N)
for i in range(len(OK)): #len(OK)でもまあ良いか。
  OK[i] = reachablefrom[i]&reachableto[i]
#ここからベルマンフォード法
d = [INF for _ in range(N+1)]
d[0] = 0 #スタートのコストは0
upd = True #サイクルで更新の有無をチェック
step = 0
while upd:
  upd = False
  for i in range(M):
    a,b,c = edge[i]
    if not OK[a] or not OK[b]:
      continue
    new = d[a]+c
    if new < d[b]: #aから来たほうが最短
      upd = True
      d[b] = new
  step += 1
  if step > N:
    print("-1")
    sys.exit()
ans = -d[N] #最後、コストをマイナスで戻す
ans = max(0,ans)
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(500000) #これがないと再帰の回数オーバーでエラー出る。
N,M,P = map(int,input().split())
N-=1 #頂点番号を0スタートに
INF = float("inf")
edge = []
to = [[] for _ in range(N+1)] #普通に移動できる点
rto = [[] for _ in range(N+1)] #逆に戻れる点
reachablefrom = [False for _ in range(N+1)] #始点0から終点N-1へ行けるかチェック
reachableto = [False for _ in range(N+1)] #終点N-1から始点0へ戻れるかチェック
OK = [False for _ in range(N+1)] #どちらからもいける点を検出。ここ以外の点はどうでもよい。
for i in range(M):
  a,b,c = map(int,input().split())
  a-=1;b-=1 #頂点番号を0スタートに
  c-=P #ここで先に最後に払う分を引いておく。
  c = -c #最短経路にしたいのでコストを負にする。
  edge.append((a,b,c))
  to[a].append(b)
  rto[b].append(a)

def dfs(v):
  if reachablefrom[v]:
    return
  reachablefrom[v] = True
  for u in to[v]:
    dfs(u)

def rdfs(v):
  if reachableto[v]:
    return
  reachableto[v] = True
  for u in rto[v]:
    rdfs(u)
    
dfs(0)
rdfs(N)
for i in range(len(OK)): #len(OK)でもまあ良いか。
  OK[i] = reachablefrom[i]&reachableto[i]
#ここからベルマンフォード法
d = [INF for _ in range(N+1)]
d[0] = 0 #スタートのコストは0
upd = True #サイクルで更新の有無をチェック
step = 0
while upd:
  upd = False
  for i in range(M):
    a,b,c = edge[i]
    if not OK[a] or not OK[b]:
      continue
    new = d[a]+c
    if new < d[b]: #aから来たほうが最短
      upd = True
      d[b] = new
  step += 1
  if step > N+1:
    print("-1")
    sys.exit()
ans = -d[N] #最後、コストをマイナスで戻す
ans = max(0,ans)
print(ans)
import sys
input()
A = list(map(int, input().split()))
ans = 1

if 0 in A:
    print(0)
    sys.exit()

else
    for a in map(int, input().split()):
        ans *= a
        if ans > 10**18
        print(-1)
        sys.exit()
    print(ans)

### FIXED CODE ###

import sys
input()
A = list(map(int, input().split()))
ans = 1

if 0 in A:
    print(0)
    sys.exit()

else:
    for a in A:
        ans *= a
        if ans > 10**18:
            print(-1)
            sys.exit()
    print(ans)
import sys
def main():
    input = sys.stdin.readline
    N,M=map(int, input().split())
    *A,=map(int, input().split())
    BC=[tuple(map(int, input().split())) for _ in range(M)]

    A.sort()
    BC.sort(key=lambda x:x[1], reverse=True)
    ans=0
    i=0
    from bisect import bisect_right
    for b,c in BC:
        j = bisect_right(A, c, i, i+b)
        ans += c*(j-i)
        i=j
    for j in range(i,N):
        ans+=A[j]
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
def main():
    input = sys.stdin.readline
    N,M=map(int, input().split())
    *A,=map(int, input().split())
    BC=[tuple(map(int, input().split())) for _ in range(M)]

    A.sort()
    BC.sort(key=lambda x:x[1], reverse=True)
    ans=0
    i=0
    from bisect import bisect_right
    for b,c in BC:
        j = bisect_right(A, c, i, min(N,i+b))
        ans += c*(j-i)
        i=j
    for j in range(i,N):
        ans+=A[j]
    print(ans)

if __name__ == '__main__':
    main()
N, K = map(int, input().split())
P = list(map(int, input().split()))
a = []
i = 0
While K+i <= N:
  k = P[i:K+i]
  a.append(k)
  i += 1
a_max = max(a)
print((a_max+K)/2)

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))

    tmp_max = sum(P[:K])
    tmp = sum(P[:K])
    for i in range(N-K):
        tmp += P[i+K]
        tmp -= P[i]

        if tmp > tmp_max:
            tmp_max = tmp

    print((tmp_max + K) / 2)

main()
a = list(map(int, input().split()))
a.sort()
print(0+abs(a[2]-a[1])+abs(a[3]-a[2]))

### FIXED CODE ###

a = list(map(int, input().split()))
a.sort()
print(0+abs(a[1]-a[0])+abs(a[2]-a[1]))
def gcd(a, b):
     while b:
         a, b = b, a%b
     return a

n, m = map(int, input().split())
s, t = input(), input()

l = n*m // gcd(n, m)

x = [0] * l
a, b = [], []

for i in range(n):
    x[i*(l//n)] = s[i]
    a.append(i*(l//n))

for j in range(m):
    x[j*(l//m)] = t[j]
    b.append(j*(l//m))

if "".join([x[i] for i in a]) == s and ("".join([x[j] for j in b])) == t:
    print(l)
else:
    print(-1)

### FIXED CODE ###

def gcd(a, b):
     while b:
         a, b = b, a%b
     return a

n, m = map(int, input().split())
s, t = input(), input()

l = n*m//gcd(n, m)

a = [i*(l//n) for i in range(n)]
b = [i*(l//m) for i in range(m)]

x = set(a) & set(b)

for i in x:
    if s[a.index(i)] != t[b.index(i)]:
        print(-1)
        exit()

print(l)
# https://atcoder.jp/contests/abc073/tasks/abc073_d

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))



# default import
from itertools import product, permutations, combinations

from scipy.sparse import csr_matrix, lil_matrix
from scipy.sparse.csgraph import dijkstra

# ダイクストラか？
# 全点間の最小距離を取得
# rについてpermutationして。その通りに訪れたときの距離をシミュレーション
# 最小値を選ぶ

N, M, r = read_ints()
R = read_ints()
adj_mat = [[0] * M for _ in range(M)]
for _ in range(M):
    a, b, c = read_ints()
    a -= 1
    b -= 1
    adj_mat[a][b] = c
    adj_mat[b][a] = c

D = dijkstra(csr_matrix(adj_mat), directed=False)
# print(D)
# 全探索パート
ans = 2**63
for p in permutations(R):
    tmp = 0
    for i in range(r - 1):
        tmp += D[p[i] - 1, p[i + 1] - 1]
    ans = min(ans, tmp)
print(int(ans))


### FIXED CODE ###

# https://atcoder.jp/contests/abc073/tasks/abc073_d

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))



# default import
from itertools import product, permutations, combinations

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

# ダイクストラか？
# 全点間の最小距離を取得
# rについてpermutationして。その通りに訪れたときの距離をシミュレーション
# 最小値を選ぶ

N, M, r = read_ints()
R = [x - 1 for x in read_ints()]
adj_mat = [[0] * N for _ in range(N)]
for _ in range(M):
    a, b, c = read_ints()
    a -= 1
    b -= 1
    adj_mat[a][b] = c
    adj_mat[b][a] = c

# print(csr_matrix(adj_mat, dtype='int'))
D = dijkstra(csr_matrix(adj_mat, dtype='int'), directed=False)
# print(D)
# 全探索パート
ans = 2 ** 31


def get_kyori(p):
    ret = 0
    for ps, pt in zip(p[:-1], p[1:]):
        ret += D[ps, pt]
    return ret


for p in permutations(R):
    ans = min(ans, get_kyori(p))
print(int(ans))
from collections import defaultdict



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

def dp(start, to, used):
    if start == to:
        return True
    if start in x_y:
        is_ok = False
        for next_start in x_y[start]:
            if not next_start in used:
                #remove used
                used.append(next_start)
                if dp(next_start, to, used):
                    return True
        return is_ok

    else:
        return False




x_y = defaultdict(lambda:set())
for _ in range(m):
    x, y = map(int, input().split())
    x_y[x].add(y)
    x_y[y].add(x)


count = 0
for index in range(len(p)):
    start = index + 1
    to = p[index]
    used = []
    if dp(start, to, used):
        # 交換可能
        count += 1
print(count)


### FIXED CODE ###


class UnionFind():
    def __init__(self, n):
        self.par = []
        for i in range(n+1):
            self.par.append(i)

    def find(self, x):
        if(self.par[x] == x):
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same(self,x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if(x == y) :
            return
        self.par[x] = y




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

uf = UnionFind(n);

for _ in range(m):
    x, y = map(int, input().split())
    uf.union(x, y)


count = 0
for index in range(n):
    start = index + 1
    to = p[index]
    if uf.same(start, to):
        count += 1
print(count)
import math
A, B = map(int, input().split())
print(int(A*B/math.gcd(A, B)))


### FIXED CODE ###

A, B = map(int, input().split())
min = min(A, B)
max = max(A, B)

cnt = 1
while True:
    if max * cnt % min == 0:
        print(max * cnt)
        exit()
    cnt += 1
a, b = (int(i) for i in input().split()) 
print(c*d)

### FIXED CODE ###

a, b = (int(i) for i in input().split()) 
print((a-1)*(b-1))
h,w=map(int,input().split())
l=[["."*(w+2)]]
for i in range(h):
  l.append(["."]+[i for i in input()]+["."])
l.append(["."]*(w+2))

def count(i,j):
  if l[i][j]=="#":
    return "#"
  c=0
  for n in range(i-1,i+2):
    for m in range(j-1,j+2):
      if l[n][m]=="#":
        c+=1
  return str(c)

ans=["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print("\n".join(ans))

### FIXED CODE ###

h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))
 
def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)
 
ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]
 
print("\n".join(ans))
a = [list(map(int, input().split())) for _ in range(3)]
n = int(input())
b = [int(input() for _ in range(n))]

flag = False
for i in range(3):
    if a[i][0] in b and a[i][1] in b and a[i][2] in b:
        flag = True
        break
    if a[0][i] in b and a[1][i] in b and a[2][i] in b:
        flag = True
        break
if a[0][0] in b and a[1][1] in b and a[2][2] in b:
    flag = True
if a[2][0] in b and a[1][1] in b and a[0][2] in b:
    flag = True
    
print("Yes" if flag else "No")

### FIXED CODE ###

import numpy as np

a = [list(map(int, input().split())) for _ in range(3)]
a = np.array(a)
n = int(input())

for _ in range(n):
    b = int(input())
    
    for j in range(3):
        for i in range(3):
            if a[j][i] == b:
                a[j][i] = 0

at = a.transpose()
flag = False
for i in range(3):
    if sum(a[:][i]) == 0 or sum(at[:][i]) == 0:
        flag = True
        break
diag = a[0][0] + a[1][1] + a[2][2]
diag2 = a[2][0] + a[1][1] + a[0][2]
if diag == 0 or diag2 == 0:
    flag = True

print("Yes" if flag else "No")
import numpy as np
import numpy.random as random
import scipy as sp
import pandas as pd
from pandas import Series, DataFrame
import math
 
wakeup = 60 * H1 + M1
gotobed = 60 * H2 + M2
 
wake = gotobed - wakeup
 
answer = wake - K
 
answer

### FIXED CODE ###

H1, M1, H2, M2, K = map(int, input().split())

wakeup = 60 * H1 + M1
gotobed = 60 * H2 + M2
 
wake = gotobed - wakeup
 
answer = wake - K
 
print(answer)
[N,L] = [int(x) for x in input().split()]
A = {}
B = [0]*N
ANS = {}
count = 0
for i,x in enumerate(input().split()):
    i = int(i)
    x = int(x)
    B[i] = x
    if x in A : A[x] += [i]
    else : A[x] = [i]
for i in range(10**9) :
    for j in A[i] :
        for k in range(L) :
            if j + k not in ANS :
                ANS[j+k] = i
                count += 1
    if count > N * 0.99 - 1 : break

for i in range(L):
    if i not in ANS : ANS[i] = min(B[:i+1])
for i in range(L,N):
    if i not in ANS : ANS[i] = min(B[i+1-L:i+1])
for i in range(L-1,N-1) : print(ANS[i], end=" ")
print(ANS[N-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
a, b, c = map(int, input().split())

if a + b == c:
    print('Yes')
    elif b + c == a:
        print('Yes')
    elif a + c == b:
        print('Yes')
    else: 
        print('No')

### FIXED CODE ###

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

if a + b == c:
    print('Yes')
elif b + c == a:
    print('Yes')
elif a + c == b:
    print('Yes')
else: 
    print('No')
#データ入力
rowNum, columnNum = map(int, input().split())
inMat = [[0] * columnNum for i in range(columnNum)]
inColumnVec = [[0] for i in range(columnNum)]
for row in range(rowNum):
    inMat[row] = [int(i) for i in input().split()]
for column in range(columnNum):
    inColumnVec[column] = int(input())

timesAns = [[0] for i in range(rowNum)]

#行列計算
for row in range(rowNum):
    timesAns[row] = 0
    for column in range(columnNum):
        timesAns[row] = timesAns[row] + inMat[row][column] * inColumnVec[column]

for row in range(rowNum):
    print(timesAns[row])


### FIXED CODE ###

#データ入力
rowNum, columnNum = map(int, input().split())
inMat = [[0] * columnNum for i in range(rowNum)]
inColumnVec = [[0] for i in range(columnNum)]
for row in range(rowNum):
    inMat[row] = [int(i) for i in input().split()]
for column in range(columnNum):
    inColumnVec[column] = int(input())

timesAns = [[0] for i in range(rowNum)]

#行列計算
for row in range(rowNum):
    timesAns[row] = 0
    for column in range(columnNum):
        timesAns[row] = timesAns[row] + inMat[row][column] * inColumnVec[column]

for row in range(rowNum):
    print(timesAns[row])
#Deven-Eleven:
D = (((-1, -1), (0, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)),
     ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 0)))
def func(y, x):
    q = [(y, x, 0)]
    cover = [[-1] * m for _ in xrange(n)]
    while len(q) != 0:
        y, x, step = q.pop(0)
        if cover[y][x] >= 0:
            continue
        cover[y][x] = step
        for dx, dy in D[y % 2]:           
            ny, nx = y + dy, x + dx
            if 0 <= ny < n and 0 <= nx < m:
                q.append((ny, nx, step + 1))
    return cover
 
def solve():
    scover = [func(pos[1] - 1, pos[0] - 1) for pos in spos]
    min_cover = [[min(sc[y][x] for sc in scover) for x in xrange(m)] for y in xrange(n)]
    def count(cover):
        ret = 0
        for y in xrange(n):
            for x in xrange(m):
                if cover[y][x] < min_cover[y][x]:
                    ret += 1
        return ret
    return max(count(func(pos[1] - 1, pos[0] - 1)) for pos in tpos)
 
while True:
    data = raw_input()
    if data == "0":
        break
    m, n = map(int, data.split())
    s = input()
    spos = [map(int, raw_input().split()) for _ in xrange(s)]
    t = input()
    tpos = [map(int, raw_input().split()) for _ in xrange(t)]
    print solve()

### FIXED CODE ###

#Deven-Eleven:
D = (((-1, -1), (0, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)),
     ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 0)))
def func(y, x):
    q = [(y, x, 0)]
    cover = [[-1] * m for _ in xrange(n)]
    while len(q) != 0:
        y, x, step = q.pop(0)
        if cover[y][x] >= 0:
            continue
        cover[y][x] = step
        for dx, dy in D[y % 2]:           
            ny, nx = y + dy, x + dx
            if 0 <= ny < n and 0 <= nx < m:
                q.append((ny, nx, step + 1))
    return cover
 
def solve():
    scover = [func(pos[1] - 1, pos[0] - 1) for pos in spos]
    min_cover = [[min(sc[y][x] for sc in scover) for x in xrange(m)] for y in xrange(n)]
    def count(cover):
        ret = 0
        for y in xrange(n):
            for x in xrange(m):
                if cover[y][x] < min_cover[y][x]:
                    ret += 1
        return ret
    return max(count(func(pos[1] - 1, pos[0] - 1)) for pos in tpos)

while True:
    data = raw_input()
    m, n = map(int, data.split())
    if m == 0:
        break
    s = input()
    spos = [map(int, raw_input().split()) for _ in xrange(s)]
    t = input()
    tpos = [map(int, raw_input().split()) for _ in xrange(t)]
    print solve()
from collections import defaultdict,deque

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

loop = N
pos = 0
oto = defaultdict(int)
ind = 0
tej = deque([])
for i in range(N):
    pos = A[pos] - 1
    tej.append(pos+1)
    if oto[pos] == 1:
        ind = A.index(pos+1)
        loop = i - ind + 1
        break
    oto[pos] = 1

##print("loop",loop,"ind",ind)
##print(tej)
if K <= ind:
    print(tej[K])
else:
    print(tej[(K-ind)%loop+max(0,ind-1)])

### FIXED CODE ###

from collections import defaultdict,deque

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

loop = N
pos = 0
oto = defaultdict(int)
ind = 0
tej = deque([])
for i in range(N):
    pos = A[pos] - 1
    tej.append(pos+1)
    if oto[pos] == 1:
        ind = tej.index(pos+1)
        loop = i - ind 
        break
    oto[pos] = 1
K -= 1
##print("loop",loop,"ind",ind)
##print(tej)
if K <= ind:
    print(tej[K])
else:
    print(tej[(K-ind)%loop+ind])