Harryxun/stf_run huggingface dataset:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
if 2*k+1+2*l_sub<l:
    r
    su-=y[2*l_sub]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)


### FIXED CODE ###

from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
#最後のところの判定っぽいな
if 2*k+1+2*l_sub<l:
    su-=y[2*l_sub]
    su-=y[2*l_sub+1]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)
n, y = list(map(int, input().split()))

for a in range(0, n+1):
    for b in range(0, n-a+1):
        c = n-(a+b)+1
        if (10000*a + 5000*b + 1000*c) == y:
                print(a, b, c)
                break()

print(-1, -1, -1)


### FIXED CODE ###

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

for a in range(0, n+1):
    for b in range(0, n-a+1):
        c = n-(a+b)
        if (10000*a + 5000*b + 1000*c) == y:
            print(a, b, c)
            exit()

print(-1, -1, -1)
l = list(map(int,input().split()))
a = 0
b = 1
c = 1
while c <= l[0]:
    if a + b >= l[1] and a + b <= l[2]:
        sum += c
    if b == 9:
        b = 0
        a += 1
    else:
        b += 1
    c = 10 * a + b
print(sum)


### FIXED CODE ###

l = sorted(list(map(int, input().split())))
if l[0] + l[1] == l[2]:
    print("Yes")
else:
    print("No")
a,b,c,d = map(int, input(),split())

if b < c or d < a:
  print("0")
elif a <= c:
  if b <= d:
    print(b-c)
  else:
    print(d-c)
else:
  if c <= a:
    print(d-a)
  else:
    print(d-c)

### FIXED CODE ###

a,b,c,d = map(int, input().split())
 
print(max(min(b,d) - max(a,c),0))
a=list(map(int,input().split()))
print(sort(a))


### FIXED CODE ###

a=list(map(int,input().split()))
a.sort()
print(a[0],a[1],a[2])
nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

N = int(input())
s = [''.join(sorted(input())) for i in range(N)]
ns = []
ans = 0
for i in range(len(s)):
    if ns.count(s[i]) == 0:
        aas = s.count(s[i])
        if aas > 0:
            ns.append(s[i])
        abs = cmb(aas,2)
        ans += int(abs)
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
s = Counter()
for i in range(N):
    s[''.join(sorted(input()))] += 1
ans = 0
for kw, count in s.most_common():
    for i in range(1, int(count)):
        ans += i
    if int(count) < 2:
        break
print(ans)
print(["No","Yes"][int(input)>=30]) 

### FIXED CODE ###

print(["No","Yes"][int(input())>=30])
a = map(int,raw_input().split())
a.sort()
print a[0], a[1], a[3]

### FIXED CODE ###

a, b, c = map(int,raw_input().split())
str = [a, b, c]
str.sort()
print str[0], str[1] , str[2]
n=input()
box=[int(input())-1 for i in range(int(n))]
print(sum(box))

### FIXED CODE ###

n=int(input())
s=list(map(int,input().split()))
print(sum(s)-n)
a, b = map(int,input().split())
if (a*b)%2 =0 :
    print("Odd")
else:
    print("Even")


### FIXED CODE ###

a, b = map(int,input().split())
if (a*b)%2 :
    print("Odd")
else:
    print("Even")
n = int(input())
point = []
for i in range(n):
    x_, y_ = map(int, input().split())
    point.append((x_, y_))

point.sort(key=lambda x: x[0])
ans = 0
x = point[:][0]
y = point[:][1]
for i in range(n):
    for j in range(i+1, n):
        count = 0
        p = x[j] - x[i]
        q = y[j] - y[i]
        for k in range(n):
            for l in range(k+1, n):
                if (x[l] - x[k] == p) and (y[l] - y[k] == q):
                    count += 1
        ans = max(ans, count)

print(n - ans)


### FIXED CODE ###

n = int(input())
point = []
for i in range(n):
    x_, y_ = map(int, input().split())
    point.append([x_, y_])

point.sort(key=lambda x: x[1])
point.sort(key=lambda x: x[0])
ans = 0
x = []
y = []
for i in range(n):
    x.append(point[i][0])
    y.append(point[i][1])

for i in range(n):
    for j in range(i+1, n):
        count = 0
        p = x[j] - x[i]
        q = y[j] - y[i]
        for k in range(n):
            for l in range(k+1, n):
                if (x[l] - x[k] == p) and (y[l] - y[k] == q):
                    count += 1
        ans = max(ans, count)

print(n - ans)
r, g, b = input()
a = int(r + g + b)
if a % 4 == 0:
    print('YES')
else:
    print('NO')

### FIXED CODE ###

r, g, b = input().split()
a = int(r + g + b)
if a % 4 == 0:
    print('YES')
else:
    print('NO')
a,b=map(int,input().split( ))
a_12 = int(a[0:2])
a_3 = int(a[2])
a_45 = int(a[3:5])
b_12 = int(b[0:2])
b_3 = int(b[2])
b_45 = int(b[3:5])
a_21 = int(a[1]+a[0])
b_21 = int(b[1]+b[0])
ans = (b_12-a_12+1)*10 - a_3 - (10 - b_3 - 1)
if a_21 < a_45:
    ans = ans - 1
if b_21 > b_45:
    ans = ans - 1
print(ans)

### FIXED CODE ###

a,b=map(str,input().split( ))
a_12 = int(a[0:2])
a_3 = int(a[2])
a_45 = int(a[3:5])
b_12 = int(b[0:2])
b_3 = int(b[2])
b_45 = int(b[3:5])
a_21 = int(a[1]+a[0])
b_21 = int(b[1]+b[0])
ans = (b_12-a_12+1)*10 - a_3 - (10 - b_3 - 1)
if a_21 < a_45:
    ans = ans - 1
if b_21 > b_45:
    ans = ans - 1
print(ans)
import sys

A=[[0,0,0]]
size = 0

def insert(A,x):
    global size

    fp=A[0][1]
    try:
        i=[e[0] for e in A].index(-1)
        A[i]=[x,fp,0]
        A[fp][2]=i
        A[0][1]=i
    except:
        A.append([x,fp,0])
        A[fp][2]=len(A)-1
        A[0][1]=len(A)-1     
    size += 1    

def delete(A,x):
    global size
    p =A[0][1]
    while A[p][0]!=x:
        p=A[p][1]
        if p==0: return
    fp=A[p][1]
    bp=A[p][2]
    A[fp][2]=bp
    A[bp][1]=fp
    A[p]=[-1,-1,-1]
    size -= 1
    return

def deletefirst(A):
    global size
    if size == 0: return
    p=A[0][1]
    fp=A[p][1]
    A[0][1]=fp
    A[fp][2]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

def deletelast(A):
    global size
    if size == 0: return
    p=A[0][2]
    bp=A[p][2]
    A[0][2]=bp
    A[bp][1]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

n = int(raw_input())
for i in range(n):
    s = (raw_input().split())
    if s[0][0] == 'i':#'insert':
        insert(A,int(s[1]))
    elif len(s[0]) == 6;#'delete':
        delete(A,int(s[1]))
    elif s[0][7] == 'F':#'deleteFirst':
        deletefirst(A)
    elif s[0][7] == 'L':#'deleteLast':
        deletelast(A)

e = A[0][1]
while e!=0:
    print A[e][0],
    e = A[e][1]

### FIXED CODE ###

import sys,gc
    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),
"""
問題文
    N段の階段があります。高橋君は現在、上り口(0段目)にいます。 高橋君は一歩で1段か2段上ることができます。
    ただし、a1,a2,a3,....aM段目の床は壊れており、その段に足を踏み入れることは危険です。
    壊れている床を踏まないようにしながら、最上段(N段目)にたどりつくまでの移動方法は何通りあるでしょうか？ 
    総数を1,000,000,007で割った余りを求めてください。

制約
    1≦N≦10^5
    0≦M≦N−1
    1≦a1<a2< ... <aM≦N−1
"""
#入力の受け取り
N, M = map(int, input().split())
hoal = [int(input()) for i in range(M)]

#listの初期化
dp = list()
dp = [0]*N
dp[0] = 1
dp[1] = 2
if 1 in hoal:
    dp[0] = 0
    dp[1] = 1
dp_check = list()
dp_check = [0]*N

for i in range(M):
    dp_check[hoal[i]-1]=1

if M>=2:
    for i in range(N):
        if dp_check[i]==1 and dp_check[i+1]==1:
            print("0")
            exit()

#実際に求める
#dp[i]<-i段までの移動方法の和
for i in range(N):
    if i>1 and dp_check[i]==0:
        dp[i]=dp[i-1]+dp[i-2]

    dp[i]=dp[i]%1000000007

print(dp[N-1])
    

### FIXED CODE ###

"""
問題文
    N段の階段があります。高橋君は現在、上り口(0段目)にいます。 高橋君は一歩で1段か2段上ることができます。
    ただし、a1,a2,a3,....aM段目の床は壊れており、その段に足を踏み入れることは危険です。
    壊れている床を踏まないようにしながら、最上段(N段目)にたどりつくまでの移動方法は何通りあるでしょうか？ 
    総数を1,000,000,007で割った余りを求めてください。

制約
    1≦N≦10^5
    0≦M≦N−1
    1≦a1<a2< ... <aM≦N−1
"""
#入力の受け取り
N, M = map(int, input().split())
hoal = [int(input()) for i in range(M)]

#listの初期化
dp = list()
dp = [0]*N

    
dp_check = list()
dp_check = [0]*N

for i in range(M):
    dp_check[hoal[i]-1]=1
    if dp_check[hoal[i]-1]==1 and dp_check[hoal[i]-2]==1:
        print("0")
        exit()

if N==1:
    if M==1:
        dp[0]=0
    else:
        dp[0]=1
    print(dp[N-1])
else:
    if dp_check[0]==1:
        dp[0]=0
        dp[1]=1
    elif dp_check[1]==1:
        dp[0]=1
        dp[1]=0
    else:
        dp[0] = 1
        dp[1] = 2
    #実際に求める
    #dp[i]<-i段までの移動方法の和
    for i in range(N):
        if i>1 and dp_check[i]==0:
            dp[i]=dp[i-1]+dp[i-2]

        dp[i]=dp[i]%1000000007

    print(dp[N-1])
def multiple_factors(i, N):
  factors = []
  factors.append(int(1*i))
  for index in range(1, N//i):
    factors.append(int(index*i))
  return factors
  
def assign_ball(factors, blist, ai, b_add_index):
  sum_b = 0
  for index in range(len(factors)-1, 0, -1):
    sum_b += blist[factors[index]-1]
  
  if ai != sum_b%2:
    blist[factors[0]-1] =1
    b_add_index.append(str(factors[index]))
  return blist, b_add_index

      
N = int(input())
alist = list(map(int, input().split()))
blist = [0 for i in range(N)]
M = 0
b_add_index = []
for i in range(N-1,-1, -1):
  factors = multiple_factors(i+1, N)
  blist, b_add_index = assign_ball(factors, blist, alist[i], b_add_index)

if sum(blist)%2 != alist[0]:
  print(-1)
else:
  print(len(b_add_index))
  print(" ".join(b_add_index))

### FIXED CODE ###

def multiple_factors(i, N):
  factors = []
  for index in range(0, N//i):
    factors.append(int((index+1)*i))
  return factors
  
def assign_ball(factors, blist, ai, b_add_index):
  sum_b = 0
  for index in range(len(factors)-1, 0, -1):
    sum_b += blist[factors[index]-1]
  
  if ai != sum_b%2:
    blist[factors[0]-1] = 1
    b_add_index.append(str(factors[0]))
  return blist, b_add_index

      
N = int(input())
alist = list(map(int, input().split()))
blist = [0 for i in range(N)]
b_add_index = []
for i in range(N-1,-1, -1):
  factors = multiple_factors(i+1, N)
  blist, b_add_index = assign_ball(factors, blist, alist[i], b_add_index)

if sum(blist)%2 != alist[0]:
  print(-1)
else:
  print(len(b_add_index))
  print(" ".join(b_add_index))
def solve(N, M, A):

    Unsolve = [0]*(N+1)
    Solved = []
    ac = 0
    wa = 0
    for i in range(M):
        if A[i][0] not in Solved:
            if A[i][1] == 'WA':
                Unsolve[A[i][0]] += 1
            elif A[i][1] == 'AC':
                Solved.append(A[i][0])
                wa += Unsolve[A[i][0]]
                ac += 1
    return ac, wa

def main():
    N, M = map(int, input().split())
    A = [list(x for x in input().split()) for _ in range(M)]
    ans = solve(N, M, A)
    print(*ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###


def solve(N, M, A):

    A = [[int(x[0]), x[1]] for x in A]

    Unsolve = [0]*(N+1)
    Solved = set()
    ac = 0
    wa = 0
    for i in range(M):
        if A[i][0] not in Solved:
            if A[i][1] == 'WA':
                Unsolve[A[i][0]] += 1
            elif A[i][1] == 'AC':
                Solved.add(A[i][0])
                wa += Unsolve[A[i][0]]
                ac += 1

    return ac, wa

def main():
    N, M = map(int, input().split())
    A = [list(x for x in input().split()) for _ in range(M)]
    ans = solve(N, M, A)
    print(*ans)

if __name__ == "__main__":
    main()
from functools import reduce
import sys
sys.setrecursionlimit(100000)
mod = 10**9+7
def mul(a,b):
    return a*b % mod
def cmb(n,r,mod):
    above = reduce(mul,range(n-r+1,n+1)) 
    below = reduce(mul,range(1,r+1)) 
    ans = above*pow(below,mod-2,mod) % mod
    if ans < 0:
        ans += mod
    return ans
x,y = map(int,input().split())
if 2*x-y < 0 or 2*y-x<0 or (2*x-y) % 3 != 0:
    print(0)
    exit()
a = (2*x-y)//3
b = (2*y-x)//3
print(cmb(a+b,min(a,b),mod))


### FIXED CODE ###

from functools import reduce
mod = 10**9+7
def mul(a,b):
    return a*b % mod
def cmb(n,r,mod):
    if r == 0:
        return 1
    if r == 1:
        return n % mod 
    above = reduce(mul,range(n-r+1,n+1)) 
    below = reduce(mul,range(1,r+1)) 
    ans = above*pow(below,mod-2,mod) % mod
    if ans < 0:
        ans += mod
    return ans
x,y = map(int,input().split())
if 2*x-y < 0 or 2*y-x<0 or (2*x-y) % 3 != 0:
    print(0)
    exit()
a = (2*x-y)//3
b = (2*y-x)//3
print(cmb(a+b,min(a,b),mod))
n=int(input())
h=[int(i) for i in input().split()]
c=0
l=[]
for i in range(n-1):  
    if h[i]>h[i+1]:
        c+=1
    else:
        c=0
    l.append(c)
print(max(l))

### FIXED CODE ###

N=int(input())
H=list(map(int,input().split()))
ans=0
tmp=0
for i in range(N-1):
    if H[i]>=H[i+1]:
        tmp+=1
    else:
        tmp=0
    ans=max(ans,tmp)
print(ans)
import math
n=int(input())
a=list(map(int,input().split()))
m=math.ceil(math.log(max(a),2))

ans=0
for i in range(m):
  sum0=0
  sum1=0
  for j in range(n):
    if (a[j]>>i)&1:
      sum1+=1
    else:
      sum0+=1
  ans+=2**i*sum1*sum0
print(ans%(10**9+7))

### FIXED CODE ###

import math
n=int(input())
al=list(map(int,input().split()))
a=['{:060b}'.format(x) for x in al]
a=''.join(a)

m=math.ceil(math.log(max(1,max(al)),2))

ans=0
for i in range(m):
  sum1=a[59-i::60].count('1')
  sum0=n-sum1
  ans+=2**i*sum1*sum0

print(ans%(10**9+7))
s = input()
k = int(input())

index = []

for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        jyuhuku = 0
        for s in index:
            if(s == s[i:j]):
                jyuhuku = 1
        if(jyuhuku == 0):
            index.append(s[i:j])
    

print(sorted(index)[k-1])


### FIXED CODE ###

s = input()
k = int(input())

index = []

for i in range(len(s)):
    for j in range(i+1,i+k+1):
        index.append(s[i:j])
    
print(sorted(list(set(index)))[k-1])
s=input()
num=0
for i in s:
  if i=="-":
    num-=1
  else
    num+=1
print(num)

### FIXED CODE ###

s=input()
num=0
for i in s:
  if i=="-":
    num-=1
  else:
    num+=1
print(num)
N=int(input())
l=list(range(1,N))
s=[0]*N
for i in range(N-1):
    s[i+1]=s[i]+l[i]
print(s[N-1])

### FIXED CODE ###

N=int(input())
print(N*(N-1)//2)
a,b = map(int,input().split())

cnt = 0

if b<=a:
    cnt = 1
hole = a
else:
    for i in range(15):
            hole +=(a-1)
            
            if b<=hole:
                cnt = i+2
                break
            else:
                pass
print(cnt)


### FIXED CODE ###

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

cnt = 0
hole = a
if b==1:
    cnt = 0

elif b!=1 and a>=b:
    cnt = 1
else:
    for i in range(pow(2,10)):
            hole +=(a-1)
            
            if b<=hole:
                cnt = i+2
                break
            else:
                pass
print(cnt)
import sys
import math
import heapq
mod=10**9+7
inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict, OrderedDict
#すべてのkeyが用意されてる defaultdict(int)で初期化
#順序を保ったdict
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########
n=int(input())
s=input()
A=[int(i)-1 for i in s]

RUI2=[pow(2,i) for i in range(21)]
import bisect as bi
@lru_cache(maxsize=10**10)
def tan(n):
    if n==0:return [1]
    if n==1:return [1,1]
    if n==2:return [1,0,1]
    i= bi.bisect_right(RUI2,n)-1
    return tan(n-pow(2,i)) + [0]*(pow(2,i+1)-n-1) + tan(n-pow(2,i))


for i in range(n-1):
    if A[i]==A[i+1]: A[i]=0
    elif (A[i]^A[i+1])&1 : A[i]=1
    else:A[i]=2
for i in range(n-2):
    if (A[i]^A[i+1])&1 : A[i]=1
    else:A[i]=0

# print(A[:-2],tan(n-3))
a=[i&j for i,j in zip(A[:-2],tan(n-3))]
# print(a)
print(1 if sum(a)%2==1 else 0)


### FIXED CODE ###

import sys
import math
import heapq
mod=10**9+7
inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict, OrderedDict
#すべてのkeyが用意されてる defaultdict(int)で初期化
#順序を保ったdict
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########
n=int(input())
s=input()
A=[int(i)-1 for i in s]

RUI2=[pow(2,i) for i in range(21)]
import bisect as bi
@lru_cache(maxsize=10**10)
def tan(n):
    if n==0:return [1]
    if n==1:return [1,1]
    if n==2:return [1,0,1]
    i= bi.bisect_right(RUI2,n)-1
    return tan(n-pow(2,i)) + [0]*(pow(2,i+1)-n-1) + tan(n-pow(2,i))


# for i in range(n-1):
#     if A[i]==A[i+1]: A[i]=0
#     elif (A[i]^A[i+1])&1 : A[i]=1
#     else:A[i]=2
# for i in range(n-2):
#     if (A[i]^A[i+1])&1 : A[i]=1
#     else:A[i]=0

# print(A[:-2],tan(n-3))
if A.count(1)!=0:
    A=[i%2!=1 for i in A]
    a=[i&j for i,j in zip(A,tan(n-1))]
    # print(a)
    print(1 if sum(a)%2==1 else 0)
else:
    a=[(i//2)&j for i,j in zip(A,tan(n-1))]
    #print(a,tan(4))
    print(2 if sum(a)%2==1 else 0)
a,b = int(input().split())
c = a * b
if c % 2 == 1:
  print('Odd')
else:
  print('Even')

### FIXED CODE ###

a,b = map(int,input().split())
c = a * b
if c % 2 == 1:
  print('Odd')
else:
  print('Even')
N,K=map(int,input().split())
A=list(map(int,input().split()))
visited=[0 for i in range(N)]
visited[0]=1
tour=[1]

for i in range(1,100):
    goto=A[tour[-1]-1]
    tour.append(goto)
    if visited[goto-1]:
        break
    visited[goto-1]=1

#print(tour)
    
before=0
shuuki=0
saigo=tour[-1]
for i in range(len(tour)):
    if tour[i]==tour[-1]:
        before=i
        shuuki=len(tour)-i-1
        break

#print(before,shuuki)

if K<=before:
    print(tour[K-1])
else:
    K-=before
    K%=shuuki
    print(tour[before+K])

### FIXED CODE ###

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

while True:
    goto=A[tour[-1]-1]
    tour.append(goto)
    if visited[goto-1]:
        break
    visited[goto-1]=1

#print(tour)
    
before=0
shuuki=1
for i in range(len(tour)):
    if tour[i]==tour[-1]:
        before=i
        shuuki=len(tour)-i-1
        break

#print(before,shuuki)

if K<len(tour):
    print(tour[K])
else:
    K-=before
    K%=shuuki
    print(tour[before+K])
a,b=map(int,input().split())
if a>9 or b>9:
  pritn(-1)
else:
  print(a*b)

### FIXED CODE ###

a,b=map(int,input().split())
if a>9 or b>9:
  print(-1)
else:
  print(a*b)
input_line = int(input().split())

result = input_line[0]*input_line[1]
 
if result % 2 == 0:
  print('Even')
else:
  print('Odd')

### FIXED CODE ###

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

result = x*y
 
if result % 2 == 0:
  print('Even')
else:
  print('Odd')
n = int(input())
a = list(map(int, input().split()))
print(*a[::-2], *a[n%2::2])

### FIXED CODE ###

n = int(input())
a = input().split()
print(' '.join(a[::-2] + a[n%2::2]))
N, K = map(int, input().split())

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

if N == 1 or K == 1:
    print(x[0])
    exit()

suml = []
for left in x:
    if x.index(left) == len(x)-K+1:
        break
    tmp = abs(left) + abs(left - x[x.index(left)+K-1])
    suml.append(tmp)
ansl = min(suml)

sumr = []
for right in reversed(x):
    if x.index(right) == K-1:
        break
    tmp = abs(right) + abs(right - x[x.index(right)-K])
    sumr.append(tmp)
ansr = min(sumr)

print(min(ansl,ansr))


### FIXED CODE ###

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

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

if N == 1 or K == 1:
    print(x[0])
    exit()

ans = 10**100
for i in range(N):
    if i < N-K+1:
        tmp1 = abs(x[i]) + abs(x[i] - x[i+K-1])
        tmp2 = abs(x[i+K-1]) + abs(x[i] - x[i+K-1])
        ans = min(tmp1, tmp2, ans)

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

MOD = 10**9 + 7

dig = 60
Abn = [[0]*dig for _ in range(n)]

for i in range(n):
  for j in range(dig):
    if A[i]>>j&1:
      Abn[i][j] = 1
      

Cbn = [[[0]*2 for j in range(dig)] for i in range(n)]

for j in range(dig):
  if Abn[n-1][j]==1:
    Cbn[n-1][j][1] = 1
  else:
    Cbn[n-1][j][0] = 1
    
for i in range(n-2, 0, -1):
  for j in range(dig):
    if Abn[i][j]==1:
      Cbn[i][j][0] = Cbn[i+1][j][0]
      Cbn[i][j][1] = Cbn[i+1][j][1] + 1
    else:
      Cbn[i][j][0] = Cbn[i+1][j][0] + 1
      Cbn[i][j][1] = Cbn[i+1][j][1]

ans = 0
two = [pow(2, i, MOD) for i in range(dig)]

for i in range(n-1):
  for j in range(dig):
    if Abn[i][j] == 1:
      ans += Cbn[i+1][j][0] * two[i] % MOD
    else:
      ans += Cbn[i+1][j][1] * two[i] % MOD
    ans %= MOD
    
print(ans)

### FIXED CODE ###

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

MOD = 10**9 + 7

dig = 60
Abn = [[0]*dig for _ in range(n)]

b0 = [0]*dig
b1 = [0]*dig
for i in range(n):
  for j in range(dig):
    if A[i]>>j&1:
      b1[j] += 1
    else:
      b0[j] += 1
      
pow2 = [pow(2, i, MOD) for i in range(dig)]

ans = 0
for i in range(dig):
  ans += b1[i]*b0[i]*pow2[i]%MOD
  
print(ans%MOD)
s = int(input())
n = len(s)
a = s[n-1:n]
if a == 2 or a == 4 or a == 5 or a == 7 or a == 9:
  print('hon')
elif a == 0 or a == 1 or a == 6 or a == 8:
  print('pon')
else:
  print('bon')


### FIXED CODE ###

s = input()
n = len(s)
a = s[n-1:n]
if a == '2' or a == '4' or a == '5' or a == '7' or a == '9':
  print('hon')
elif a == '0' or a == '1' or a == '6' or a == '8':
  print('pon')
else:
  print('bon')
def makePath(x):
    for y in G[x]:
        if hist[y]==0:
            A.append(y)
            hist[y] = 1
            makePath(y)
            return
N,M = map(int,input().split())
G = {}
for _ in range(M):
    a,b = map(int,input().split())
    if a not in G:
        G[a] = []
    G[a].append(b)
    if b not in G:
        G[b] = []
    G[b].append(a)
A = [1]
hist = [0 for _ in range(N+1)]
hist[1] = 1
makePath(1)
B = A[:]
A = []
makePath(1)
A = A[::-1]
A += B
print(len(A))
print(*A)

### FIXED CODE ###

from collections import deque
def makePath():
    que = deque([1])
    while que:
        x = que.pop()
        for y in G[x]:
            if hist[y]==0:
                que.append(y)
                A.append(y)
                hist[y]=1
                break
N,M = map(int,input().split())
G = {i:[] for i in range(1,N+1)}
for _ in range(M):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
A = [1]
hist = [0 for _ in range(N+1)]
hist[1] = 1
makePath()
B = A[:]
A = []
makePath()
A = A[::-1]
A += B
print(len(A))
print(*A)
"N,M".split(",")
N=N-M
if =0<N:
   print(N)
else:
   print(-1)

### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
B=0
for i in A:
    B=B+i       
N=N-B
if 0<=N:
    print(N)
else:
    print(-1)
n,a,b=input(),input(),input()
k=n//(a+b)*a+min(a,n%(a+b))
  
print(k)

### FIXED CODE ###

n,a,b=map(int, input().split())
k=n//(a+b)*a+min(a,n%(a+b))
  
print(k)