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

import sys

input = sys.stdin.readline

N, K = map(int, input().split())
S = set()
for _ in range(K):
    l, r = map(int, input().split())
    for i in range(l, r+1):
        S.add(i)

mod = 998244353
dp = [0 for _ in range(N+1)]
dp[1] = 1
for i in range(1, N):
    for s in S:
        if i + s <= N:
            dp[s+i] += dp[i]
            dp[s+i] %= mod

print(dp[N])

### FIXED CODE ###

import sys

input = sys.stdin.readline

N, K = map(int, input().split())
S = set()
for _ in range(K):
    l, r = map(int, input().split())
    S.add((l, r))

mod = 998244353
dp = [0 for _ in range(N+1)]
dp[1] = 1
acc = [0 for _ in range(N+1)]
acc[1] = 1
for i in range(1, N+1):
    for (l, r) in S:
        left = max(i - r - 1, 0)
        right = max(i - l, 0)
        # print("left", left, "right", right)
        dp[i] += acc[right] - acc[left]
        dp[i] %= mod
    acc[i] = acc[i-1] + dp[i]
    acc[i] %= mod

# print(acc)
print(dp[N])
### BUGGY CODE ###

a, b, n = map(int, input().split())
ans = 0
for x in range(1, n+1):
    front = (a*x - (a*x)%b)//b
    back = ((x - x%b))//b
    ans = max(ans, front - back * a)
print(ans)


### FIXED CODE ###

a, b, n = map(int, input().split())
if n < b - 1:
    print(a*(n%b)//b)
else:
    print(a*((b-1)%b)//b)
### BUGGY CODE ###

a,b,k = map(int,input().split())
     
for i in range(k):
  if a == 0 and b == 0:
    break
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1
     
print(str(a) + " " + str(b))

### FIXED CODE ###

A, B, K = map(int,input().split())

A -= K
if(A < 0):
    B -= abs(A)
    A = 0
    if(B < 0):
        B = 0

print(str(A) + " " + str(B))
### BUGGY CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
c = [float("INF")] * n
c[0] = 0
for i in range(1, n):
    for j in range(1, min(n, k + 1)):
        c[i] = min(c[i], c[i - j] + abs(h[i] - h[i - j]))

print(c[n - 1])



### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
c = [float("INF")] * n
c[0] = 0
for i in range(1, n):
    for j in range(1, min(n, k + 1)):
        c[i] = min(c[i], c[i - j] + abs(h[i] - h[i - j]))

print(c[n - 1])
### BUGGY CODE ###

n, k = map(int, input().split())
pList = list(map(int, input().split()))
pListMax = 0

for i in range(n):
    pList[i] = (pList[i] + 1) / 2

for i in range(n-k+1):
    pListSlice = pList[i:k+i]
    if sum(pListSlice) > pListMax:
        pListMax = sum(pListSlice)
print(pListMax)


### FIXED CODE ###

n, k = map(int, input().split())
pList = list(map(int, input().split()))
pKList = [(1 + p)/2 for p in pList]
pListMax = 0

for i in range(n-k+1):
    if i == 0:
        res = sum(pKList[i:i+k])
    else:
        res = res - pKList[i-1] + pKList[i+k-1]
    pListMax = max(pListMax, res)
print(pListMax)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys

N,*A = map(int, sys.stdin.buffer.read().split())

A = sorted(A)
setA = set(A)

def yakusu(x):
  if x==1:
    return []
  result = [1]
  y = 2
  while y**2<=x:
    if x%y==0:
      result.append(y)
      result.append(x//y)
    y += 1
  return result

count = N
for i in range(N):
  a = A[i]
  if i>0 and a==A[i-1]:
    count -= 1
    continue
  if i<N-1 and a==A[i+1]:
    count -= 1
    continue
  for y in yakusu(a):
    if y in setA:
      count -= 1
      break

print(count)

### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys

N,*A = map(int, sys.stdin.buffer.read().split())

S = [0 for _ in range(10**6+1)]
T = [0 for _ in range(10**6+1)]

for a in A:
  S[a] += 1

count = 0
for i in range(1,10**6+1):
  if T[i] == 1:
    continue
  if S[i] == 0:
    continue
  if S[i] == 1:
    count += 1
  for j in range(10**6//i+1):
    T[i*j] = 1

print(count)
### BUGGY CODE ###

N, K = map(int, input().split())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, input().split())

ans = (max(x) - min(x)) * (max(y) - min(y))

for i in range(N):
    for j in range(N):
        lx = x[i]
        rx = x[j]

        if lx >= rx:
            continue

        for k in range(N):
            for l in range(N):
                cnt = 0
                uy = y[k]
                dy = y[l]

                if dy >= uy:
                    continue

                for m in range(N):
                    if lx <= x[m] <= rx and dy <= y[m] <= uy:
                        cnt += 1

                if cnt >= K:
                    ans = min(ans, (rx - lx) * (uy - dy))

print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, input().split())

ans = (max(x) - min(x)) * (max(y) - min(y))

for i in range(N):
    for j in range(N):
        for k in range(N):
            for l in range(N):
                cnt = 0

                lx = x[i]
                rx = x[j]
                uy = y[k]
                dy = y[l]

                if lx >= rx or dy >= uy:
                    continue

                for m in range(N):
                    if lx <= x[m] <= rx and dy <= y[m] <= uy:
                        cnt += 1

                if cnt >= K:
                    ans = min(ans, (rx - lx) * (uy - dy))

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

inpl = lambda: list(map(int,input().split()))
H, W = inpl()
A = []
new_black = []
Nb = 0
for h in range(H):
    line = input()
    Aline = [0] * W
    for w in range(W):
        if line[w] == '#':
            Aline[w] = 1
            new_black.append((h,w))
            Nb += 1
    A.append(Aline)
Nw = H*W - Nb
t = 0
while Nw > 0:
    t += 1
    prev_black = new_black
    new_black = []
    for h, w in prev_black:
        for nh, nw in [(h+1,w), (h-1,w), (h,w+1), (h,w-1)]:
            if 0<=nh<H and 0<=nw<W and A[nh][nw] == 0:
                Nw -= 1
                A[nh][nw] = 1
                new_black.append((nh,nw))
print(t)

### FIXED CODE ###

inpl = lambda: list(map(int,input().split()))
H, W = inpl()
A = []
new_black = []
Nb = 0
for h in range(H):
    line = input()
    Aline = [0] * W
    for w in range(W):
        if line[w] == '#':
            Aline[w] = 1
            new_black.append((h,w))
            Nb += 1
    A.append(Aline)
Nw = H*W - Nb
t = 0
while Nw > 0:
    t += 1
    prev_black = new_black
    new_black = []
    for h, w in prev_black:
        for nh, nw in [(h+1,w), (h-1,w), (h,w+1), (h,w-1)]:
            if 0<=nh<H and 0<=nw<W and A[nh][nw] == 0:
                Nw -= 1
                A[nh][nw] = 1
                new_black.append((nh,nw))
print(t)
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
ai = []
 
for i, j in enumerate(a):
    ai.append([i+1,j])
    ai[i].sort()
 
c = 0
for i in range(len(ai)):
    c += ai.count(ai[i])
 
print(c // 4)

### FIXED CODE ###

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

for i, j in enumerate(a):
    ai.append([i+1,j])
    ai[i].sort()

print(len(ai) - len(list(map(list,set(map(tuple, ai))))))
### BUGGY CODE ###


N,W = map(int, input().split())
WV = [[0,0]]
for i in range(N):
    WV.append(list(map(int, input().split())))

W1 = WV[0][0]
for i in range(N):
    WV[i][0] -= W1
    

dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

for n in range(1,N+1):
    for w in range(W+1):
        if w<WV[n][0]:
            dp[n][w] = dp[n-1][w]
        else:
            dp[n][w] = max(dp[n-1][w],
                           dp[n-1][w-WV[n][0]]+WV[n][1])

print(dp[N][W])

### FIXED CODE ###


from collections import defaultdict

N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))

dp = defaultdict(int)

dp[0] = 0

for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
X,Y,Z,K = map(int, input().split())

A_list = [int(e) for e in input().split()]
B_list = [int(e) for e in input().split()]
C_list = [int(e) for e in input().split()]

delicious_list=[]

#Maxは3000通りであることを利用する
#足し算なのでA～C_listのうち上位3000しか使われない
A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)

for i in range(min(X,3000)):
  for j in range(min(Y,3000)):
    for k in range(min(Z,3000)):
      delicious_list.append(A_list[i]+B_list[j]+C_list[k])
      
delicious_list.sort(reverse=True)

for i in range(K):
  print(delicious_list[i])

### FIXED CODE ###

import numpy as np
import pprint

#入力を受け付ける
X,Y,Z,K = list(map(int, input().split()))
A_list =[int(i) for i in input().split()]
B_list =[int(i) for i in input().split()]
C_list =[int(i) for i in input().split()]

#並び替え
A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)

#処理準備
calclated_flag = np.zeros((X,Y,Z), dtype=np.bool) #計算済みか
#calclated_result = np.zeros((X,Y,Z), dtype=np.int64) #計算結果 　…stockを使うので不要
ans = [] #解答

#木の頭
x=0
y=0
z=0
stock=[[x,y,z,A_list[x] + B_list[y] + C_list[z]]]
calclated_flag[x,y,z]=True

#ここから解答が条件の手前になるまでループ
for i in range(K):
  #stockの中から最大を調べる
  stock.sort(key=lambda x: x[3]) #ソート
  ans.append(stock[len(stock)-1][3]) #最小は一番最後。ansに追加

  temp = stock.pop() #stockから削除。
  x=temp[0]
  y=temp[1]
  z=temp[2]

  #子に分岐 ここで範囲外にならないよう判断が必要
  if x+1 <= X-1:
    if calclated_flag[x+1,y,z]!=True:
      stock.append([x+1,y,z,A_list[x+1] + B_list[y] + C_list[z]])
      calclated_flag[x+1,y,z]=True
      
  if y+1 <= Y-1:
    if calclated_flag[x,y+1,z]!=True: 
      stock.append([x,y+1,z,A_list[x] + B_list[y+1] + C_list[z]])
      calclated_flag[x,y+1,z]=True
      
  if z+1 <= Z-1:
    if calclated_flag[x,y,z+1]!=True:
      stock.append([x,y,z+1,A_list[x] + B_list[y] + C_list[z+1]])
      calclated_flag[x,y,z+1]=True
  
#回答を出力
for i in range(K):
  print(ans[i])
### BUGGY CODE ###

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

def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    if len(set(l))==1:
        for i in range(n):
            print(max(l))
    else:
        biggest = max(l)
        lsort=sorted(l)
        niban=lsort[-2]
        for j in range(n):
            if l[j]!=biggest:
                print(biggest)
            else:
                print(niban)
resolve()

### FIXED CODE ###

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

def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    biggest = max(l)
    lsort=sorted(l)
    niban=lsort[-2]
    for j in range(n):
        if l[j]!=biggest:
            print(biggest)
        else:
            print(niban)
resolve()
### BUGGY CODE ###

n = int(raw_input())
ax = []
bx = []
for _ in xrange(n):
    a,b = map(int,raw_input().split())
    ax.insert(0,a)
    bx.insert(0,b)

cnt = 0
for i in xrange(n):
    if ax[i] % bx[i] !=0:
        k = bx[i] - (ax[i] % bx[i])
        cnt += k
        ax = map(lambda x:x+k,ax)
print cnt


### FIXED CODE ###

n = int(raw_input())
ax = []
bx = []
for _ in xrange(n):
    a,b = map(int,raw_input().split())
    ax.append(a)
    bx.append(b)

ax = ax[::-1]
bx = bx[::-1]
cnt = 0
for i in xrange(n):
    if (ax[i] + cnt) % bx[i] !=0:
        k = bx[i] - ((ax[i] + cnt) % bx[i])
        cnt += k

print cnt
### BUGGY CODE ###

import bisect as bi

def ans(s,t,x):
    i=bi.bisect_left(s,x)
    sl=s[i-1];sr=s[i]
    j=bi.bisect_left(t,x)
    tl=t[j-1];tr=t[j]
    # print(sl,sr,tl,tr)
    return min(abs(x-sl)+abs(sl-tl),abs(x-sl)+abs(sl-tr),abs(x-sr)+abs(sr-tl),abs(x-sr)+abs(sr-tr),\
        abs(x-tl)+abs(tl-sl),abs(x-tl)+abs(tl-sr),abs(x-tr)+abs(tr-sl),abs(x-tr)+abs(tr-sr))

A,B,Q=map(int,input().split())

s=[-10**11]
for i in range(A):
    s.append(int(input()))
s.append(10**11)

t=[-10**11]
for i in range(B):
    t.append(int(input()))
t.append(10**11)

for i in range(Q):
    print(ans(s,t,int(input())))

### FIXED CODE ###

import bisect as bi

A,B,Q=map(int,input().split())

s=[-10**11]
for i in range(A):
    s.append(int(input()))
s.append(10**11)

t=[-10**11]
for i in range(B):
    t.append(int(input()))
t.append(10**11)

for k in range(Q):
    x=int(input())
    i=bi.bisect_left(s,x)
    sl=s[i-1];sr=s[i]
    j=bi.bisect_left(t,x)
    tl=t[j-1];tr=t[j]
    # print(sl,sr,tl,tr)
    print(min(abs(x-sl)+abs(sl-tl),abs(x-sl)+abs(sl-tr),abs(x-sr)+abs(sr-tl),abs(x-sr)+abs(sr-tr),\
        abs(x-tl)+abs(tl-sl),abs(x-tl)+abs(tl-sr),abs(x-tr)+abs(tr-sl),abs(x-tr)+abs(tr-sr)))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)

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

ans = 1000

def dfs(money, share, day):
    global ans
    if day > n:
        ans = max(ans, money)
        return
    max_buy = money//A[day-1]
    max_sel = share
#    for n_buy in range(-max_sel, max_buy+1):
#        dfs(money-n_buy*A[day-1], share+n_buy, day+1)
    for n_buy in [-max_sel, 0, max_buy]:
        dfs(money-n_buy*A[day-1], share+n_buy, day+1)

dfs(1000,0,1)

print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

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

CurrentMoney = 1000

for i in range(0, n-1):
    Stocks = 0
    if A[i] < A[i+1]: Stocks = CurrentMoney // A[i]
    CurrentMoney += (A[i+1] - A[i]) * Stocks

print(CurrentMoney)
### BUGGY CODE ###

# coding:utf-8
def main():
    a = int(input())
    lists = []
    lists= [int(input()) for _ in range(a)]

    temp = lists.copy()

    for i in range(a):
        lists = temp.copy()
        lists.pop(i)
        ans = max(lists)
        print(ans)

if __name__ == '__main__' :
    main()

### FIXED CODE ###

# coding:utf-8
def main():
    a = int(input())
    lists= [int(input()) for _ in range(a)]

    temp = lists.copy()
    first = max(temp)
    temp.remove(first)
    second = max(temp)

    for i in range(a):
        if lists[i] == first:
            print(second)
        else:
            print(first)

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

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

num=[i+1 for i in range(N)]
l=zip(num,X)
l=sorted(l,key=lambda x:x[1])
indexlist=[]
for i in range(N):
    indexlist.append(l[i][0])
for i in range(N):
    x=indexlist.index(i+1)
    if x<=N//2-1:
        ans=l[N//2][1]
    else:
        ans=l[N//2-1][1]
    print(ans)

### FIXED CODE ###

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

# Xをソート(Xsort)
# 除く数字が中央より左の場合、答えは中央右(r)の数字
# 逆も然り
Xsort=sorted(X)
l=(N-1)//2
r=l+1
th=Xsort[l]
for i in range(N):
    if X[i]<=th:
        ans=Xsort[r]
    else:
        ans=Xsort[l]
    print(ans)
### BUGGY CODE ###

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

a_max = max(A)
a_min = min(A)
dummy = -float("inf")

for x in range(a_min, a_max+1):
    cnt = 0
    A_replace = [l for l in A if l -1 <= x and l + 1 >= x]
    if dummy < len(A_replace):
        dummy = len(A_replace)
        ans = dummy
print(ans)


### FIXED CODE ###

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

a_max = max(A)
a_min = min(A)

cnt_dict = {}
for i in range(a_min-1, a_max+2):
    cnt_dict[i] = 0
for i in range(len(A)):
    cnt_dict[A[i]-1] += 1
    cnt_dict[A[i]] += 1
    cnt_dict[A[i]+1] += 1

print(max(cnt_dict.items(), key = lambda x: x[1])[1])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N=int(input())
A=[int(input()) for i in range(N)]
SUM=sum(A)
MAX=(SUM-1)//2

mod=998244353

DPLIST=[0]*(MAX+2)
DPLIST[0]=1

for a in A:
    for i in range(MAX+1,-1,-1):
        if i==MAX+1:
            DPLIST[i]=DPLIST[i]*3%mod
        else:
            DPLIST[min(i+a,MAX+1)]=(DPLIST[min(i+a,MAX+1)]+DPLIST[i])%mod
            DPLIST[i]=DPLIST[i]*2%mod

if SUM%2==1:
    print((sum(DPLIST)-DPLIST[-1]*3)%mod)
    sys.exit()

MAX=SUM//2
       
DPLIST2=[0]*(MAX+2)
DPLIST2[0]=1

for a in A:
    for i in range(MAX+1,-1,-1):
        DPLIST2[min(i+a,MAX+1)]=(DPLIST2[min(i+a,MAX+1)]+DPLIST2[i])%mod

        
print((sum(DPLIST)-DPLIST[-1]*3+DPLIST2[-2]*3)%mod)


### FIXED CODE ###

import sys
input = sys.stdin.readline

N=int(input())
A=[int(input()) for i in range(N)]
SUM=sum(A)
MAX=(SUM-1)//2

mod=998244353

DPLIST=[0]*(MAX+2)
DPLIST[0]=1

for a in A:
    for i in range(MAX+1,-1,-1):
        if i==MAX+1:
            DPLIST[i]=DPLIST[i]*3%mod
        else:
            DPLIST[min(i+a,MAX+1)]=(DPLIST[min(i+a,MAX+1)]+DPLIST[i])%mod
            DPLIST[i]=DPLIST[i]*2%mod

if SUM%2==1:
    print((sum(DPLIST)-DPLIST[-1]*3)%mod)
    sys.exit()

MAX=SUM//2
       
DPLIST2=[0]*(MAX+2)
DPLIST2[0]=1

for a in A:
    for i in range(MAX+1,-1,-1):
        DPLIST2[min(i+a,MAX+1)]=(DPLIST2[min(i+a,MAX+1)]+DPLIST2[i])%mod

        
print((sum(DPLIST)-DPLIST[-1]*3+DPLIST2[-2]*3)%mod)
### BUGGY CODE ###

from sys import stdin

d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

input = stdin.readline

n = int(input())

tree = [[0] * n * 2 for _ in range(26)]

def build(arr):
    for i in range(n):
        tree[ddd][n + i] = arr[i]

    for i in range(n - 1, 0, -1):
        tree[ddd][i] = tree[ddd][i << 1] + tree[ddd][i << 1 | 1]

def update(p, value):
    p += n
    tree[ddd][p] = value

    i = p

    while i > 1:
        tree[ddd][i >> 1] = tree[ddd][i] + tree[ddd][i ^ 1]
        i >>= 1

def query(l, r):
    res = 0

    l += n
    r += n

    while l < r:

        if l & 1:
            res += tree[ddd][l]
            l += 1

        if r & 1:
            r -= 1
            res += tree[ddd][r]

        l >>= 1
        r >>= 1

    return res

s = list(input())

arrs = [[0] * n for _ in range(26)]

for i in range(n):
    arrs[d[s[i]]][i] = 1

for ddd in range(26):
    build(arrs[ddd])

for _ in range(int(input())):
    t = input().split()

    if t[0] == '1':
        i, c = t[1:]
        i = int(i) - 1

        new = d[c]
        cur = d[s[i]]

        s[i] = c

        ddd = cur
        update(i, 0)

        ddd = new
        update(i, 1)

    if t[0] == '2':
        l, r = map(int, t[1:])
        l -= 1
        r -= 1

        ans = 0

        for ddd in range(26):
            if query(l, r + 1) != 0:
                ans += 1

        print(ans)

### FIXED CODE ###

from sys import stdin

d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

input = stdin.readline

n = int(input())

tree = [[0] * n * 2 for _ in range(26)]

def build(arr):
    for i in range(n):
        tree[ddd][n + i] = arr[i]

    for i in range(n - 1, 0, -1):
        tree[ddd][i] = tree[ddd][i << 1] + tree[ddd][i << 1 | 1]

def update(p, value):
    p += n
    tree[ddd][p] = value

    i = p

    while i > 1:
        tree[ddd][i >> 1] = tree[ddd][i] + tree[ddd][i ^ 1]
        i >>= 1

def query(l, r):
    res = 0

    l += n
    r += n

    while l < r:

        if l & 1:
            res += tree[ddd][l]
            l += 1

        if r & 1:
            r -= 1
            res += tree[ddd][r]

        l >>= 1
        r >>= 1

    return res

s = list(input())

arrs = [[0] * n for _ in range(26)]

for i in range(n):
    arrs[d[s[i]]][i] = 1

for ddd in range(26):
    build(arrs[ddd])

for _ in range(int(input())):
    t = input().split()

    if t[0] == '1':
        i, c = t[1:]
        i = int(i) - 1

        new = d[c]
        cur = d[s[i]]

        s[i] = c

        ddd = cur
        update(i, 0)

        ddd = new
        update(i, 1)

    if t[0] == '2':
        l, r = map(int, t[1:])
        l -= 1
        r -= 1

        ans = 0

        for ddd in range(26):
            if query(l, r + 1) != 0:
                ans += 1

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

#!/usr/bin/env python3
import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, K = IL(int)
    A = IL(lambda x: int(x) - 1)
    memo = [0]
    i = 0
    while(not A[i] in memo):
        memo.append(A[i])
        idx = i
        i = A[i]
    if K < len(memo):
        print(memo[K] + 1)
    else:
        loop = memo[memo.index(A[i]):]
        print(loop[(K - (len(memo) - len(loop))) % len(loop)] + 1)


solve()


### FIXED CODE ###

#!/usr/bin/env python3
import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, K = IL(int)
    A = IL(lambda x: int(x) - 1)
    memo = [0]
    visited = [False] * N
    i = 0
    while(not visited[A[i]]):
        memo.append(A[i])
        visited[A[i]] = True
        i = A[i]
    if K < len(memo):
        print(memo[K] + 1)
    else:
        loop = memo[memo.index(A[i]):]
        print(loop[(K - (len(memo) - len(loop))) % len(loop)] + 1)


solve()
### BUGGY CODE ###

n = int(input())
v = list(input().split())
from collections import Counter
x, y = list(Counter(v[::2]).items()), list(Counter(v[1::2]).items())
x.sort(key=lambda i:i[1], reverse=True)
y.sort(key=lambda i:i[1], reverse=True)
r = []
for a, b in x[:max(len(x), 3)]:
    for aa, bb in y[:max(len(y), 3)]:
        if a == aa:
            r.append(n-max(b, bb))
        else:
            r.append(n-b-bb)
print(min(r))

### FIXED CODE ###

import collections
n = int(input())
v = list(map(int, input().split(' ')))
a = v[::2]
b = v[1::2]

a_count = []
b_count = []

a_count = collections.Counter(a).most_common(3)
b_count = collections.Counter(b).most_common(3)

if a_count[0][0] != b_count[0][0]:
    print(n-int(a_count[0][1])-int(b_count[0][1]))
else:
    if len(a_count)!= 1 and len(b_count)!= 1:
        print(min(n-int(a_count[0][1])-int(b_count[1][1]),n-int(a_count[1][1])-b_count[0][1]))
    elif len(a_count) == 1 and len(b_count)!=1:
        print(n-int(a_count[0][1])-int(b_count[1][1]))
    elif len(b_count)==1 and len(a_count)!= 1:
        print(n-int(a_count[1][1])-int(b_ocunt[0][1]))
    else:
        print(int(n/2))
### BUGGY CODE ###

from fractions import gcd
mod = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

lcm = A[0]
for i in range(1, N):
    lcm = (lcm*A[i]) // gcd(lcm, A[i])

ans = 0
for i in range(N):
    ans += lcm // A[i]
    ans %= mod
print(ans)

### FIXED CODE ###

from fractions import gcd
mod = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

lcm = A[0]
for i in range(1, N):
    lcm = (lcm*A[i]) // gcd(lcm, A[i])

ans = 0
for i in range(N):
    ans += lcm // A[i]
ans %= mod
print(ans)
### BUGGY CODE ###

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

cnt = 0
while True:
  if 1 in map(lambda x: x % 2, li):
    break
  else:
    cnt += 1
print(cnt)


### FIXED CODE ###

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

    num_of_operation = 0
    is_dividable = True

    while True:
        for num in li:
            if num % 2 != 0:
                is_dividable = False
                break

        if not is_dividable:
            break

        for i in range(N):
            li[i] //= 2
        num_of_operation += 1

    print(num_of_operation)


if __name__ == '__main__':
    main()