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

import sys
input = sys.stdin.readline

sys.setrecursionlimit(10**5 + 5)  # 再帰回数の上限！！

n = int(input())
A = [-1] + [int(input()) - 1 for i in range(n-1)]

M = [[] for i in range(n)]
for i in range(1, n):
    M[A[i]].append(i)

# 0-indexedの場合
V = [0] * n

# print(M)

def des(x):
    D = []
    V[x] = 1
    for i in M[x]:
        if V[i] == 0:
            V[i] = 1
            D.append(des(i))
    D.sort(reverse=True)
    # print(x,D)
    for i in range(len(D)):
        D[i] += i+1
    if len(D) == 0:
        ans = 0
    else:
        ans = max(D)
    return ans

print(des(0))

### FIXED CODE ###

import sys
input = sys.stdin.readline

sys.setrecursionlimit(10**5 + 5)  # 再帰回数の上限！！

n = int(input())
A = [-1] + [int(input()) - 1 for i in range(n-1)]

M = [[] for i in range(n)]
for i in range(1, n):
    M[A[i]].append(i)

# 0-indexedの場合
V = [0] * n

# print(M)

def des(x):
    D = []
    V[x] = 1
    for i in M[x]:
        if V[i] == 0:
            V[i] = 1
            D.append(des(i))
    D.sort(reverse=True)
    # print(x,D)
    for i in range(len(D)):
        D[i] += i+1
    if len(D) == 0:
        ans = 0
    else:
        ans = max(D)
    return ans

print(des(0))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
n,m=mii()

from fractions import gcd
from functools import reduce

#list
def gcd_list(numbers):
    return reduce(gcd, numbers)
ans=0
for i in range(1,m//n+1):
    p=gcd(i,m-i*n)
    ans=max(ans,p)
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
n,m=mii()

def mdiv(n):
    divisors=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:

            divisors.append(i)
            if i!=n//i:

                divisors.append(n//i)
    return divisors
#print(mdiv(m))

ans=0
for i in mdiv(m):
    if i<=m//n:
        ans=max(i,ans)
print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

    dp = [[[0 for k in range(N + 1)]for j in range(N + 1)]for i in range(N + 1)]


    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    for k in range(three + 1):
        for j in range(two + three + 1 - k):
            for i in range(one + two + three + 1 -k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                ret = 0
                if i > 0:
                    ret += dp[i - 1][j][k] * i
                if j > 0:
                    ret += dp[i + 1][j - 1][k] * j 
                if k > 0:
                    ret += dp[i][j + 1][k -1] * k

                ret += N
                ret *= 1.0 / (i + j + k)
                dp[i][j][k] = ret

    print(dp[one][two][three])

main()


### FIXED CODE ###

import sys
input = sys.stdin.readline

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

    dp = [[[0.0 for k in range(N + 1)]for j in range(N + 1)]for i in range(N + 1)]


    one = A.count(1)
    two = A.count(2)
    three = A.count(3)

    for k in range(three + 1):
        for j in range(two + three + 1 - k):
            for i in range(one + two + three + 1 -k -j):
                if i == 0 and j == 0 and k == 0:
                    continue
                ret = N * 1.0
                if i > 0:
                    ret += dp[i - 1][j][k] * i
                if j > 0:
                    ret += dp[i + 1][j - 1][k] * j 
                if k > 0:
                    ret += dp[i][j + 1][k -1] * k

                dp[i][j][k] = ret / (i + j + k)

    print(dp[one][two][three])

main()
### BUGGY CODE ###

n= int(input())
a = list(map(int, input().split()))
flag = True
res = 0
while flag:
    max_num = max(a)
    res += (a[a.index(max_num)] * len(a[a.index(max_num):]))- sum(a[a.index(max_num):])
    a = a[:a.index(max_num)]
    if len(a) == 0:
        flag = False

print(res)

### FIXED CODE ###

n= int(input())
a = list(map(int, input().split()))
res =0
x = a[0]
for i in range(len(a)):
    if x < a[i]:
        x = a[i]
    res += x - a[i]

print(res)
### BUGGY CODE ###

S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            S=S+A[i][2]
        elif A[i][1]=="2":
            S=A[i][2]+S
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            S=A[i][2]+S
        elif A[i][1]=="2":
            S=S+A[i][2]
if c%2==1:
    S=S[::-1]
print(S)

### FIXED CODE ###

S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
R=""
L=""
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            R=R+A[i][2]
        elif A[i][1]=="2":
            L=A[i][2]+L
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            L=A[i][2]+L
        elif A[i][1]=="2":
            R=R+A[i][2]
S=L+S+R
if c%2==1:
    S=S[::-1]
print(S)
### BUGGY CODE ###

def Sqrt(x, e=0.0001):
    p = 0.0
    q = x
    while abs(q - p) > e:
        p = q
        q = p - (p*p - x)/(2.*p)
    return q

N=int(input())
a=0
b=0
x=1
for i in range(N+1):
  a=Sqrt(i)
  b=100*a
  c=int(b/100)
  if(i==c*c):
    x=i
print(x)
   

### FIXED CODE ###

import math
N=int(input())
n=math.sqrt(N)
m=math.floor(n)
print(m*m)
### BUGGY CODE ###

R,G,B,n=map(int,input().split())
ans=0
for r in range(((n+1)//R)+1):
    for g in range(((n+1)//G)+1):
        if ((n-r*R - g*G)%B)== 0 and (n-r*R-g*G)>=0:
            ans+=1
print(ans)


### FIXED CODE ###

R,G,B,n=map(int,input().split())
ans=0
for r in range(((n+1)//R)+1):
    for g in range(((n+1)//G)+1):
        if ((n-r*R - g*G)%B)== 0 and (n-r*R-g*G)>=0:
            ans+=1
print(ans)
### BUGGY CODE ###

s = input()
n = int(input())
f = 0
for i in range(n):
  l = input().replace(" ","")
  if l[0] == "2":
    if (l[1] == "1" and f == 0) or (l[1] == "2" and f == 1):
      s = l[2] + s
    else:
      s = s + l[2]
  else:
    if f == 0:
      f = 1
    else:
      f = 0

print(s if f == 0 else s[::-1])


### FIXED CODE ###

from collections import deque
s = deque(input())
#s = input()
n = int(input())
f = 1
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    tmp = int(l[1])*f
    if tmp == 1 or tmp == -2:
      s.appendleft(l[2])
    else:
      s.append(l[2])
  else:
    f *= -1

if f == -1:
  s.reverse()
print("".join(s))
### BUGGY CODE ###

from collections import Counter
n, k = map(int, input().split())
a = list(input().split())
counter = Counter(a)
count = 0
while len(counter) > k:
    max_c = min(counter.values())
    for name, cnt in counter.most_common():
        if cnt == max_c:
            count += cnt
            counter.pop(name)
            break
print(count)

### FIXED CODE ###

from collections import Counter
n, k = map(int, input().split())
a = list(input().split())
len_a = len(set(a))
if len_a <= k:
    print(0)
else:
    counter = Counter(a)
    count = 0
    s = len_a - k
    sort_counter = sorted(counter.items(), key=lambda  x:x[1])
    for x in range(s):
        count += sort_counter[x][1]
    print(count)
### BUGGY CODE ###

n, a, b = map(int, input().split())
M = 10**9 + 7
total = pow(2, n, M)

def comb(n, k, p):
    if k > n-k:
        return comb(n, n-k, p)
    num, den = 1, 1
    for i in range(k):
        num = num * (n-i) % p
        den = den * (i+1) % p
    return num * pow(den, p-2) % p

print((total - 1 + (M - comb(n,a,M)) + (M - comb(n,b,M))) % M)


### FIXED CODE ###

n, a, b = map(int, input().split())
M = 10**9 + 7
total = pow(2, n, M)

def comb(n, k, p):
    if k > n-k:
        return comb(n, n-k, p)
    num, den = 1, 1
    for i in range(k):
        num = num * (n-i) % p
        den = den * (i+1) % p
    return num * pow(den, p-2, p) % p

print((total - 1 + (M - comb(n,a,M)) + (M - comb(n,b,M))) % M)
### BUGGY CODE ###

import itertools as iter
import math as m
from collections import deque

def main():
    N, M = list(map(int, input().split()))
    R = []
    for _ in range(M):
        R.append(list(map(int, input().split())))
    
    q = deque()
    s = set()
    r = [0] * (N+1)
    q.append(1)
    while any(q):
        x = q.popleft()
        s.add(x)
        ro = R[:]
        for i in ro:
            if i[0] == x and i[1] not in s and i[1] not in q:
                if r[i[1]] == 0:
                    r[i[1]] = i[0]
                q.append(i[1])
                R.remove(i)
            elif i[1] == x and i[0] not in s and i[0] not in q:
                if r[i[0]] == 0:
                    r[i[0]] = i[1]
                q.append(i[0])
                R.remove(i)
    if len(s) == N:
        print("Yes")
        for i in r[2:]:
            print(i)
    else:
        print("No")
    
        

if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import deque

def main():
    N, M = list(map(int, input().split()))
    R = [set() for _ in range(N+1)]
    for _ in range(M):
        x = list(map(int, input().split()))
        R[x[0]].add(x[1])
        R[x[1]].add(x[0])
    
    q = deque()
    #s = set()
    r = [0] * (N+1)
    q.append(1)
    while any(q):
        x = q.popleft()
        #s.add(x)
        ro = R[x]
        for i in ro:
            #if r[i] == 0 and i not in s and i not in q:
            if r[i] == 0: 
                r[i] = x
                q.append(i)
    #if len(s) == N:
    print("Yes")
    print("\n".join(map(str, r[2:])))
    #else:
    #    print("No")

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

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

def solve(i, m):
    if m == 0:
        return True
    elif i >= n:
        return False
    else:
        return solve(i+1, m) or solve(i+1, m - A[i])
    

for num in m:
    print('yes' if solve(0, num) else 'no')



### FIXED CODE ###

# used DP

n = int(input())
A = list(map(int, input().split() ))
q = int(input())
M = list(map(int, input().split()))


dp = [[None]*(max(M)+1) for i in range(n)]

# we refer to the list dp only when i = 0, 1, ... , n-1 and m >= 0.
def solve(i, m):    
    if i >= n:
        return m == 0
    
    elif m < 0:
        return False
    
    elif dp[i][m] is not None:
        return dp[i][m]
    
    else:
        if m == 0:
            dp[i][m] = True
        elif solve(i+1, m):
            dp[i][m] = True
        elif solve(i+1, m - A[i]):
            dp[i][m] = True
        else:
            dp[i][m] =  False        
        return dp[i][m]

    
for num in M:
    print('yes' if solve(0, num) else 'no')
### BUGGY CODE ###

def main(N):
    ans = 0

    for i in range(1, N+1):
        for j in range(i, N+1, i):
            ans += j
    print(ans)


if __name__ == '__main__':
    N = int(input())
    main(N)


### FIXED CODE ###

def main():
    N = int(input())
    A = [1]*(N+1)
    A[0] = 0

    for i in range(2, N+1):
        j = i
        while j <= N:
            A[j] += 1
            j += i
    print(sum(i*a for i, a in enumerate(A)))


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

import sys
sys.setrecursionlimit(10**7)

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

def rec(arr):
    if len(arr)==1:
        if arr[0] == 1:
            return [1]
        else:
            return False
    for i,a in enumerate(arr):
        if i+1 == a:
            res = rec(arr[:i] + arr[i+1:])
            if res:
                res.append(i+1)
                return res
    return False

ans = rec(B)
if ans:
    print(*ans,sep='\n')
else:
    print(-1)

### FIXED CODE ###

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

ans = []
while B:
    for i,a in reversed(list(enumerate(B))):
        if i+1 == a:
            ans.append(a)
            del B[i]
            break
    else:
        print(-1)
        exit()
print(*ans[::-1], sep='\n')
### BUGGY CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
def power(x, y):
   if y == 0:
       return 1
   elif y == 1:
       return x % mod
   elif y % 2 == 0:
       return power(x, y/2) ** 2 % mod
   else:
       return power(x, (y-1)/2) ** 2 * x % mod

if N%2 == 0: # 偶数の時
	for i in range(N//2):
		if A.count(N-1-2*i) != 2:
			# print(N-1-2*i)
			print(0)
			break
	else:
		print(power(2,(N//2)))
else: # 奇数の時
	if A.count(0) != 1:
		print(0)
	else:
		for i in range((N-1)//2):
			if A.count(N-1-2*i) != 2:
				# print(N-1-2*i)
				print(0)
				break
		else:
			print(power(2,((N-1)//2)))

### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

dic = {}
def power(x, y):
   if y == 0:
       return 1
   elif y == 1:
       return x % mod
   elif y % 2 == 0:
       return power(x, y/2) ** 2 % mod
   else:
       return power(x, (y-1)/2) ** 2 * x % mod

for i in range(N):
	if not A[i] in dic:
		dic[A[i]] = 1
	else:
		dic[A[i]] += 1

if N%2 == 0: # 偶数の時
	for i in range(N//2):
		if N-1-2*i not in dic:
			print(0)
			break
		elif dic[N-1-2*i] != 2:
			print(0)
			break
		else:
			pass
	else:
		print(power(2,(N//2)))
else: # 奇数の時
	if A.count(0) != 1:
		print(0)
	else:
		for i in range(N//2-1):
			if N-1-2*i not in dic:
				print(0)
				break
			elif dic[N-1-2*i] != 2:
				print(0)
				break
			else:
				pass
		else:
			print(power(2,(N//2)))
### BUGGY CODE ###

from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue

        ans = (ans*f(k-2)//f(k-2-len(edge[i])+1))%bign
    print(ans)


main()


### FIXED CODE ###

from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue
        t = k-2
        for j in range(len(edge[i])-1):
            ans = ans * t % bign
            t-=1
        
    print(ans)


main()
### BUGGY CODE ###

a, b, x = map(int,input().split())
ans = 0  
for i in range(a, b+1):
  if i%x==0:
    ans+=1
print(ans)
 

### FIXED CODE ###

a, b, x = map(int,input().split())
print((b//x) - ((a-1)//x))
### BUGGY CODE ###

N = int(input())
s=[]
for i in range(N):
  flag = 0
  a = input()
  for i in range(len(s)):
    if(s[i] == a):
      flag += 1
      break
  if(flag == 0):
    s.append(a)
print(len(s))

### FIXED CODE ###

import collections
N = int(input())
s=[]
for i in range(N):
  s.append(input())
c = collections.Counter(s)
 
print(len(c))
### BUGGY CODE ###

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

for i in range(n):
  t=x[:i]+x[i+1:]
  t.sort()
  print(t[(n-1)//2])

### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
t=sorted(x)
right=t[n//2]
left=t[n//2-1]

for i in x:
  if i <= left:
    print(right)
  elif i >= right:
    print(left)
### BUGGY CODE ###

n=int(input())
A=list(map(int,input().split()))
s=1
for a in A:
  s*=a
print(-1 if s>10**18 else s)

### FIXED CODE ###

n=int(input())
A=list(map(int,input().split()))
s=1

if 0 in A:
  s=0
else:
  for a in A:
    s*=a
    if s>10**18:
      s=-1
      break
print(s)
### BUGGY CODE ###

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

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

P = 10**9 + 7
N = 1000001
inv_t = [0]+[1]
for i in range(2,N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

n=int(input())
A=[int(i) for i in input().split()]

#kyodai=lcm_list(A)
kyodai=A[0]
for i in range(1,n):
    kyodai=lcm_base(kyodai,A[i])

ans=0

for i in range(n):
    ans+=kyodai*inv_t[A[i]]
    ans=ans%P

print(ans)


### FIXED CODE ###

def gcd(a,b):
    for xx in range(1000001):
        a,b=b,a%b
        if b==0:
            break
    return a

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

P = 10**9 + 7
N = 1000001
inv_t = [0]+[1]
for i in range(2,N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

n=int(input())
A=[int(i) for i in input().split()]

#kyodai=lcm_list(A)
kyodai=A[0]
for i in range(1,n):
    kyodai=lcm_base(kyodai,A[i])

ans=0

for i in range(n):
    ans+=inv_t[A[i]]
    ans=ans%P
ans=ans*kyodai
ans=ans%P

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

n,m=map(int,input().split(' '))
Mod=1000000007
spl=int(n**0.5)+1
f,k=[i for i in range(spl)]+[n//i for i in range(1,spl)][::-1],2*spl-1
dp=[0]+[1]*(k-1)
for i in range(m):
	dp2=[0]*k
	for j in range(1,k):
		dp2[j]=(dp2[j-1]+dp[k-j]*(f[j]-f[j-1]))%Mod
	dp=dp2
print(dp[k-1])

### FIXED CODE ###

n,m=map(int,input().split(' '))
Mod=1000000007
spl=int(n**0.5)+1
f,k=[i for i in range(spl)]+[n//i for i in range(1,spl)][::-1],2*spl-1
dp=[0]+[1]*(k-1)
for i in range(m):
	dp2=[0]*k
	for j in range(1,k):
		dp2[j]=(dp2[j-1]+dp[k-j]*(f[j]-f[j-1]))%Mod
	dp=dp2
print(dp[k-1])
### BUGGY CODE ###

num, poi, ans = [int(x) for x in input().split()]

list01 = [poi for x in range(num)]

i = 1
while i <= ans:
  list01 = [x -1 for x in list01]
  j = int(input())
  list01[j -1] += 1
  i += 1
  
for k in list01:
  if k <= 0:
    print("No")
  else:
    print("Yes")


### FIXED CODE ###

num, poi, ans = [int(x) for x in input().split()]

list01 = [poi for x in range(num)]
list02 = [0 for x in range(num)]

for i in range(ans):
  j = int(input())
  list02[j-1] += 1

for i in range(num):
  list02[i] = list02[i] + list01[i] - ans

for k in list02:
  if k <= 0:
    print("No")
  else:
    print("Yes")
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
    if r[cur] is not None:
        m = i - r[cur]
        K -= ((K-i)//m)*m
    r[cur] = i
    i += 1
    if i == K:
        print(A[cur])
        exit()
    cur = A[cur] - 1

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
    if m is None and r[cur] is not None:
        m = i - r[cur]
        K = i + (K-i)%m
    else:
        r[cur] = i
    if i == K:
        print(cur+1)
        exit()
    i += 1
    cur = A[cur] - 1
### BUGGY CODE ###

from statistics import mean, median,variance,stdev
import sys
import math
import fractions
def j(a):
    if a: print("AC")
    else :print("WA")


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

#x = ip()                              #入力整数1つ
x,y = (int(i) for i in input().split())      #入力整数横2つ
#x,y,z = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列

max = 1
ls = int(math.sqrt(y))+1
s = []
if y==1 or x>=y:print(1)
elif x==1:print(y)
else:
    n = y
    for i in range(2,y//2+1):
        if n==1:break
        l=0
        while n%i == 0:
             n = n//i
             l+=1
        s.append([i,l])
    for i in range(len(s)):
        s[i][1]//=x
    num = 1
    for i in range(len(s)):
        if s[i][1]: num*=(pow(s[i][0],s[i][1]))
    print(num)


### FIXED CODE ###

from statistics import mean, median,variance,stdev
import sys
import math
import fractions
def j(a):
    if a: print("AC")
    else :print("WA")


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

#x = ip()                              #入力整数1つ
x,y = (int(i) for i in input().split())      #入力整数横2つ
#x,y,z = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列

max = 1
ls = int(math.sqrt(y))+1
s = []
if y==1 or x>=y:print(1)
elif x==1:print(y)
else:
    n = y
    for i in range(2,ls):
        if n==1:break
        l=0
        while n%i == 0:
             n = n//i
             l+=1
        s.append([i,l])
    for i in range(len(s)):
        s[i][1]//=x
    num = 1
    for i in range(len(s)):
        if s[i][1]: num*=(pow(s[i][0],s[i][1]))
    print(num)