Harryxun/stf_run huggingface dataset:
#%%
W, H = [int(i) for i in input().split()]
maze = [[] for _ in range(H)]

num_black = 0
for i in range(H):
    temp = input()
    for j in temp:
        maze[i].append(j)
        if j == "#":
            num_black += 1

#%%
import copy, collections

maze_copy = copy.deepcopy(maze)
color = [["W"] * W for _ in range(H)]


def BFS(Y, X):
    vec = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    color[Y][X] = "G"
    que = collections.deque()
    maze_copy[Y][X] = 1
    que.append([Y, X])

    while len(que) != 0:
        u = que.popleft()
        if u[0] == H - 1 and u[1] == W - 1:
            return maze_copy[u[0]][u[1]]
        for y, x in vec:
            temp_x = u[1] + x
            temp_y = u[0] + y
            if (0 <= temp_x < W and 0 <= temp_y < H
                    and maze_copy[temp_y][temp_x] == "." and color[temp_y][temp_x] == "W"):
                color[temp_y][temp_x] = "G"
                maze_copy[temp_y][temp_x] = maze_copy[u[0]][u[1]] + 1
                que.append([temp_y, temp_x])
    
    return  -1

ans = BFS(0, 0)
if ans == -1:
    print(-1)
else:
    print(W * H - num_black - ans)



### FIXED CODE ###

#%%
H, W = [int(i) for i in input().split()]
maze = [[] for _ in range(H)]

num_black = 0
for i in range(H):
    temp = input()
    for j in temp:
        maze[i].append(j)
        if j == "#":
            num_black += 1
            
#%%
import copy, collections

maze_copy = copy.deepcopy(maze)
color = [["W"] * W for _ in range(H)]


X, Y = 0, 0
vec = [[1, 0], [-1, 0], [0, 1], [0, -1]]
color[Y][X] = "G"
que = collections.deque()
maze_copy[Y][X] = 1
que.append([Y, X])

while len(que) != 0:
    u = que.popleft()
    for y, x in vec:
        temp_x = u[1] + x
        temp_y = u[0] + y
        if (0 <= temp_x < W and 0 <= temp_y < H
                and maze_copy[temp_y][temp_x] == "." and color[temp_y][temp_x] == "W"):
            color[temp_y][temp_x] = "G"
            maze_copy[temp_y][temp_x] = maze_copy[u[0]][u[1]] + 1
            que.append([temp_y, temp_x])
    
if maze_copy[-1][-1] == ".":
    print(-1)
else:
    print(W * H - num_black - maze_copy[-1][-1])
N, X = map(int, input().split())
pat = [1 for _ in range(N+1)]
mai = [1 for _ in range(N+1)]
for i in range(1, N+1):
  mai[i] = 2 * mai[i-1] + 3
  pat[i] = 2 * pat[i-1] + 1

def cnt_pat(x, n):
  if n == 0:
    return 1
  if x == 1:
    return 0
  if x <= mai[n] // 2:
    return cnt_pat(x-1, n-1)
  if x == mai[n] // 2 + 1:
    return pat[n-1] + 1
  if x < mai[n]:
    return pat[n-1] + 1 + cnt_pat(x-mai[n]//2-1, n-1)
  if x == n:
    return pat[n]
  
print(cnt_pat(X, N))

### FIXED CODE ###

N, X = map(int, input().split())
pat = [1 for _ in range(N+1)]
mai = [1 for _ in range(N+1)]
for i in range(1, N+1):
  mai[i] = 2 * mai[i-1] + 3
  pat[i] = 2 * pat[i-1] + 1

def cnt_pat(x, n):
  if n == 0:
    return 1
  if x == 1:
    return 0
  if x <= mai[n] // 2:
    return cnt_pat(x-1, n-1)
  if x == mai[n] // 2 + 1:
    return pat[n-1] + 1
  if x < mai[n]:
    return pat[n-1] + 1 + cnt_pat(x-mai[n]//2-1, n-1)
  if x == mai[n]:
    return pat[n]
  
print(cnt_pat(X, N))
ABCD=input()
L=list(ABCD)

def bfs(i,j,a,c):#i,hidari,migi
  if i==3:
    if j+int(L[3])==7:
      c+=[L[0]+a[0]+L[1]+a[1]+L[2]+"+"+L[3]+"=7"]
    elif j-int(L[3])==7:
      c+=[L[0]+a[0]+L[1]+a[1]+L[2]+"-"+L[3]+"=7"]
    return 
    
  else:
    bfs(i+1,j+int(L[i]),a+"+",c)
    bfs(i+1,j-int(L[i]),a+"-",c)
    return c[0]
  


print(bfs(1,int(L[0]),"",[]))

### FIXED CODE ###

ABCD=input()
L=list(ABCD)

def bfs(i,j,a,c):#i,hidari,migi
  if i==3:
    if j+int(L[3])==7:
      c+=[L[0]+a[0]+L[1]+a[1]+L[2]+"+"+L[3]+"=7"]
    elif j-int(L[3])==7:
      c+=[L[0]+a[0]+L[1]+a[1]+L[2]+"-"+L[3]+"=7"]
    return 
    
  else:
    bfs(i+1,j+int(L[i]),a+"+",c)
    bfs(i+1,j-int(L[i]),a+"-",c)
    return c
  

d=[]

ans=bfs(1,int(L[0]),"",d)
print(d[0])
h,n = map(int,input().split())
a = list(map(int,input().split()))
x = max(a)
a.remove(x)
y = max(a)
print(h,x,y)
if x+y >= h:
         print("Yes")
else:
         print("No")

### FIXED CODE ###

h,n = map(int,input().split())
a = list(map(int,input().split()))
if sum(a)>= h:
         print("Yes")
else:
         print("No")
print a*b, a+a+b+b

### FIXED CODE ###

a, b = map(int, raw_input().split())
print a*b, 2*a + 2*b
import bisect

N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

cnt = 0
A.sort()
    
if V <= P:
    for i in range(N-P):
        if A[i]+M < A[N-P]:
            cnt += 1
        else:
            break
else:
    for i in range(N-P):
        if i+P >= V:
            if A[i]+M < A[N-P]:
                cnt += 1
            else:
                break
        else:
            num = M*V-M*(i+P)#さばかないといけない数
            j = bisect.bisect_left(A, A[i]+M)
            if A[j] == A[i]+M:
                j -= 1
            j = min(j, N-P)
            if j > V-P:
                cnt += 1
                break
            L = A[i+1:j+1]
            t = 0
            while L[V-P-i-1] < A[i]+M and num > 0:
                s = A[i]+M-L[V-P-i-1]
                for k in range(V-P-i):
                    L[k] += s
                L.sort()
                num -= s*(V-P-i)
            if num > 0:
                cnt += 1
            else:
                break
print(N-cnt)

### FIXED CODE ###

N,M,V,P = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort(reverse=True)

S = [0]*N
t = 0
for i in range(N):
    t += A[i]
    S[i] = t

s = M*(N-V)
l = P
r = N
if P == 1:
    a = 0
else:
    a = S[P-2]
while l < r:
    m = (r-l)//2 + l
    if A[P-1]-M > A[m]:
        r = m
        continue
    if S[m-1]-a-(m-P+1)*A[m] > s:
        r = m
        continue
    l = m+1
    
print(l)
s1=input()
s2=input()
n=len(s1)
m=len(s2)
import numpy as np
dp=[[0 for i in range(m+1)]for i in range(n+1)]
for i in range(1,n+1):
    for j in range(1,m+1):
        if s1[i-1]==s2[j-1]:
            dp[i][j]=dp[i-1][j-1]+1
        else:
            dp[i][j]=max(dp[i-1][j],dp[i][j-1])
# print(np.array(dp))
ans=""
print(dp[-1][-1])
i=n
j=m
while i>0 and j>0:
    if dp[i][j]==dp[i-1][j]:
        i-=1
    elif dp[i][j]==dp[i][j-1]:
        j-=1
    else:
        ans=s1[i-1]+ans
        i-=1
        j-=1
print(ans)

### FIXED CODE ###

s1=input()
s2=input()
n=len(s1)
m=len(s2)
# import numpy as np
dp=[[0 for i in range(m+1)]for i in range(n+1)]
for i in range(1,n+1):
    for j in range(1,m+1):
        if s1[i-1]==s2[j-1]:
            dp[i][j]=dp[i-1][j-1]+1
        else:
            dp[i][j]=max(dp[i-1][j],dp[i][j-1])
# print(np.array(dp))
ans=""
# print(dp[-1][-1])
i=n
j=m
while i>0 and j>0:
    if dp[i][j]==dp[i-1][j]:
        i-=1
    elif dp[i][j]==dp[i][j-1]:
        j-=1
    else:
        ans=s1[i-1]+ans
        i-=1
        j-=1
print(ans)
N, Y = map(int, input().split())
for x in range(0,N)
	for y in range(0,N)		
		for z in range(0,N)			
   			if Y==10000*x+5000*y+1000*z:
    			print(x,y,z)
    			break
print(-1,-1,-1)

### FIXED CODE ###

N, Y = map(int, input().split())
 
for x in range(N+1):
  for y in range(N-x+1):
    z = N - x - y
    if 10000 * x + 5000 * y + 1000 * z == Y:
      print(x,y,z)
      exit()
print(-1,-1,-1)
n=int(input())
a=list(map(int,input().split()))
ans=1
a.sort()
la=list(set(a))
if n%2==0:
    for i in range(1,n,2):
        if a.count(i)==2:
            ans*=2
            ans%=1e9+7
        else:
            ans=0
            break
else:
    if a[0]==0 and a[1]!=0:
        a=a[1:]
        for i in range(2,n,2):
            if a.count(i)==2:
                ans*=2
                ans%=1e9+7
            else:
                ans=0
                break
    else:
        ans=0
print(int(ans))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
A.sort()
if N % 2 == 1:
    if A[0] != 0:
        print(0)
        exit()
    for i in range(N // 2):
        if A[2 * i + 1] != 2 * (i + 1) or A[2 * i + 2] != 2 * (i + 1):
            print(0)
            exit()
    print(pow(2, N // 2) % (10 ** 9 + 7))
else:
    for i in range(N // 2):
        if A[2 * i] != 2 * i + 1 or A[2 * i + 1] != 2 * i + 1:
            print(0)
            exit()
    print((pow(2, N // 2)) % (10 ** 9 + 7))
K, X = map(int, input().split())

low = X - K
hight = X + K

a = set()
for x in range(low + 1, hight, 1):
    a.add(x)
print(' '.join([str(x) for x in sorted(a)]b))


### FIXED CODE ###

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

low = X - K
hight = X + K

a = set()
for x in range(low + 1, hight, 1):
    a.add(x)
a = sorted(a)
b = [str(x) for x in a]
print(' '.join(b))
n=int(input())
a=list(map(int,input().split()))
cnt1=0
cnt4=0
for i in a:
  if i%2==1:
    cnt1+=1
  elif cnt%4==0:
    cnt4+=1
if cnt4>=cnt1+1:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
cnt1=0
cnt2=0
cnt4=0
for i in a:
  if i%2==1:
    cnt1+=1
  elif i%4!=0:
    cnt2+=1
  elif i%4==0:
    cnt4+=1
if cnt2==0:
  if cnt1<=cnt4+1:
    print('Yes')
  else:
    print('No')
else:
  if cnt1<=cnt4:
    print('Yes')
  else:
    print('No')
if S[0] == S[1] == S[2]:
  print('No')
else
  print('Yes')

### FIXED CODE ###

S = input()
if S[0] == S[1] and S[1] == S[2]:
  print('No\n')
else:
  print('Yes\n')
a=input()
b=input()
if a>=3200:
	print(b)
else:
  print("red")

### FIXED CODE ###

a=input()
b=input()
if int(a)>=3200:
	print(b)
else:
  print("red")
N = input()
K = input()
l = list(map(int(input().split()))

for i in l:
     ans = 0    
	ans += min(i,K-i)
    
print(ans*2)
         

### FIXED CODE ###

N = input()
K = int(input())
l = input().split()
         

         
ans =0 
for i in l:
	s = int(i)
	ans += min(s,K-s)
print(ans*2)
def valid(i,j):
    if i>=0 and i<h and j>=0 and j<w:
        return 1
    return 0
def paths(i,j):

    if a[i][0][j]=='#' or valid(i,j)==0:
        return 0
    if i==h-1 and j==w-1:
        return 1
    if i<h-1 and j<w-1:
        if dp[i][j]==-1:
          dp[i][j]=paths(i+1,j)+paths(i,j+1)
          return dp[i][j]
        else:
            return dp[i][j]
    if i==h-1 and j<w-1:
        if dp[i][j]==-1:
            dp[i][j]=paths(i,j+1)
            return dp[i][j]
        else:
            return dp[i][j]
    if i<h-1 and j==w-1:
        if dp[i][j]==-1:
            dp[i][j]=paths(i+1,j)
            return dp[i][j]
        else:
            return dp[i][j]


h,w=map(int,input().split())
a=[[]for _ in range(h)]
m=1000000007
for i in range(h):
   a[i].extend(input().split())
dp=[[-1 for _ in range(w+1)]for _ in range(h+1)]
print(paths(0,0)%m)


### FIXED CODE ###

import sys
sys.setrecursionlimit(20000)

def paths(i, j):

    if i == h - 1 and j == w - 1:
        return 1
    if a[i][0][j] == '#':
        return 0
    if i < h - 1 and j < w - 1:
        if dp[i][j] == -1:
            dp[i][j] = paths(i + 1, j) + paths(i, j + 1)
            return dp[i][j]
        else:
            return dp[i][j]
    elif i == h - 1 and j < w - 1:
        if dp[i][j] == -1:
            dp[i][j] = paths(i, j + 1)
            return dp[i][j]
        else:
            return dp[i][j]
    elif i < h - 1 and j == w - 1:
        if dp[i][j] == -1:
            dp[i][j] = paths(i + 1, j)
            return dp[i][j]
        else:
            return dp[i][j]


h, w = map(int, input().split())
a = [[] for _ in range(h)]
m = 1000000007
for i in range(h):
    a[i].append(input())
dp = [[-1 for _ in range(1000)] for _ in range(1000)]
print(paths(0, 0) % m)
import fractions
import math
n,m = map(int,input().split())
a = list(map(int, input().split()))
a = set(a)
c = set()
l = 1
k = (a[0] // 2) % 2
for i in a:
    l = l * i // fractions.gcd(l,i)
    if l // 2 > m:
        ans = 0
        break
    count = 0
    while True:
        if (i>>count) % 2 == 0:
            count += 1
        else:
            c.add(count)
            break
    if len(c) > 1:
        ans = 0
        break
else:
    ans = (m + l // 2) // l

print(ans)

### FIXED CODE ###

import fractions
import math
n,m = map(int,input().split())
a = list(map(int, input().split()))
s = set(a)
c = set()
l = 1
k = (a[0] // 2) % 2
for i in a:
    l = l * i // fractions.gcd(l,i)
    if l // 2 > m:
        ans = 0
        break
    count = 0
    while True:
        if (i>>count) % 2 == 0:
            count += 1
        else:
            c.add(count)
            break
    if len(c) > 1:
        ans = 0
        break
else:
    ans = (m + l // 2) // l

print(ans)
S = input()
print("Yes" if s[0]==s[1]==s[2] or s[1]==s[2]==s[3] else "No")

### FIXED CODE ###

S = input()
print("Yes" if S[0]==S[1]==S[2] or S[1]==S[2]==S[3] else "No")
inputA= int(input())
inputB =map(input())

print(inputB.count('ABC'))

### FIXED CODE ###

inputA= int(input())
inputB =input()

print(inputB.count('ABC'))
# coding:utf-8
# C - Grand Garden

N = int(input())
h = list(map(int,input().split()))
h += [0]
ans = 0

for _ in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                ans += 1
            add = False

print(ans)

### FIXED CODE ###

N = int(input())
h = list(map(int,input().split()))
h += [0]
add = False
ans = 0
for n in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                ans += 1
            add = False
print(ans)
from collections import deque

h,w = map(int,input().split())
maze = []
for i in range(h):
    s = input()
    maze.append(s)

def cand_pos(i,j):
    neighbor = []
    if i==0:
        neighbor.append(maze[i+1][j])
    elif i==h-1:
        neighbor.append(maze[i-1][j])
    else:
        neighbor.append(maze[i+1][j])
        neighbor.append(maze[i-1][j])
    
    if j==0:
        neighbor.append(maze[i][j+1])
    elif j==w-1:
        neighbor.append(maze[i][j-1])
    else:
        neighbor.append(maze[i][j+1])
        neighbor.append(maze[i][j-1])
    
    if maze[i][j]!='#' and neighbor.count('.')==1:
        return True
    else:
        return False


# (sx, sy) から (gx, gy) への最短距離を求める
# 辿り着けないと INF
def bfs(sx,sy,gx,gy):
    # すべての点を INF で初期化
    d = [[float("inf")] * w for i in range(h)]

    # 移動4方向のベクトル
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    # スタート地点をキューに入れ、その点の距離を0にする
    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    # キューが空になるまでループ
    while que:
        # キューの先頭を取り出す
        p = que.popleft()
        # 取り出してきた状態がゴールなら探索をやめる
        if p[0] == gx and p[1] == gy:
            break
        # 移動4方向をループ
        for i in range(4):
            # 移動した後の点を (nx, ny) とする
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            # 移動が可能かの判定とすでに訪れたことがあるかの判定
            # d[nx][ny] != INF なら訪れたことがある
            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):
                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]

pos_dic = {}
for i in range(h):
    for j in range(w):
        if cand_pos(i,j):
            pos_dic[i] = j
        else:
            continue

ans = 1000000

if len(pos_dic) == 0:
    ans = h+w-2
elif len(pos_dic) == 1:
    stx,sty = 0,0
    for k,v in pos_dic.items():
        glx, gly = k,v
    ans = bfs(stx, sty, glx, gly)
else:
    for k,v in pos_dic.items():
        stx, sty = k,v
        for kk, vv in pos_dic.items():
            glx, gly = kk, vv
            tmp = bfs(stx, sty, glx, gly)
            ans = min(ans, tmp)

print(ans)

### FIXED CODE ###

from collections import deque

h,w = map(int,input().split())
maze = []

for _ in range(h):
    maze.append(list(input()))

def bfs(initial):
    distance = [[-1] * w for _ in range(h)]
    frontier = deque()
    frontier.append(initial)
    distance[initial[0]][initial[1]] = 0
    while len(frontier):
        current_node = frontier.popleft()
        x,y = current_node
        for i,j in ((1,0),(-1,0),(0,1),(0,-1)):
            new_x, new_y = x+i, y+j
            if 0<=new_x<h and 0<=new_y<w:
                if maze[new_x][new_y] == '.' and distance[new_x][new_y]==-1:
                    distance[new_x][new_y] = distance[x][y] + 1
                    frontier.append((new_x, new_y))
    return distance[x][y]

ans = 0
for i in range(h):
    for j in range(w):
        if (maze[i][j] == "#"):
            continue
        solution = bfs((i,j))
        ans = max(ans,solution)

print(ans)
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[10**9]*N
dp[0]=0
for i in range(K):
  H.append(10**9)

for i in range(N-1):
  A=[]
  for j in range(K):
    A.append(dp[i-j]+abs(H[i-j]-H[i+1]))
  dp[i+1]=min(A)

print(str(dp[N-1]))

### FIXED CODE ###

N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[0]*N

for i in range(N-1):
  A=[]
  for j in range(K):
    if i-j>=0:
      A.append(dp[i-j]+abs(H[i-j]-H[i+1]))
  dp[i+1]=min(A)

print(dp[N-1])
n = int(input())
count_ans = 0
for i in range(n//2 + 1):
  count_y = 0
  for j in range(i):
    if i%j == 0:
      count_y += 1
  if count_y = 8:
    count_ans += 1
print(count_ans)

### FIXED CODE ###

n = int(input())
count_ans = 0
for i in range(n//2 + 1 if n%2 ==1 else n//2):
  count_y = 0
  for j in range(2*i+2):
    if j > 0:
      if (2 * i + 1)%j == 0:
        count_y += 1
  if count_y == 8:
    count_ans += 1
print(count_ans)
s = input()
if s == 'Sunny':
    print('Cloudy')
elif:
    s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')

### FIXED CODE ###

s = input()
if s == 'Sunny':
    print('Cloudy')
elif s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')
x = int(input())

t = (-1 + math.sqrt(1 + 8 * x))
if t % 2 == 0:
  print(t//2)
else:
  print(t//2+1)


### FIXED CODE ###

import math
x = int(input())

t = (-1 + math.sqrt(1 + 8 * x))
if t % 2 == 0:
  print(int(t)//2)
else:
  print(int(t)//2+1)
f=lambda s:zip(*[t for t in s if'#'in t]);for r in f(f(open(0).readlines())):print(*r,sep='')

### FIXED CODE ###

h, w = map(int, input().split())
a = []
for _ in range(h):
  t = list(input())
  if t.count('.') != w:
    a.append(t)
i = 0
while True:
  if i >= len(a[0]):break
  for t in a:
    if t[i] != '.':
      i += 1
      break
  else:
    for j in range(len(a)):
      del a[j][i]
for t in a:
  print(''.join(t))
import sys
input = sys.stdin.readline
def main():
    N = input()
    N_l = list(N)
    m = int(N_l[-1])
    if m == 0 or m == 1 or m==6 or m==8:
        print("pon")
    elif m == 2 or m== 4 or m == 5 or m == 7 or m==9:
        print("hon")
    else:
        print("bon")
if __name__ == "__main__":
    main()


### FIXED CODE ###


def main():
    N = input()
    N_l = list(N)
    m = int(N_l[-1])
    if m == 0 or m == 1 or m==6 or m==8:
        print("pon")
    elif m == 2 or m== 4 or m == 5 or m == 7 or m==9:
        print("hon")
    elif m == 3:
        print("bon")
if __name__ == "__main__":
    main()
import numpy as np

n, m = map(int, input().split())
a = np.zeros(m)
for i in range(m):
    a[i] = int(input())

dp = np.zeros(n + 1)
dp[0] = 1
if a[0] == 1:
    dp[1] = 0
else:
    dp[1] = 1

for i in range(2, n + 1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007

print(int(dp[n]))

"""
100 5
1
23
45
67
89
"""


### FIXED CODE ###

n, m = map(int, input().split())
A = set()
for i in range(m):
    a = int(input())
    A.add(a)
dp = [0] * (n + 1)
for i in range(n + 1):
    if i in A:
        dp[i] = 0
    else:
        if i == 0:
            dp[i] = 1
        elif i == 1:
            dp[i] = 1
        else:
            dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007
print(dp[n])

"""
100 5
1
23
45
67
89
"""
#154 c
n=int(input())
a=list(map(int,input().split()))
num=dict()
flag=True
for x in a:
    if x not in num:
        a[x]=1
    else:
        flag=False
    if flag==False:
        break
if flag:
    print("YES")
else:
    print("NO")


### FIXED CODE ###

#154 c
n=int(input())
a=list(map(int,input().split()))
num=set(a)
if len(a)==len(num):
    print("YES")
else:
    print("NO")
n=int(input())
for i in range(1,10):
  for j in range(1,10):
    if n==i*j:
      print("Yes")
	  exit()
print("No")

### FIXED CODE ###

n=int(input())
for i in range(1,10):
    for j in range(1,10):
        if n==i*j:
            print("Yes")
            exit()
print("No")
n=int(input())
if x>=400 and x<=599:
    print(8)
    exit()
if x>=600 and x<=799:
    print(7)
    exit()
if x>=800 and x<=999:
    print(6)
    exit()
if x>=1000 and x<=1199:
    print(5)
    exit()
if x>=1200 and x<=1399:
    print(4)
    exit()
if x>=1400 and x<=1599:
    print(3)
    exit()
if x>=1600 and x<=1799:
    print(2)
    exit()
if x>=1800 and x<=1999:
	print(1)
  	exit()
 

### FIXED CODE ###

x=int(input())
if x>=400 and x<=599:
    print(8)
    exit()
if x>=600 and x<=799:
    print(7)
    exit()
if x>=800 and x<=999:
    print(6)
    exit()
if x>=1000 and x<=1199:
    print(5)
    exit()
if x>=1200 and x<=1399:
    print(4)
    exit()
if x>=1400 and x<=1599:
    print(3)
    exit()
if x>=1600 and x<=1799:
    print(2)
    exit()
if x>=1800 and x<=1999:
    print(1)
    exit()
s = input()
t = input()

print(t + s)

### FIXED CODE ###

s,t = map(str, input().split())

print(t + s)
s = input()
ans = 0
for i in range(n - 2):
  if s[i:i + 3] == "ABC":
    ++ans
print(ans)

### FIXED CODE ###

n = int(input())
s = input()
ans = 0
for i in range(n - 2):
  if s[i : i + 3] == "ABC":
    ans += 1
print(ans)
now=False
N=int(input())
for k in range(N):
    lst=lst.append(input())
M=int(input())
for t in range(M):
    ID=input()
    if ID in lst:
        now=not(now)
        if now:
            print("Opened by",ID)
        else:
            print("Closed by",ID)
    else:
        print("Unknown",ID)

### FIXED CODE ###

now=False
lst=[]
N=int(input())
for k in range(N):lst.append(input())
M=int(input())
for t in range(M):
    ID=input()
    if ID in lst:
        now=not(now)
        if now:
            print("Opened by",ID)
        else:
            print("Closed by",ID)
    else:
        print("Unknown",ID)
n,k = map(int,input().split())
a = list(map(int,input().split()))

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

gcd1 = gcd(a[0], a[1])

for i in range(2,n):
    gcd1 = gcd(gcd1,a[i])

if k > max(a):
    print('IMPOSSIBLE')
else:
    if k % gcd1 == 0:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')


### FIXED CODE ###

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

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

if n == 1:
    gcd1 = a[0]
else:
    gcd1 = gcd(a[0], a[1])

    for i in range(2,n):
        gcd1 = gcd(gcd1,a[i])

if k > max(a):
    print('IMPOSSIBLE')
else:
    if k % gcd1 == 0:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]
    
    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        l2 = l[l.index(num):]
        print(l2[k])

main()


### FIXED CODE ###

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l[k])

main()