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



def read_input():
    s = input()
    k = int(input())
    return s, k

def submit():
    s, k = read_input()

    subs = set()
    for i in range(len(s)):
        for j in range(i+1,len(s)+1):
            subs.add(s[i:j])

    subs = [[c for c in s] for s in subs]
    subs.sort()

    print(''.join(subs[k - 1]))

if __name__ == '__main__':
    submit()


### FIXED CODE ###



def read_input():
    s = input()
    k = int(input())
    return s, k


# sから、candsの間に挟まる文字列(長さl)を探す
def search_insertion(s, cands, l):
    result = []

    for i in range(len(s)):
        for c in cands:
            if s[i:i+l].startswith(c):
                result.append(s[i:i+l])

    cands.extend(result)
    cands = list(set(cands))
    return cands


def sort_s(cands):
    t = [[c for c in s] for s in cands]
    t.sort()
    return [''.join(s) for s in t]


def submit():
    s, k = read_input()

    cands = list(set([c for c in s]))
    precands = None
    for i in range(2, len(s) + 1):
        cands = search_insertion(s, cands, i)
        cands = sort_s(cands)
        cands = cands[:k]

        if precands == cands:
            break
        precands = cands[:]

    print(cands[-1])

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

a = int(input())
def getSum(n):
    s = 0
    while (n != 0):
        s = s + (n % 10)
        n = n // 10
    return s
c = getSum(a)
if c % 9 == 0 :
    print("Yes")
else:
    print("No")

### FIXED CODE ###

a = input()
s = 0
for i in a:
    s += int(i)
if s % 9 == 0 :
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

#いい加減深さ優先探索, 幅優先探索をスムーズに実装してえなおい

H,W = map(int,input().split())

S = ["#"] * (H+2)
S[0] = ["#"] * (W+2)
S[H+1] = ["#"] * (W+2)
for i in range(1,H+1):
    S[i] = ["#"]+ list(input()) + ["#"]

inf = 10 ** 3
m = 0
mi = -1
mj = -1
mk = -1
ml = -1
  
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            continue
        data = [[inf]*(W+2) for k in range(H+2)]
        data[i][j] = 0
        stack = [[i,j,0]]
        while stack:
            x = stack.pop()
            if S[x[0]+1][x[1]] == "." and data[x[0]+1][x[1]]>x[2]+1:
                data[x[0]+1][x[1]] = x[2]+1
                stack.append([x[0]+1,x[1],x[2]+1])
            if S[x[0]][x[1]+1] == "." and data[x[0]][x[1]+1]>x[2]+1:
                data[x[0]][x[1]+1] = x[2]+1
                stack.append([x[0],x[1]+1,x[2]+1])
            if S[x[0]-1][x[1]] == "." and data[x[0]-1][x[1]]>x[2]+1:
                data[x[0]-1][x[1]] = x[2]+1
                stack.append([x[0]-1,x[1],x[2]+1])
            if S[x[0]][x[1]-1] == "." and data[x[0]][x[1]-1]>x[2]+1:
                data[x[0]][x[1]-1] = x[2]+1
                stack.append([x[0],x[1]-1,x[2]+1])
            #print(i,j,stack)

        for k in range(1,H+1):
            for l in range(1,W+1):
                if data[k][l] > m and data[k][l] != inf:
                    m = data[k][l]
                    mi = i
                    mj = j
                    mk = k
                    ml = l

#print(mi,mj,mk,ml)
print(m)

#DFSならワイの実装力でも割とスムーズにできそう？
#なんで昨日これやらなかったんや
#と言いつつWAするアホ

### FIXED CODE ###

#いい加減深さ優先探索, 幅優先探索をスムーズに実装してえなおい

H,W = map(int,input().split())

S = ["#"] * (H+2)
S[0] = ["#"] * (W+2)
S[H+1] = ["#"] * (W+2)
for i in range(1,H+1):
    S[i] = ["#"]+ list(input()) + ["#"]

inf = 10 ** 3
m = 0
mi = -1
mj = -1
mk = -1
ml = -1
  
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            continue
        data = [[inf]*(W+2) for k in range(H+2)]
        data[i][j] = 0
        stack = [[i,j,0]]
        while stack:
            x = stack.pop()
            if S[x[0]+1][x[1]] == "." and data[x[0]+1][x[1]]>x[2]+1:
                data[x[0]+1][x[1]] = x[2]+1
                stack.append([x[0]+1,x[1],x[2]+1])
            if S[x[0]][x[1]+1] == "." and data[x[0]][x[1]+1]>x[2]+1:
                data[x[0]][x[1]+1] = x[2]+1
                stack.append([x[0],x[1]+1,x[2]+1])
            if S[x[0]-1][x[1]] == "." and data[x[0]-1][x[1]]>x[2]+1:
                data[x[0]-1][x[1]] = x[2]+1
                stack.append([x[0]-1,x[1],x[2]+1])
            if S[x[0]][x[1]-1] == "." and data[x[0]][x[1]-1]>x[2]+1:
                data[x[0]][x[1]-1] = x[2]+1
                stack.append([x[0],x[1]-1,x[2]+1])
            #print(i,j,stack)

        for k in range(1,H+1):
            for l in range(1,W+1):
                if data[k][l] > m and data[k][l] != inf:
                    m = data[k][l]
                    mi = i
                    mj = j
                    mk = k
                    ml = l

#print(mi,mj,mk,ml)
print(m)

#DFSならワイの実装力でも割とスムーズにできそう？
#なんで昨日これやらなかったんや
#と言いつつWAするアホ
### BUGGY CODE ###

import itertools
d,g=map(int,input().split())
p=[]
c=[]
for i in range(d):
    p1,c1=map(int,input().split())
    p.append(p1)
    c.append(c1//100)
n=list(itertools.permutations(list(range(1,d+1))))
ans=2000
g//=100
for i in range(len(n)):
    cnt=g
    cnt2=0
    for j in range(d):
        num3=n[i][j]
        num4=num3*p[num3-1]
        if num4>=cnt:
            cnt2+=((cnt-1)//num3)+1
            break
        elif num4+c[num3-1]>=cnt:
            cnt2+=p[num3-1]
            break
        else:
            cnt-=num4+c[num3-1]
            cnt2+=p[num3-1]
    ans=min(ans,cnt2)
print(ans)


### FIXED CODE ###

import itertools
d,g=map(int,input().split())
p=[]
c=[]
for i in range(d):
    p1,c1=map(int,input().split())
    p.append(p1)
    c.append(c1//100)
n=list(itertools.permutations(list(range(1,d+1))))
ans=1024
g//=100
for i in range(len(n)):
    cnt=g
    cnt2=0
    for j in range(d):
        num3=n[i][j]
        num4=num3*p[num3-1]
        if num4>=cnt:
            cnt2+=((cnt-1)//num3)+1
            break
        elif num4+c[num3-1]>=cnt:
            cnt2+=p[num3-1]
            break
        else:
            cnt-=num4+c[num3-1]
            cnt2+=p[num3-1]
    ans=min(ans,cnt2)
print(ans)
### BUGGY CODE ###

N=int(input())
a = list(map(int, input().split()))
for i in range(N):
    print(a.index(i+1)+1)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):
  b[a[i]-1]=str(i+1)
print(" ".join(b))
### BUGGY CODE ###

N=int(input())
s=N
ans=0
m=N
for i in range(N-1,0,-1):
    if i%s<m%s:
        ans+=m%s
        m=s-1
        s-=1
    else:
        ans+=i%s
        s-=1
print(ans)
        

### FIXED CODE ###

N=int(input())
if N%2==0:
    s=int(N/2)
    print(s*(N-1))
if N%2==1:
    s=int((N-1)/2)
    print(s*N)
### BUGGY CODE ###

n, k= map(int, input().split())
lis = list(map(int, input().split()))
dp = []
tmp = []
for _ in range(n+1):
    dp.append(0)

for i in range(2, k+1):
    if i <= n:
        dp[i] = abs(lis[i-1] - lis[0])
       # print(i, dp[i])
#print("start")
for i in range(k+1, n+1):
    for t in range(1, k+1):
        cost_t = abs(lis[i-1] - lis[i-1 - t]) + dp[i-t]
        tmp.append(cost_t)
    dp[i] = min(tmp)
    tmp = []
    #print(i, dp[i])
print(dp[n])

### FIXED CODE ###

def main():
    n, k= map(int, input().split())
    lis = list(map(int, input().split()))
    dp = []
    tmp = []
    for _ in range(n+1):
        dp.append(0)

    for i in range(2, k+1):
        if i <= n:
            dp[i] = abs(lis[i-1] - lis[0])
        # print(i, dp[i])
    #print("start")
    for i in range(k+1, n+1):
        for t in range(1, k+1):
            cost_t = abs(lis[i-1] - lis[i-1 - t]) + dp[i-t]
            tmp.append(cost_t)
        dp[i] = min(tmp)
        tmp = []
        #print(i, dp[i])
    print(dp[n])

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

def main():
  import sys
  input=sys.stdin.buffer.readline
  n=int(input())+1
  data=[0]*2*n
  def update(i,x):
    i+=n
    data[i]=2**(x-97)
    i//=2
    while i:
      data[i]=data[i*2]|data[i*2+1]
      i//=2
  def query(l,r):
    l+=n
    r+=n
    s=0
    while l<r:
      if l&1:
        s|=data[l]
        l+=1
      if r&1:
        r-=1
        s|=data[r]
      l//=2
      r//=2
    return bin(s).count('1')
  for i,c in enumerate(input()[:-1]):
    update(i,c)
  for _ in range(int(input())):
    q,a,b=input().split()
    if q[0]>49:print(query(int(a)-1,int(b)))
    else:update(int(a)-1,b[0])
main()

### FIXED CODE ###

import sys
input=sys.stdin.buffer.readline
n=int(input())+1
data=[0]*n*2
def update(i,x):
  i+=n
  data[i]=x
  i//=2
  while i:
    data[i]=data[i*2]|data[i*2+1]
    i//=2
def query(l,r):
  l+=n
  r+=n
  s=0
  while l<r:
    if l&1:
      s|=data[l]
      l+=1
    if r&1:
      r-=1
      s|=data[r]
    l//=2
    r//=2
  return sum(map(int,bin(s)[2:]))
for i,c in enumerate(input()[:-1]):
  update(i,2**(c-97))
for _ in range(int(input())):
  q,a,b=input().split()
  if q[0]>49:print(query(int(a)-1,int(b)))
  else:update(int(a)-1,2**(b[0]-97))
### BUGGY CODE ###

N, P = map(int, input().split())
S = input()

ans = 0
for i in range(N) :
    for j in range(i+1,N+1) :
        if int(S[i:j])%P == 0 :
            ans += 1

print(ans)


### FIXED CODE ###

N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
now = 0
t = 1
for i in range(N) :
    now += int(S[-1-i]) * t
    now %= P
    modP[now] += 1
    t = t * 10 % P

for r in modP :
    ans += r*(r-1)//2

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

A, B, N = map(int,input().split())
fivex = 0
i = 1
ans = 0
x = 0
while x <= N:
    x = -((-1)*i*B // A)
    if x > N:
        break
    now = i - A *  int(x / B)
    if now > ans and x <= N:
        ans = now
    i += 1

print(ans)

### FIXED CODE ###

A, B, N = map(int,input().split())
fivex = 0
i = 1
ans = 0
x = 0
# ans_x = -((-1)*A*N // B)
# if ans_x>N:
#     ans_x -= 1
# print(ans_x)
# print(int(A*ans_x/B)-A*int(ans_x/B))
while x <= B:
    x = -((-1)*i*B // A)
    if x > N:
        break
    now = i - A *  int(x / B)
    if now > ans and x <= N:
        # print(x)
        ans = now
    i += 1

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

T=input()
for i in range(T):
	A,B,C,D=map(int,raw_input().split())
	if A<B:
		print "No"
	elif B>D:
		print "No"
	else:
		A=A%B

		while A<=B:
			if (D-B)%B==0:
				if A>C and A<B:
					print "No"
					break
				else:
					print "Yes"
					break

			A+=(D-B)%B
			if A>C and A<B:
				print "No"
				break
		else:
			print "Yes"

### FIXED CODE ###

import fractions

def func(A,B,C,D):
#	print "A,B,C,D::",A,B,C,D

	if A<B:
		print "No"
	elif B>D:
		print "No"
	elif B<=C:
		print "Yes"

	else:
		A%=B
		g=fractions.gcd(B,D)
		if g==B:
			if A>C:
				print "No"
			else:
				print "Yes"
		elif B-g>C:
			print "No"
		else:
			print "Yes"
T=input()

for i in range(T):
	a,b,c,d=map(int, raw_input().split())
	func(a,b,c,d)
### BUGGY CODE ###

nk = list(map(int, input().split()))
N = nk[0]
K = nk[1]
p = list(map(int, input().split()))



def calc(x):
    sum_p = 0
    for i in range(x,x+K):
        sum_p = sum_p + p[i]
        #print(sum_p)
    return sum_p

def max_cal(max_p,sum_p):
    if(max_p < sum_p):
        max_p = sum_p
    return max_p

if __name__ == '__main__':
    max_p = 0
    for j in range(N-K+1):
        sum_p = calc(j)
        max_p = max_cal(max_p, sum_p)
    #print(max_p)
    print((max_p+K)/2)

### FIXED CODE ###

nk = list(map(int, input().split()))
N = nk[0]
K = nk[1]
p = list(map(int, input().split()))



def max_cal(max_p,sum_p):
    if(max_p < sum_p):
        max_p = sum_p
    return max_p

if __name__ == '__main__':
    max_p = 0
    sum_p = 0
    for k in range(K):
        sum_p = sum_p + p[k]
        max_p = max_cal(max_p, sum_p)
    #print(sum_p)
    for j in range(1,N-K+1):
        sum_p = sum_p - p[j-1] + p[j+K-1]
        max_p = max_cal(max_p, sum_p)
    #print(max_p)
    print((max_p+K)/2)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.buffer.readline
def inp():
    return map(int,input().split())
n, q = [int(i) for i in input().split()]
e = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = inp()
    e[a-1].append(b-1)
    e[b-1].append(a-1)

p = [0] * n
for _ in range(q):
    pp, x = inp()
    p[pp - 1] += x


def dfs(i=0, r=-1):
    for j in e[i]:
        if j != r:
            p[j] += p[i]
            dfs(j, i)
dfs()

print(" ".join(map(str,p)))


### FIXED CODE ###

import sys

sys.setrecursionlimit(1000000)


def input():
    return sys.stdin.readline()


n, q = [int(i) for i in input().split()]
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    i, j = [int(i)-1 for i in input().split()]
    ab[i].append(j)
    ab[j].append(i)
p = [0]*n
for _ in range(q):
    i, j = [int(i) for i in input().split()]
    p[i-1] += j

al = [False]*n
pp = [0]*n


def a(i=0, j=0):
    al[i] = True
    j += p[i]
    pp[i] = str(j)
    for k in ab[i]:
        if not al[k]:
            a(k, j)


a()
print(" ".join(pp))
### BUGGY CODE ###

n = int(input())
l = [int(input()) for _ in [0]*n]
#最大値が2つある場合に注意しないといけない。
#最大値が2つ以上ある場合を先に処理
import sys
if l.count(max(l))>1:
  for i in range(n):
    print(max(l))
#O(n)の処理なら何回やってもいいくらいの気持ちでいく
else:
  s=set(l)
  s.remove(max(s))
  m = max(s)
  for i in l:
    if i == max(l):
      print(m)
    else:
      print(max(l))

### FIXED CODE ###

n = int(input())
numlist = []
for i in range(n):
    numlist.append(int(input()))
argmax = numlist.index(max(numlist))
maxnum = max(numlist)
del numlist[argmax]
secondnum = max(numlist)
                       
for i in range(argmax):
    print (maxnum)
print (secondnum)
for i in range(argmax+1,n):
    print (maxnum)
### BUGGY CODE ###

import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[[0] * 4 for _ in range(C+1)] for _ in range(R+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            dp[i][j][0] = max(dp[i][j-1][0],max(dp[i-1][j]))
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[i][j][k] = max(dp[i][j-1][k],dp[i][j-1][k-1]+m[i][j])
                dp[i][j][1] = max(dp[i][j][1],max(dp[i-1][j])+m[i][j])
            else:
                for k in range(1,4):
                    dp[i][j][k] = dp[i][j-1][k]
    print(max(dp[-1][-1]))

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1]))

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

S = list(input())
n = len(S)

dp = [0] * 2019
dp[0] = 1
tmp = 0
for i in range(n):
    tmp += int(S[n-1-i]) * (10**i)
    dp[tmp%2019] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()

S = list(input())
n = len(S)

dp = [0] * 2019
dp[0] = 1
tmp = 0
ten = 1
for i in range(n):
    tmp += int(S[n-1-i]) * ten
    tmp %= 2019
    ten *= 10
    ten %= 2019
    dp[tmp%2019] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)
### BUGGY CODE ###

import copy

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

tmp = copy.deepcopy(s)
wait = 0
last_day = k
count = []

for num, i in enumerate(tmp):
    if last_day == 0:
        break
    if wait>0:
        wait -= 1
        continue
    if i == 'o':
        s[num] = 'a'
        count.append(num)
        wait = c
        last_day -= 1

# print(count)        
ans = []    
for j in count:
    wait = 0
    tmp = copy.deepcopy(s)
    last_day = k
    tmp[j] = 'x'
    for num, i in enumerate(tmp):
        if last_day == 0:
            break
        if wait>0:
            wait -= 1
            continue
        if i == 'o' or i == 'a':
            wait = c
            last_day -= 1
    if last_day > 0:
        ans.append(j+1)
        
for i in ans:
    print(i)

### FIXED CODE ###

import copy

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

tmp = copy.deepcopy(s)
wait = 0
last_day = k
count = set()

for num, i in enumerate(tmp):
    if wait>0:
        wait -= 1
        continue
    if i == 'o':
        count.add(num)
        wait = c
        last_day -= 1
        if last_day == 0:
            break

tmp = copy.deepcopy(s)
wait = 0
last_day = k
count2 = set()
for num, i in enumerate(reversed(tmp)):
    if wait>0:
        wait -= 1
        continue
    if i == 'o':
        count2.add(n - num - 1)
        wait = c
        last_day -= 1
        if last_day == 0:
            break

count = list(count)
count2 = list(count2)
count.sort()
count2.sort()
ans = []
for i in range(len(count)):
    if count[i]==count2[i]:
        ans.append(count[i]+1)

for i in ans:
    print(i)
### BUGGY CODE ###

from fractions import gcd
from functools import reduce

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

def gcd_list(numbers):
    return reduce(gcd, numbers)

ans = 0
  
for i in range(N):
    ans = max(ans, gcd_list(a[0:i]+a[i+1:]))
         
print(ans)

### FIXED CODE ###

from fractions import gcd

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

g1 = [0]*N
g2 = [0]*N

g1[0] = a[0]
g2[N-1] = a[N-1]

for i in range(1, N):
    g1[i] = gcd(a[i-1], g1[i-1])
    g2[N-1-i] = gcd(a[N-i], g2[N-i])
    
g1[0] = g2[0]
g2[N-1] = g1[N-1]

g = [gcd(g1[i], g2[i]) for i in range(N)]
print(max(g))
### BUGGY CODE ###

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int , input().split()))
    max_number = 10 ** 18
    result = 1
    if 0 in A:
        print(0)
        return
    for a in A:
        result = result * a
    if result > max_number:
        print(-1)
    else:
        print(result)

    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int , input().split()))
    max_number = 10 ** 18
    result = 1
    if 0 in A:
        print(0)
        return
    for a in A:
        result = result * a
        if result > max_number:
            print(-1)
            return
    print(result)
    return


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

from math import gcd

# = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
mA = max(A)
counter = [0 for _ in range(mA+1)]

primenum = [2]
sgn = [-1 for _ in range(int(mA**0.5)+1)]
k = 3
while k**2 <= mA:
  if sgn[k] == -1:
    primenum.append(k)
    add = k*k
    ok = k
    while k**2 <= mA:
      sgn[k] = 1
      ok += add
  k += 2
#prime_num が用意されているとき
def factorization(n):
  M = n
  for p in primenum:
    if p**2 > M:
      break
    else:
      if M%p == 0:
        if counter[p] == 0:
          counter[p] = 1
        else:
          return False
        while M%p == 0:
          M //= p

  if M > 1:
    if counter[M] == 0:
      counter[M] = 1
    else:
      return False
  return True



g = A[0]
flag = True
for k in range(N):
  a = A[k]
  g = gcd(g, a)
  if flag:
    flag = factorization(a)


if flag:
  print('pairwise coprime')
  exit()
if g == 1:
  print('setwise coprime')
else:
  print('not coprime')

### FIXED CODE ###

from math import gcd

# = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
mA = max(A)
counter = [0 for _ in range(mA+1)]

primenum = [2]
sgn = [-1 for _ in range(int(mA**0.5)+1)]
k = 3
while k**2 <= mA:
  if sgn[k] == -1:
    primenum.append(k)
    add = k*k
    ok = k
    while ok**2 <= mA:
      sgn[ok] = 1
      ok += add
  k += 2
#prime_num が用意されているとき
def factorization(n):
  M = n
  for p in primenum:
    if p**2 > M:
      break
    else:
      if M%p == 0:
        if counter[p] == 0:
          counter[p] = 1
        else:
          return False
        while M%p == 0:
          M //= p

  if M > 1:
    if counter[M] == 0:
      counter[M] = 1
    else:
      return False
  return True



g = A[0]
flag = True
for k in range(N):
  a = A[k]
  g = gcd(g, a)
  if flag:
    flag = factorization(a)


if flag:
  print('pairwise coprime')
  exit()
if g == 1:
  print('setwise coprime')
else:
  print('not coprime')
### BUGGY CODE ###

#abc_167_D
#doubling 
#類題:agc_006_c

n,k = [int(i) for i in input().split()]
a   = [int(i)-1 for i in input().split()]

#do doubling
s = list(range(n))
while k>0:
    if k&1:
        s = [s[a[i]] for i in range(n)]
    a = [a[a[i]] for i in range(n)]
    k >>= 1
    
#初期値0に対するk移動後の位置は、s[0]で求まる,
print(s[0]+1)


'''
pos = 1
move= 0
colors = [-1]*(n+1)

while(colors[pos]==-1):
    colors[pos] = move
    move += 1
    pos = a[pos]
    if move==k:
        print(pos)
        break
else:
    turn = move - colors[pos]
    rest = (k-colors[pos]) % turn
    while rest:
        pos = a[pos]
        rest -= 1
    print(pos)
'''

### FIXED CODE ###

n,k = [int(i) for i in input().split()]
a = [0] + [int(i) for i in input().split()]

pos = 1
move= 0
colors = [-1]*(n+1)

while(colors[pos]==-1):
    colors[pos] = move
    move += 1
    pos = a[pos]
    if move==k:
        print(pos)
        break
else:
    turn = move - colors[pos]
    rest = (k-colors[pos]) % turn
    while rest:
        pos = a[pos]
        rest -= 1
    print(pos)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())

edge = [[]] * n
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edge[a].append(b)
    edge[b].append(a)

imos = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    imos[p] += x

def dfs(v, p):
    for u in edge[v]:
        if u != p:
            imos[u] += imos[v]
            dfs(u, v)

dfs(0, -1)
print(' '.join([str(x) for x in imos]))


### FIXED CODE ###

from collections import deque

n, q = map(int, input().split())
edge = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    edge[a].append(b)
    edge[b].append(a)

imos = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    imos[p] += x

dq = deque([(0, -1)])
while dq:
    v, p = dq.popleft()
    for u in edge[v]:
        if u == p:
            continue
        imos[u] += imos[v]
        dq.append((u, v))

print(' '.join([str(x) for x in imos]))
### BUGGY CODE ###

a = int(input())
p = 100
cnt=0
while(p!=a):
  cnt+=1
print(cnt)

### FIXED CODE ###

a = int(input())
p = 100
cnt=0
while(1):
  cnt+=1
  p+=p//100
  if(p>=a):
    break
print(cnt)
### BUGGY CODE ###

N,K = map(int,input().split())
A = list(map(lambda x: int(x)-1,input().split()))

o = [0] * 63

def bi(x):
    i = 0
    while x != 0:
        o[i] = x%2
        x //= 2
        i += 1

bi(K)

n = 0
for i in range(63):
    if o[i]:
        n = A[n]
    A = [A[A[x]] for x in range(N)]
        
print(n+1)

### FIXED CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N):
    A[i] -= 1

P = [-1]*N
P[0] = 0

cnt = 0
NOW = 0
while True:
    NOW = A[NOW]
    cnt += 1
    if P[NOW] == -1:
        P[NOW] = cnt
        if cnt == K:
            print(NOW+1)
            exit()
    else:
        L = cnt - P[NOW]
        for i in range(P[NOW],P[NOW]+L):
            if K % L != i % L:
                continue
            for k,v in enumerate(P):
                if v == i:
                    print(k+1)
                    exit()