Harryxun/stf_run huggingface dataset:
def main(N,A):
    B=[0]
    count = 0
    for i in range(N):
        T = A[i] + B[i]
        for m in range(N+1):
            if T - B[m] == 0:
                # print(i, m)
                # print(B[i],B[i-m])
                # print(T)
                # print("kore",B[i],B[i-m])
                count += 1
        B.append(T)

    #print(count)
    #print(B)
    return print(count)

if __name__ == "__main__":
    N=int(input())
    A=list(map(int,input().split()))

    main(N,A)

### FIXED CODE ###

import collections

def main(N,A):
    B=[0]
    count = 0
    for i in range(N):
        T = A[i] + B[i]

        B.append(T)
    c = collections.Counter(B)

    for i in range(len(c)):
        k, v = c.popitem()
        for z in range(v):
            count += z

    return print(count)

if __name__ == "__main__":
    N=int(input())
    A=list(map(int,input().split()))

    main(N,A)
n = int(input())
ans = 0
while 2**n > n
	n-=1
ans = 2**n
print(ans)


### FIXED CODE ###

n = int(input())
pow = n
ans = 0
while 2**pow > n:
    pow-=1
print(2**pow)
n,y = map(int,input().split())

for i in range(n+1):
    for j in range(n-i+1):
        k = n-i-j:
        if (10000*i+5000*j+1000*k == y):
            print(i,j,k)
            exit()
print(-1,-1,-1)

### FIXED CODE ###

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

for i in range(n+1):
    for j in range(n-i+1):
        k = n-i-j
        if (10000*i+5000*j+1000*k == y):
            print(i,j,k)
            sys.exit()
print(-1,-1,-1)
import math
from functools import reduce

def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

### FIXED CODE ###

import fractions

def lcm(x,y):
    return (x*y)//fractions.gcd(x,y)

a,b=map(int,input().split())
print(lcm(a,b))
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
field=[[[0 for h in range(6401)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6401):
    if field[h-1][w-1][i]==1:print(i);exit()


### FIXED CODE ###

import sys
input=sys.stdin.readline
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
field=[[[0 for h in range(6500)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6500):
    if field[h-1][w-1][i]==1:print(i);exit()
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a.copy()
count = 1
d = 1
while k >= d:
    count += 1
    d *= 2
else:
    d //= 2
anst = 0
for i in range(count):
    level = 0
    for j in range(n):
        tmpb = b[j] // d
        if tmpb >= 1 and tmpb % 2 == 1:
            level += 1
        b[j] %= d
    if k >= anst + d and n > level * 2:
        anst += d
    d //= 2

ans = 0
for i in range(n):
    ans += a[i] ^ anst
print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a.copy()
count = 1
d = 1
while k >= d:
    count += 1
    d *= 2
else:
    count -= 1
    d //= 2
anst = 0
for i in range(count):
    level = 0
    for j in range(n):
        tmpb = b[j] // d
        if tmpb >= 1 and tmpb % 2 == 1:
            level += 1
        b[j] %= d
    if k >= anst + d and n > level * 2:
        anst += d
    d //= 2

ans = 0
for i in range(n):
    ans += a[i] ^ anst
print(ans)
N = int(input())
A = list(map(int, input().split()))
tmp = [False if i == 0 or i == 1 else True for i in range(1000)]
p, s = True, True

for i in range(2, int(max(A) ** 0.5) + 1):
    t = 0
    if not tmp[i]:
        continue

    for j in range(N):
        if A[j] % i == 0:
            t += 1
            A[j] //= i

    for j in range(i * 2, 1000, i):
        tmp[j] = False

    if t >= 2:
        p = False

    if t == N:
        s = False
        break

print('pairwise coprime' if p else 'setwise coprime' if s else 'not coprime')

### FIXED CODE ###

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

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)

def erat():
    end = 10 ** 6 + 1
    t = [False if i == 0 or i == 1 else True for i in range(end)]
    ret = [0 for i in range(end)]

    for i in range(2, end):
        if t[i]:
            for j in range(i * 2, end, i):
                t[j] = False
                if not ret[j]:
                    ret[j] = i
            ret[i] = i
    return ret

tmp = A[0]
for i in A[1:]:
    tmp = gcd(tmp, i)
isS = True if tmp == 1 else False
isP = True

e = erat()

D = dict(iter([(i, False) for i in range(10 ** 6 + 1)]))
for i in A:
    tmp = set()
    while i != 1:
        tmp.add(e[i])
        i //= e[i]
    for i in tmp:
        if D[i]:
            isP = False
            break
        else:
            D[i] = True
    if not isP:
        break

if isP:
    print('pairwise coprime')
elif isS:
    print('setwise coprime')
else:
    print('not coprime')
S = input()

if S = ABC:
    print('ARC')
else:
    print('ABC')

### FIXED CODE ###

S = input()

if S == 'ABC':
    print('ARC')
else:
    print('ABC')
a = raw_input()
n, k = a.split()
n = int(n)
k = int(k)
b = raw_input()
data = [int(one) for one in b.split()]
res = []
for i in range(len(data) - 1):
  for j in range(i+1, len(data)):
    res.append(data[i]*data[j])
res = sorted(res)
print(res[k-1])

### FIXED CODE ###

d = raw_input()
a, b, c =  d.split()
a = int(a)
b = int(b)
c = int(c)
if (a==b):
  if c == a:
    print("No")
  else:
    print("Yes")
else:
  if a==c or b == c:
    print("Yes")
  else:
    print("No")
a,s=map(int,input().split())
ad=0
for i in range(a,b+1):
  s=str(i)
  if s==s[::-1]:ad+=1
print(ad)

### FIXED CODE ###

a,b=map(int,input().split())
ans=0
for i in range(b+1)[a:]:
  if len(str(i))%2==0:
    p=list(str(i))[:len(str(i))//2]
    q=list(reversed(list(str(i))[len(str(i))//2:]))
    if p==q:
      ans+=1
  else:
    s=list(str(i))[:len(str(i))//2+1]
    d=list(reversed(list(str(i))[len(str(i))//2:]))
    if s==d:
      ans += 1
print(ans)
"""
Sの文字列、最初が何かに依存しそう
"""


S = input()
K = int(input())

if S[0] == "1":
    len_one = 0
    for s in S:
        if s == 1 and flag == 0:
            len_one += 1
        else:
            flag = 0
    
    if K <= len_one:
        ans = 1
    else:
        ans = S[1]

else:
    ans = S[0]

print(ans)

### FIXED CODE ###

"""
Sの文字列、最初が何かに依存しそう
"""


S = input()
K = int(input())

if S[0] == "1":
    len_one = 0
    flag = 0
    for s in S:
        if s == "1" and flag == 0:
            len_one += 1
        elif flag == 0:
            save_num = s
            flag = 1
        else:
            flag = 1
    if K <= len_one:
        ans = 1
    else:
        ans = int(save_num)

else:
    ans = int(S[0])

print(ans)
x=input()
print( x**3 )

### FIXED CODE ###

import sys

x = int( sys.stdin.readline() )
print( "{}".format( x**3 ) )
if __name__ == "__main__":
    a, b, x = map(int, input().split())
    if x > b:
        print(0)
    else:
        found = False
        i = a
        counter = 0
        while i < b + 1:
            if not found and i % x != 0:
                i += 1
            else:
                i += x
                counter += 1
    print(counter)
    


### FIXED CODE ###

if __name__ == "__main__":
    a, b, x = map(int, input().split())
    z = (b) // x
    z -= (a - 1) // x
    print(z)
h,w=list(map(int,input().split()))
s=[input() for _ in range(h)]
g=[[] for _ in range(h*w)]

for i in range(w):
    for j in range(h):
        for di in [-1,0,1]:
            for dj in [-1,0,1]:
                if abs(di)+abs(dj)!=1:
                    continue
                if 0<=i+di<w and 0<=j+dj<h:
                    if s[j][i]!=s[j+dj][i+di]:
                        g[j*w+i].append(i+di+(j+dj)*w)

visit=[0]*(h*w)
ans=0
black=[0]*(h*w)
white=[0]*(h*w)

def rec(j,i,rj,ri):
    if s[j][i]=='#':
        black[rj*w+ri]+=1
    else:
        white[rj*w+ri]+=1
    
    for n in g[j*w+i]:
        if visit[n]==1:
            continue

        nj=n//w
        ni=n%w

        visit[n]=1

        rec(nj,ni,rj,ri)

for i in range(w):
    for j in range(h):
        if visit[w*j+i]:
            continue
        visit[w*j+i]=1

        rec(j,i,j,i)
        
        ans+=black[w*j+i]*white[w*j+i]

print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(500*500)

h,w=list(map(int,input().split()))
s=[[1 if c=='#' else 0 for c in input()] for _ in range(h)]
g=[[] for _ in range(h*w)]

for i in range(w):
    for j in range(h):
        for di in [-1,0,1]:
            for dj in [-1,0,1]:
                if abs(di)+abs(dj)!=1:
                    continue
                if 0<=i+di<w and 0<=j+dj<h:
                    if s[j][i]!=s[j+dj][i+di]:
                        g[j*w+i].append(i+di+(j+dj)*w)

visit=[0]*(h*w)
black=[0]*(h*w)
white=[0]*(h*w)

def rec(j,i,rj,ri):
    if s[j][i]:
        black[rj*w+ri]+=1
    else:
        white[rj*w+ri]+=1
    
    for n in g[j*w+i]:
        if visit[n]:
            continue

        visit[n]=1

        rec(n//w,n%w,rj,ri)

ans=0

for i in range(w):
    for j in range(h):
        if visit[w*j+i]:
            continue
        visit[w*j+i]=1

        rec(j,i,j,i)
        
        ans+=black[w*j+i]*white[w*j+i]

print(ans)
inp = input()
inp = input()
 
def mp(nums):
  n = 1
  limit= 10 ** 10
  for num in nums:
    n *=num
  	if num > limit:
  	  print(-1)
  	  return
  print(num)
  return
 
mp([int(x) for x in inp.split(" ")])

### FIXED CODE ###

inp = input()
inp = input()
 
def mp(nums):
  if 0 in nums:
    print(0)
    return
  n = 1
  limit = 10**18
  for num in nums:
    n *=num
    if n > limit:
      print(-1)
      return
  print(n)
 
mp([int(x) for x in inp.split(" ")])
def incre(a, i, j):
    if a[i][j] != '#':
        a[i][j] += 1


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

a = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            a[i][j] = '#'

for i in range(h):
    for j in range(w):
        if (s[i][j] == '#'):
            if i == 0:
                if j == 0:
                    incre(a, i, j+1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)

                elif j == w-1:
                    incre(a, i, j-1)
                    incre(a, i-1, j-1)
                    incre(a, i+1, j)
            elif 0 < i < h - 1:
                if j == 0:
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j+1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif j == w-1:
                    incre(a, i-1, j)
                    incre(a, i-1, j-1)
                    incre(a, i, j-1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
            elif i == h-1:
                if j == 0:
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j+1)
                elif 0 < j < w - 1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                elif j == w-1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i, j-1)

[print(*i, sep="") for i in a]




### FIXED CODE ###

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

dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1,  0,  1, -1, 1,-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        elif s[i][j] == '.':
            num = 0
            for a in range(8):
                ni = i + dx[a]
                nj = j + dy[a]

                if (0 <= ni < h) and (0 <= nj < w) and (s[ni][nj] == '#'):
                    num += 1
            s[i][j] = num

[print(*i,sep='') for i in s]
from bisect import bisect_left
import sys
sys.setrecursionlimit(100000)


def dfs(v, p):
    i = bisect_left(dp, a[v])
    dp[i], old = a[v], dp[i]
    ans[v] = i
    if p != -1:
        ans[v] = max(ans[v], ans[p])
    for u in to[v]:
        if u == p:
            continue
        dfs(u, v)
    dp[i] = old

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    to = [[] for _ in range(n)]
    ans = [None]*n
    INF = 10**10
    for _ in range(n-1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        to[u].append(v)
        to[v].append(u)
    dp = [INF]*n
    dp[0] = -INF
    dfs(0, -1)
    print("\n".join(list(map(str, ans))))

### FIXED CODE ###

from bisect import bisect_left
import sys
sys.setrecursionlimit(1000000)


def dfs(v, p):
    i = bisect_left(dp, a[v])
    dp[i], old = a[v], dp[i]
    ans[v] = bisect_left(dp, INF)-1
    for u in to[v]:
        if u == p:
            continue
        dfs(u, v)
    dp[i] = old

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    to = [[] for _ in range(n)]
    ans = [0]*n
    INF = 10**20
    for _ in range(n-1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        to[u].append(v)
        to[v].append(u)
    dp = [INF]*(n+1)
    dp[0] = -INF
    dfs(0, -1)
    print("\n".join(list(map(str, ans))))
N = int(input())
A_lis = list(map(int, input().split()))
B_lis = list(map(int, input().split()))

f = [(B_lis[_]/A_lis[_], A_lis[_], B_lis[_]) for _ in range(3)]

f.sort()
f.reverse()

rest = N
gain = 0

for j in range(3):
  if f[j][0] > 1.0:
    g = rest // f[j][1]
    rest -= g * f[j][1]
    gain += g * f[j][2]

print(gain+rest)

### FIXED CODE ###

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

if K==1:
  print(0)
else:
  print(N-K)
import math
R = int(input())
print(R*2*math.pi)


### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
sumA = sum(A)
remain = N - sumA
if remain >= 0:
    print(remain)
else:
    print(-1)
from collections import deque
q = deque()
for _ in range(int(input())):
    c, *k = input().split()
    k = k[0] if k else None
    if c[0] == 'i':
        q.appendleft(k)
    elif c[0] == 'd':
        if c[6] == 'F':
            q.popleft()
        elif c[6] == 'L':
            q.pop()
        else:
            if k in q:
                q.remove(k)
print(*q)


### FIXED CODE ###

from collections import deque
from sys import stdin
q = deque()
input()
for s in stdin:
    if s == '\n':
        break
    c, *k = s.split()
    k = k[0] if k else None
    if c == 'insert':
        q.appendleft(k)
    elif c == 'delete':
        if k in q:
            q.remove(k)
    elif c == 'deleteFirst':
        q.popleft()
    else:
        q.pop()
print(*q)
n = int(input())
m = input().split()
ppp = list(map(int, m))
max = 2*10**5+1
ans = 0
for p in ppp:
    if p < max:
        ans += 1
        max = p
print(ans)

### FIXED CODE ###

inp1 = input().split()
a = int(inp1[0])
b = int(inp1[1])

if b ==0:
  print("No")
else:
  c = a%b
  if c == 0:
    print("Yes")
  else:
    print("No")
N=int(input())
a = list(map(int, input().split()))
cnt=[0]*10000
print(a)
for t in a:
    cnt[t] += 1
    cnt[t-1] += 1
    cnt[t+1] += 1
print(max(cnt))

### FIXED CODE ###

N=int(input())
a = list(map(int, input().split()))
cnt=[0]*100009
for t in a:
    cnt[t] += 1
    cnt[t-1] += 1
    cnt[t+1] += 1
print(max(cnt))
i = 0
n = (int)raw_input()
while n != 0:
  i = i+1
  print 'Case {0}: {1}'.format(i,n)
  n = (int)raw_input()

### FIXED CODE ###

n = input()
i = 1
while n != 0:
  print "Case %d: %d" % (i, n)
  n = input()
  i += 1;
from math import gcd
a, b = list(map(int, input().split(' ')))
print((a * b)//gcd(a, b))

### FIXED CODE ###

from fractions import gcd
a, b = list(map(int, input().split(' ')))
print((a * b)//gcd(a, b))
res = [0,0]
for i in range(n[0]):
    get = input().split(" ")
    get = list(map(int,get))
    

    if get[1] < n[1]:
        res[0] = get[0]
        res[1] = get[1]
    
if res[0]==0 and res[1]==0:
    print('TLE')
else:print(res[0])

### FIXED CODE ###

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


cost = 10000
res = [0,0]

for i in range(n[0]):
    get = input().split(" ")
    get = list(map(int,get))
    
    if get[1] <= n[1]:
        res[1] = get[1]
        if get[0] < cost:
            cost = get[0]


if res[1]==0:
    print('TLE')
else:print(cost)
def main():
    n, m = map(int, input().split())
    students = [tuple(map(int, input().split())) for i in range(n)]
    check_points = [tuple(map(int, input().split())) for i in range(m)]

    for a, b in students:
        dist = [abs(a - c) + abs(b - d) for c, d in check_points]
        print(check_points.index(min(dist) + 1))
        
    # for st in students:
    #     min_pt = check_points[0]
    #     for pt in check_points:
    #         if abs(pt[0] - st[0]) + abs(pt[1] - st[1]) < \
    #            abs(min_pt[0] - st[0]) + abs(min_pt[1] - st[1]):
    #             min_pt = pt
    #     print(check_points.index(min_pt) + 1)


if __name__ == "__main__":

    main()


### FIXED CODE ###

def main():
    n, m = map(int, input().split())
    students = [tuple(map(int, input().split())) for i in range(n)]
    check_points = [tuple(map(int, input().split())) for i in range(m)]

    for st in students:
        min_pt = check_points[0]
        for pt in check_points:
            if abs(pt[0] - st[0]) + abs(pt[1] - st[1]) < \
               abs(min_pt[0] - st[0]) + abs(min_pt[1] - st[1]):
                min_pt = pt
        print(check_points.index(min_pt) + 1)


if __name__ == "__main__":

    main()
from collections import Counter
ct = Counter(input())
a1, a2 = ct.most_common(2)
if a1[1] == 2 and a2[1] == 2:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

from collections import Counter
ct = Counter(input())
a = ct.most_common(2)
if len(a) == 2 and a[0][1] == 2 and a[1][1] == 2:
  print('Yes')
else:
  print('No')
n, y = map(int, input().split())

ans = "-1, -1, -1"

for n10000 in range(n+1):
    for n5000 in range(n+1):
        n1000 = n - n5000 - n10000
        money_total = n10000 * 10000 + n5000 * 5000 + n1000 * 1000
        if money_total == y:
            ans = f"{n10000} {n5000} {n1000}"

print(ans)

### FIXED CODE ###

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

for n10000 in range(n+1):
    for n5000 in range(n - n10000 + 1):
        n1000 = n - n5000 - n10000
        money_total = n10000 * 10000 + n5000 * 5000 + n1000 * 1000
        if money_total == y:
            print(n10000, n5000, n1000)
            exit()

print(-1, -1, -1)
n,a,b = map(int,input().split())

def add(n,a,b):
    ls = list(str(n))
    pl = sum([int(i) for i in ls])
    if a<= pl<= b:
        return 1
    else:
        pass

flg = 0
for i in range(n+1):
    flg += add(i,a,b)

print(flg)

### FIXED CODE ###

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

def add(n,a,b):
    ls = list(str(n))
    pl = sum([int(i) for i in ls])
    if a<= pl and pl<= b:
        return n
    else:
        return 0

flg = 0
for i in range(n+1):
    flg += add(i,a,b)

print(flg)
¥from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = LI()
now = 0
if k < n:
    for j in range(k):
        now = A[now] - 1
    print(now + 1)
    exit()


D = {0:-1}
for i in range(n):
    now = A[now] - 1
    if now in D:
        break
    D[now] = i


k -= (i + 1)
b = now
c = k % (i - D[now])
for j in range(c):
    b = A[b] - 1

print(b + 1)








### FIXED CODE ###

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = LI()
now = 0
D = {0:-1}
for i in range(min(k, n)):
    now = A[now] - 1
    if now in D:
        break
    D[now] = i

if k < n:
    print(now + 1)
    exit()

k -= (i + 1)
b = now
c = k % (i - D[now])
for j in range(c):
    b = A[b] - 1

print(b + 1)