Harryxun/stf_run huggingface dataset:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


N, u, v = map(int, input().split())
u = u - 1
v = v - 1


# In[2]:


eda = [[] for _ in range(N)]
for _ in range(N-1):
    A, B = map(int, input().split())
    eda[A-1].append(B-1)
    eda[B-1].append(A-1)


# In[3]:


eda


# In[ ]:





# In[4]:


def dfs(n1, n0):
    houmon[n1] = True
    par[n1] = n0
    for n2 in eda[n1]:
        if not houmon[n2]:
            dfs(n2, n1)
    


# In[5]:


houmon = [False] * N
par = [0] * N
dfs(v, v)


# In[6]:


par


# In[7]:


dis = 0
n = u
while n != v:
    n = par[n]
    dis += 1


# In[8]:


dis


# In[9]:


n = u
for _ in range((dis - 1) // 2):
    n = par[n]


# In[10]:


n


# In[11]:


def dfs2(n1):
    ret = 0
    for n2 in eda[n1]:
        if n2 == par[n1]:
            continue
        ret = max(ret, dfs2(n2) + 1)
    return ret


# In[12]:


y = (dis - 1) // 2
x = dis - y
z = dfs2(n)


# In[13]:


print(y + z + 1 if dis % 2 == 0 else y + z)



### FIXED CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[21]:


N, u, v = map(int, input().split())
u = u - 1
v = v - 1


# In[22]:


eda = [[] for _ in range(N)]
for _ in range(N-1):
    A, B = map(int, input().split())
    eda[A-1].append(B-1)
    eda[B-1].append(A-1)


# In[25]:


par = [0] * N
# depth = [0] * N

stack = [v]
par[v] = -1

while stack:
    n1 = stack.pop()
    for n2 in eda[n1]:
        if n2 == par[n1]:
            continue
        par[n2] = n1
#         depth[n2] = depth[n1] + 1
        stack.append(n2)
        


# In[26]:


par


# In[27]:


dis = 0
n = u
while n != v:
    n = par[n]
    dis += 1


# In[28]:


n = u
for _ in range((dis - 1) // 2):
    n = par[n]


# In[29]:


y = (dis - 1) // 2
x = dis - y
# z = depth[n]


# In[38]:


stack = [n]
depth = [0] * N
maxd = 0

while stack:
    n1 = stack.pop()
    for n2 in eda[n1]:
        if n2 == par[n1]:
            continue
        depth[n2] = depth[n1] + 1
        maxd = max(maxd, depth[n2])
        stack.append(n2)
        


# In[41]:


z = maxd


# In[43]:


if dis % 2 == 0:
    print(y + z + 1)
else:
    print(y + z)


# In[ ]:
n = int(input())
p = list(map(int, input().split()))

l = sorted(p)
count = 0

for a, b in zip(p, l):
    if (a is not b): count += 1

if ((count == 0) or ((count == 2)):
    print("YES")
else:
    print("NO")

### FIXED CODE ###

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

l = sorted(p)
count = 0

for a, b in zip(p, l):
    if (a is not b): count += 1

if ((count == 0) or (count == 2)):
    print("YES")
else:
    print("NO")
s = input()
ans = []

for i in range(len(s)):
    if s[i] == '0':
        ans.append('0')
    if s[i] == '1':
        ans.append('1')
    if s[i] == 'B':
        if len(s) != 0:
            ans.pop(-1)

print(''.join(ans))


### FIXED CODE ###

s = input()
ans = []

for i in range(len(s)):
    if s[i] == '0':
        ans.append('0')
    if s[i] == '1':
        ans.append('1')
    if s[i] == 'B':
        if len(ans) != 0:
            ans.pop(-1)

print(''.join(ans))
while True:
    n, x = [int(i) for i in input().split()]

    if n == x == 0:
        break

    count = 0
    for s in range(1, n + 1):
        for m in range(s+1, n):
            for e in range(m+1, n+1):
                if x == sum(s, m, e):
                    count += 1
    print(count)

### FIXED CODE ###

while True:
    n, x = [int(i) for i in input().split()]

    if n == x == 0:
        break

    count = 0
    for s in range(1, n + 1):
        for m in range(s+1, n):
            for e in range(m+1, n+1):
                if x == s + m + e:
                    count += 1

    print(count)
import math
a,b = map(float, input().split())
print(math.floor(((a*(b*100))/100))


### FIXED CODE ###

a,b=input().split()
a=int(a)
b=int("".join(b.split(".")))
print((a*b)//100)
import math
n, k = (int(i) for i in input().split()) 
mod = 1000000007

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

r = n - k
for i in range(1, k+1):
    print(combinations_count(r+1, i) % mod * combinations_count(k-1, i-1) % mod)
    
 
    

### FIXED CODE ###

import math
n, k = (int(i) for i in input().split()) 
mod = 1000000007

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

r = n - k
for i in range(1, k+1):
  if r + 1 >= i:
    print(combinations_count(r+1, i) % mod * combinations_count(k-1, i-1) % mod)
  else:
    print(0)
p,q,r = [2],[],[]
k = 3
while k < 100000:
  flag = True
  for t in p:
    if t*t > k:
      break
    if k%t == 0:
      flag = False
  if flag:
    p.append(k)
  k += 2
for t in p[1:]:
  if (t+1)//2 in p:
    q.append(t)
q.append(100000+3)
k = 0
for i in range(99999//2):
  while q[k]<=2*i+1:
    k += 1
  r.append(k)
c = int(input())
while c>0:
  x,y = map(int,input().split())
  print(r[y//2]-r[max(x,3)//2-1])
  c -= 1
  

### FIXED CODE ###

N = 10**5
p = [0]*(N+1)
q = [0]*(N+1)

p[2] = 1
primes = [2]

for i in range(3,N+1):
  f = 1
  for t in primes:
    if t*t > i:
      break
    if i%t == 0:
      f = 0
      break
  if f == 1:
    primes.append(i)
    p[i] = 1
    if p[(i+1)//2]:
      q[i] = 1
m = 0
s = [0]*(N+1)
for i in range(1,N+1):
  m += q[i]
  s[i] = m
  
for i in range(int(input())):
  l,r = map(int,input().split())
  print(s[r]-s[l-1])
s =int( input())
a = s[-1]
if a in [2, 4, 5, 7, 9]:
  print('hon')
elif a in [0, 1, 6, 8]:
  print('pon')
else:
  print('bon')

### FIXED CODE ###

s =int( input()[-1])
if s in [2, 4, 5, 7, 9]:
  print('hon')
elif s in [0, 1, 6, 8]:
  print('pon')
else:
  print('bon')
def hoge():
  n, m = map(int, input().split())
  data = [[int(e) for e in input().split()] for _ in range(n)]
  d1 = {}
  for i in range(n):
    a, b = data[i]
    if a not in d1:
      d1[a] = []
    d1[a].append(b)
  result = 0
  d2 = {}
  maxb = -1
  for i in range(1, m + 1):
    if i in d1:
      for j in d1[i]:
        if j not in d2:
          d2[j] = 0
        d2[j] += 1
        if j > maxb:
          maxb = j
    else:
      if len(d2) == 0:
        continue
    if maxb == -1:
      maxb = max(d2.keys())
    result += maxb
    if d2[maxb] == 1:
      del d2[maxb]
      maxb = -1
    else:
      d2[maxb] -= 1
  print(result)
main()


### FIXED CODE ###

n, m = map(int, input().split())
d1 = {}
for _ in range(n):
  a, b = map(int, input().split())
  if a in d1:
    d1[a].append(b)
  else:
    d1[a] = [b]
result = 0
d2 = {}
maxb = -1
for i in range(1, m + 1):
  if i in d1:
    for j in d1[i]:
      if j not in d2:
        d2[j] = 0
      d2[j] += 1
      if j > maxb:
        maxb = j
  else:
    if len(d2) == 0:
      continue
  result += maxb
  if d2[maxb] == 1:
    del d2[maxb]
    if len(d2) == 0:
      maxb = 0
    else:
      maxb = max(d2.keys())
  else:
    d2[maxb] -= 1
print(result)
import queue
n, m = map(int, input().split())
route = [[] for _ in range(n)]
vals = []
for _ in range(m):
    a, b = map(int, input().split())
    route[a-1].append(b-1)
    route[b-1].append(a-1)
    vals.append([a-1, b-1])
ans = m
for i in range(m):
    route2 = set()
    q = queue.Queue()
    q.put(i)
    route[vals[i][0]].remove(vals[i][1])
    route[vals[i][1]].remove(vals[i][0])
    while not q.empty():
        a = q.get()
        if not a in route2:
            route2.add(a)
            for j in route[a]:
                q.put(j)
    if len(route2) == n:
        ans -= 1
    route[vals[i][0]].append(vals[i][1])
    route[vals[i][1]].append(vals[i][0])
    
print(ans)
        


### FIXED CODE ###

import queue
n, m = map(int, input().split())
route = [[] for _ in range(n)]
vals = []
for _ in range(m):
    a, b = map(int, input().split())
    route[a-1].append(b-1)
    route[b-1].append(a-1)
    vals.append([a-1, b-1])
ans = m
for i in range(m):
    route2 = set()
    q = queue.Queue()
    q.put(0)
    route[vals[i][0]].remove(vals[i][1])
    route[vals[i][1]].remove(vals[i][0])
    while not q.empty():
        a = q.get()
        if not a in route2:
            route2.add(a)
            for j in route[a]:
                q.put(j)
        if len(route2) == n:
            ans -= 1
            break
    route[vals[i][0]].append(vals[i][1])
    route[vals[i][1]].append(vals[i][0])
print(ans)
a = map(int,input().split(" "))
b = map(int,input().split(" "))
c = map(int,input().split(" "))

d = max(b)- min(b)
e = max(c)- min(c)

f = sum(range(a[0]))
g = sum(range(a[1]))

ans = (d * e * f * g) % (10**9 + 7)

print(ans)

### FIXED CODE ###

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

x = 0
y = 0

for i in range(1,a[0]):
    x = x + b[i] - b[0]
    
xx = x
for i in range(1,a[0]-1):
    xx = xx - ((b[i] - b[i - 1]) * (a[0] - i))
    x = x + xx

for i in range(1,a[1]):
    y = y + c[i] - c[0]
    
yy = y
for i in range(1,a[1]-1):
    yy = yy - ((c[i] - c[i - 1]) * (a[1] - i))
    y = y + yy

ans = x * y
ans = ans % (10**9 + 7)

print(ans)
from heapq import *
import numpy as np
import sys
sys.setrecursionlimit(200000)

def dk(start,node,edge):
  d = [0]*node
  d[start] = 0
  q = [(0, start)]
  
  while(len(q) != 0):
    ci, i = heappop(q)
    for j in edge[i]:
      nc = ci + cost[i][j]
      if nc%2 == 1:
        d[j] = 1
      heappush(q,(nc, j))
  return d

#入力
N = int(input())
#エッジ
E = [[] for _ in [0]*(N+1000)]
cost = np.zeros((N+1000, N+1000))

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append(b-1)
  cost[a-1][b-1] = w
  cost[b-1][a-1] = w


d = dk(0, N, E)

#出力
print(*d, sep = "\n")
    

### FIXED CODE ###

import numpy as np
import sys
INF = float('inf')
sys.setrecursionlimit(10**5)
max = 10**5+5

def dk(start,node,edge):
  ans = [INF] * node
  ans[0] = 0
  q = [start]
  
  while(len(q) != 0):
    i = q.pop()
    for j, cost in edge[i]:
      if(ans[j] != INF):
        continue
      ans[j] = (ans[i] + cost)%2
      q.append(j)
  return ans

#入力
N = int(input())
#エッジ
E = [[] for _ in [0]*(max)]

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append((b-1, w))
  E[b-1].append((a-1, w))


ans = dk(0, N, E)

#出力
print(*ans, sep = "\n")
W,H,x,y=map(int,input().split())

print(W*H/2, 1 if W=2*x and H=2*y else 0)

### FIXED CODE ###

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

print(W*H/2, 1 if W==2*x and H==2*y else 0)
h,w=map(int,input().split())
for i in range(h):
  a=input()
  if 'snuke' in a:
    b=a.find('snuke')
    break
print(chr(ord('A')+b)+str(a+1))

### FIXED CODE ###

h,w=map(int,input().split())
for i in range(h):
  a=input()
  if 'snuke' in a:
    b=a.find('snuke')
    break
print(chr(ord('A')+b//6)+str(i+1))
a=int(input())
b=int(input())
c=a*b
if c%2==1:
  print('Odd')
elif c%2==0:
  print('Even')

### FIXED CODE ###

a,b=map(int,input().split())
if (a*b)%2==0:
  print("Even")
else:
  print("Odd")
N = int(input())
S = int(input())

# コーナーケース
if N == S:
    print(N + 1)
    exit()


# x進数
def encode_x_base(n, x):
    ret = []
    while n:
        ret.append(n % x)
        n //= x
    return ret[::-1]


# 桁和
def digit_sum(n):
    if isinstance(n, (int, str)):
        return sum([int(d) for d in str(n)])
    if isinstance(n, list):
        return sum([int(d) for d in n])


# Bが小さいとき
for b in range(2, int(N ** 0.5) + 1):
    encoded_N = encode_x_base(N, b)
    d_sum = digit_sum(encoded_N)
    if d_sum == S:
        print(b)
        exit()

# Bが大きいときはpを全探索
for p in range(1, int(N ** 0.5) + 1):
    b = (N - S) // p + 1
    encoded_N = encode_x_base(N, b)
    d_sum = digit_sum(encoded_N)
    if d_sum == S:
        print(b)
        break
else:
    print(-1)


### FIXED CODE ###

N = int(input())
S = int(input())

# コーナーケース
if N == S:
    print(N + 1)
    exit()


# x進数
def encode_x_base(n, x):
    ret = []
    while n:
        ret.append(n % x)
        n //= x
    return ret[::-1]


# 桁和
def digit_sum(n):
    if isinstance(n, (int, str)):
        return sum([int(d) for d in str(n)])
    if isinstance(n, list):
        return sum([int(d) for d in n])


# Bが小さいとき
for b in range(2, int(N ** 0.5) + 1):
    encoded_N = encode_x_base(N, b)
    d_sum = digit_sum(encoded_N)
    if d_sum == S:
        print(b)
        exit()

# Bが大きいときはpを全探索
for p in range(int(N ** 0.5) + 1, 0, -1):
    b = (N - S) // p + 1
    if b < 2:
        continue
    encoded_N = encode_x_base(N, b)
    d_sum = digit_sum(encoded_N)
    if d_sum == S:
        print(b)
        break
else:
    print(-1)
import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
N, M = map(int, input().split())
A = [0]*(N+1)
for i in range(M):
    a, b = map(int, input().split())
    A[a].append([b])
    A[b].append([a])
visited=[1]+[0]*N
ans = 0
def DFS(x):
    if visited[x] == 1:
        return
    if sum(visited) == N:
        ans += 1
    visited[x] = 1
    for i in range(N):
        DFS(A[x][i])
print(ans)

### FIXED CODE ###


import itertools

N, M = map(int, input().split())
adj_matrix = [[0]* N for _ in range(N)]

for i in range(M): #隣接行列
    a, b = map(int, input().split())
    adj_matrix[a-1][b-1] = 1
    adj_matrix[b-1][a-1] = 1

cnt = 0

for each in itertools.permutations(range(N)):
    if each[0] != 0:
        break
    factor = 1
    for i in range(N-1):
        factor *= adj_matrix[each[i]][each[i+1]]
    cnt += factor

print(cnt)
n = int(input())
a = list(map(int,input().split()))
ans = a[0]
for i in range(n-1):
    ans *= a[i+1]
    if ans > 10**18:
        break
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

from decimal import Decimal
a,b = map(Decimal,input().split())

print(int(a*b))
def main():
    N, M = map(int, input().split())
    S = [i == "B" for i in input()]
    ab = [list(map(int, input().split())) for _ in [0]*M]
    graph = [set() for _ in [0]*N]
    [graph[a-1].add(b-1) for a, b in ab]
    [graph[b-1].add(a-1) for a, b in ab]

    status = [[0, 0] for _ in [0]*N]

    for i in range(N):
        for j in g[i]:
            status[i][S[j]] += 1

    que = []
    for i, (p, q) in enumerate(status):
        if (p == 0) ^ (q == 0):
            que.append(i)

    while que:
        i = que.pop()
        c = S[i]
        for j in graph[i]:
            if i != j:
                graph[j].remove(i)
            status[j][c] -= 1
            p, q = status[j]
            if (p == 0) ^ (q == 0):
                que.append(j)
        g[i] = set()

    for i in g:
        if i:
            print("Yes")
            return
    print("No")


main()


### FIXED CODE ###

def main():
    from sys import stdin
    input = stdin.readline
    n, m = map(int, input().split())
    s = [i == "B" for i in input()]
    ab = [list(map(int, input().split())) for _ in [0]*m]
    g = [set() for _ in [0]*n]
    [g[a-1].add(b-1) for a, b in ab]
    [g[b-1].add(a-1) for a, b in ab]

    g2 = [[0, 0] for _ in [0]*n]

    for i in range(n):
        for j in g[i]:
            g2[i][s[j]] += 1

    ss = []
    for i, (j, k) in enumerate(g2):
        if (j == 0 and k > 0) or (j > 0 and k == 0):
            ss.append(i)

    while ss:
        i = ss.pop()
        c = s[i]
        for j in g[i]:
            if i != j:
                g[j].remove(i)
            g2[j][c] -= 1
            p, q = g2[j]
            if (p == 0 and q > 0) or (p > 0 and q == 0):
                ss.append(j)
        g[i] = set()

    for i in g:
        if len(i) > 0:
            print("Yes")
            return
    print("No")


main()
s=sorted(str(input()))
t=sorted(str(input()),reverse=True)
if s<t:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

a,b = map(int, input().split())
print(int((a+b+2-1)/2))
# -*- coding: utf-8 -*-
text = list(S)
if text[2] == text[3]:
    if text[4]==text[5]:
        print('Yes')
    else:
        prinrt('No')
else:
    print('No')

### FIXED CODE ###

# -*- coding: utf-8 -*-
text = list(input())
if text[2] == text[3]:
    if text[4]==text[5]:
        print('Yes')
    else:
        print('No')
else:
    print('No')
s = input()
N = s.count('N')
S = s.count('S')
E = s.count('E')
W = s.count('W')
if ((N >= 1 and S == 0) or (N == 0 and S >= 1) or 
    (E >= 1 and W == 0) or (E == 0 and W >= 1):
  print('Yes')
else:
  print('No')


### FIXED CODE ###

S =input()
if (('N' in S) == ('S' in S)) and (('W' in S) == ('E' in S)):
    print('Yes')
else:
    print('No')
import sys

data = sys.stdin.readline()

a, b = data.strip().split()

if a == b:
  return 0
else:
  k = (a + b) / 2
  if k.is_integer():
    return k
  else:
    return 'IMPOSSIBLE'



### FIXED CODE ###

a, b = (int(x) for x in input().split()) 
if a == b:
    print(0)
else:
  k = (a + b) / 2
  if k.is_integer():
    print(int(k))
  else:
    print('IMPOSSIBLE')
print(["NO","YES"][int(input())==7 or int(input())==5 or int(input())==3])

### FIXED CODE ###

x=input()
print(["NO","YES"][x in "357"])
import sys
from string import ascii_lowercase as aplhabets
from collections import defaultdict

readline=sys.stdin.readline

s=readline().split()
d=defautdict(int)
for x in s:
  d[x]=1
for a in alphabets:
  if d[a]=0:
    print(a)
    sys.exit()
print('None')

### FIXED CODE ###

import sys
from string import ascii_lowercase as alphabets
from collections import defaultdict

readline=sys.stdin.readline

s=readline().strip()
d=defaultdict(int)
for x in s:
  d[x]=1
for a in alphabets:
  if d[a]==0:
    print(a)
    sys.exit()
print('None')
import numpy as np

a,b = map(int,input().split())
print(round(np.mean(a,b)))

### FIXED CODE ###

a = [int(i) for i in input().split()]
print(-(-(a[0]+ a[1] )//2))
N = int(input())
 
t = [input() for i in range(N)]
 
M = int(input())
 
a = []
for i in range(M):
    a.append(map(int, raw_input().split()))       
 
cal = sum(t)
 
for j in range(M):
    print(cal - t[a[j][0] - 1] + a[j][1])
N = int(input())
 
t = [input() for i in range(N)]
 
M = int(input())
 
a = []
for i in range(M):
    a.append(map(int, raw_input().split()))       
 
cal = sum(t)
 
for j in range(M):
    print(cal - t[a[j][0] - 1] + a[j][1])


### FIXED CODE ###

N = int(input())
 
t = list(map(int, input().split()))
 
M = int(input())
 
cal = sum(t)
 
for j in range(M):
    p, x = map(int, input().split())
    print(cal - t[p - 1] + x)
a = str(input())
b = str(input())
c = str(input())
d = str(input())
e = str(input())
X = [a, b, c, d, e]
endzero = 0
elesnum = []
for x in X:
  if x[-1] == "0":
    endzero += int(x)
  else:
    elesnum.append(x)
elesnum.sort(key = lambda x: x[-1])
min_ = int(elesnum[0])
for e in elesnum[1:]:
  endzero += (int(e)//10+1)*10
print(endzero+min_)

### FIXED CODE ###

X = [input() for _ in range(5)]
endzero = 0
elesnum = []
for x in X:
  if x[-1] == "0":
    endzero += int(x)
  else:
    elesnum.append(x)
if elesnum:
  elesnum.sort(key = lambda x: x[-1])
  min_ = int(elesnum[0])
  for e in elesnum[1:]:
    endzero += (int(e)//10+1)*10
  print(endzero+min_)
else:
  print(endzero)
def givel(): 
    n=int(input()) 
    l=[int(i) for i in input().split()] 
    curr = 0 
    i = 0 
    while i<len(l): 
    	temp = i+1 
	    ans = 0 
		while temp<len(l) and l[temp]<=l[temp-1]: 
        	temp+=1 
	        ans+=1 
			i=temp 
		curr = max(curr,ans) 
    return curr
givel()

### FIXED CODE ###

def givel(): 
    n=int(input()) 
    l=[int(i) for i in input().split()] 
    curr = 0 
    i = 0 
    while i<len(l): 
        temp = i+1 
        ans = 0 
        while temp<len(l) and l[temp]<=l[temp-1]: 
            temp+=1 
            ans+=1 
        i=temp 
        curr = max(curr,ans) 
    return curr
print(givel())
import sys
input = sys.stdin.readline

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

l = [[] for _ in range(M)]
for j in range(M):
    b,c = [int(i) for i in input().split()]
    l[j] = [c,b]

l.sort(reverse=True)
A.sort()
ans = 0

for i,j in l:
    for k in range(N):
        if i <= A[k]:
            break
    k = min(k,j)
    ans += k*i
    A = A[k:]
print(ans+sum(A))


### FIXED CODE ###

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))
input_data=input()

P=int(input_data[0])

Q=int(input_data[2])

R=int(input_data[4])

X=max([P,Q,R])

print(P+Q+R-X)



### FIXED CODE ###

input_data=input()

X=input_data.split()

P=int(X[0])

Q=int(X[1])

R=int(X[2])

S=max([P,Q,R])

print(P+Q+R-S)
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1]
fi = [0]*(n+1)
a = 0
for i in range(k):
    if fi[ls[a]] == 0:
        fi[ls[a]] = i
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(a)

### FIXED CODE ###

n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])
import numpy as np

R = int(input())
print(R*2.*np.pi)


### FIXED CODE ###

import numpy as np
 
R = int(input())
print(R*2.*np.pi)
print eval(raw_input().replace(' ','*')/3.30578

### FIXED CODE ###

print eval(raw_input().replace(' ','*'))/3.305785
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    X = 19
    N, Q = next(it), next(it)
    b1, br, bc = (1 << X)-1, 2*X+1, 1<<X

    if Q == 3:
        raise 1

    A = [None] * (N+Q)
    B = [0] * N
    C = [None] * Q
    for i in range(N):
        ci = next(it)-1
        A[i] = i << (X+1) | i
        B[i] = ci
    for i in range(Q):
        l, r = next(it)-1, next(it)-1
        A[N+i] = r<< (X+1) | bc | i
        C[i] = l

    A.sort()
    dp = [0] * N

    N1 = (2*N+1)//2
    dp2 = [0] * (N1*2)

    ans = [0] * Q
    for arg in A:
        r = arg >> (X+1)
        cmd = arg & bc
        i = arg & b1
        if cmd == 0:
            ci = B[i]

            lo = N1 + dp[ci]
            hi = N1 + r

            while lo < hi:
                if lo & 1:
                    dp2[lo] += 1
                    lo += 1
                if not hi & 1:
                    dp2[hi] += 1
                    hi -= 1
                lo >>= 1; hi >>= 1;

            if lo == hi: dp2[lo] += 1

            dp[ci] = r + 1
            continue
        l = C[i]

        num = 0
        j = l + N1
        while j > 0:
            num += dp2[j]
            j >>= 1
        ans[i] = num

    print(*ans, sep="\n")


if __name__ == "__main__":
    resolve()


### FIXED CODE ###

#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    X = 19
    N, Q = next(it), next(it)
    X = Q.bit_length()
    b1, br, bc = (1 << X)-1, 2*X+1, 1<<X

    Aa = [next(it)-1 for i in range(N)]
    Aq = [0] * Q
    Ac = [0] * Q
    for i in range(Q):
        l, r = next(it)-1, next(it)-1
        Aq[i] = r<<X | i
        Ac[i] = l

    Aq.sort()
    dp = [0] * N

    N1 = (2*N+1)//2
    dp2 = [0] * (N1*2)

    ans = [0] * Q
    offset = 0
    for arg in Aq:
        r = arg >> X
        i = arg & b1

        for j in range(offset, r+1):
            ci = Aa[j]

            lo = N1 + dp[ci]
            hi = N1 + j

            while lo < hi:
                if lo & 1:
                    dp2[lo] += 1
                    lo += 1
                if not hi & 1:
                    dp2[hi] += 1
                    hi -= 1
                lo >>= 1; hi >>= 1;

            if lo == hi: dp2[lo] += 1

            dp[ci] = j + 1
        offset = r+1
        l = Ac[i]

        num = 0
        j = l + N1
        while j > 0:
            num += dp2[j]
            j >>= 1
        ans[i] = num

    print(*ans, sep="\n")


if __name__ == "__main__":
    resolve()