Harryxun/stf_run huggingface dataset:
from collections import deque
import sys
N, M = list(map(int, input().split))
ans = [[] * N]
visited = [[False] * N]
dq = deque([0])
path = [[0] * N]
for s in sys.stdin.readlines():
    a, b = map(lambda: int(x)-1, s.split())
    path[a].add(b)
    path[b].add(a)
while dq:
   i = dq.popleft()
   if visited[i]:
      continue
   visited[i] = True
   next_rs = path[i]
   for i in range(len(next_rs)):
      dq.append(next_rs[i])
      ans[next_rs[i]] = i
if sum(visited) == N:
   print('Yes')
   for i in range(N):
      if i == 0:
         continue
      else:
         print(ans[i])
else:
   print(-1)

### FIXED CODE ###

from collections import deque
import sys
N, M = list(map(int, input().split()))
ans = [-1] * N
visited = [False] * N
dq = deque([0])
path = [[] for _ in range(N)]
for s in sys.stdin.readlines():
    a, b = list(map(lambda x: int(x)-1, s.split()))
    path[a].append(b)
    path[b].append(a)
while dq:
   i = dq.popleft()
   if visited[i]:
      continue
   visited[i] = True
   next_rs = path[i]
   for j in range(len(next_rs)):
      if not visited[next_rs[j]]:
         dq.append(next_rs[j])
         if ans[next_rs[j]] == -1:
            ans[next_rs[j]] = i
if sum(visited) == N:
   print('Yes')
   for i in range(N):
      if i == 0:
         continue
      else:
         print(ans[i]+1)
else:
   print(-1)
N=int(input())
sum=0

for i in range(1,N+1):
  if i%3==0 or i%5==0:
    pass
  else:
    ans += i
    
print(ans)

### FIXED CODE ###

N=int(input())
ans=0

for i in range(1,N+1):
  if i%3==0 or i%5==0:
    pass
  else:
    ans += i
    
print(ans)
N, K = map(int, input().split())
dist = [[] for i in range(N)]
A = list(map(int, input().split()))
for i in range(N):
    dist[i].append(A[i] - 1)
dp = [[0, 0] for i in range(N)]

def dfs(pos, depth):
    # 初めての訪問なら
    if dp[pos][0] == 0:
        dp[pos][0] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
    elif dp[pos][1] == 0:
        dp[pos][1] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
dfs(0, 0)
for i in range(N):
    if dp[i][0] != 0:
        if dp[i][0] == K or dp[i][1] == K:
            print(i + 1)
            break
        else:
            split = dp[i][1] - dp[i][0]
            if split > 0:
                if (K - dp[i][0]) % split == 0:
                    print(i + 1)
                    break

### FIXED CODE ###

def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left

import sys
# sys.setrecursionlimit(1000000)
# list(map(int, input().split()))
n, k = getList()
# dist
nums = [0] + getList()
cnt = 1
if k == 1:
    print(1)
    exit()
cur = 1
visited = [-1 for i in range(n + 1)]
visited[1] = 1
rooped = False
while True:
    # cur 行き先
    cur = nums[cur]
    cnt += 1

    # １回目に訪れていたら
    if visited[cur] != -1:
        # ループの周期
        roop = cnt - visited[cur]
        # 途中を飛ばす
        k -= ((k-cnt) // roop) * roop
        rooped = True
    if not rooped:
        # １回目に訪れたのであれば
        visited[cur] = cnt

    if cnt == k:
        print(nums[cur])
        break
import math
MOD = 10**9+7
x, y = map(int, input().split())
if (x+y)%3 != 0 or 3*x < x+y or 3*y < x+y:
    print(0)
    exit()
n = (x+y)//3
x -= n
y -= n
c = math.factorial(x) // (math.factorial(x - y) * math.factorial(y))
print(c%MOD)


### FIXED CODE ###

def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

MOD = 10**9+7
x, y = map(int, input().split())
if (x+y)%3 != 0 or 3*x < x+y or 3*y < x+y:
    print(0)
    exit()
n = (x+y)//3
x -= n
y -= n
fact = [1, 1]
for i in range(2,n+1):
    fact.append(fact[i-1] * i % MOD)
factX = fact[x]
factY = fact[y]
factN = fact[n]
# print(x,y,n)
# print(factX,factY,factN)

c = factN * modinv(factX, MOD) * modinv(factY, MOD)
c %= MOD
print(c)
import math
#数値ひとつ
#I = int(input())
#数値欲しいとき
#H, W = map(int, input().split())
#文字欲しいとき
A = input()
B = input()
C = input()

bool = False

if A == '7':
    bool = True

elif B == '7':
    bool = True

elif C == '7':
    bool = True


if bool == True:
    print('Yes')
else:
    print('No')


### FIXED CODE ###

import math
#数値ひとつ
#I = int(input())
#数値欲しいとき
#H, W = map(int, input().split())
#文字欲しいとき

N = input()
listA = list(N)

A = listA[0]
B = listA[1]
C = listA[2]

bool = False

if A == '7':
    bool = True

elif B == '7':
    bool = True

elif C == '7':
    bool = True


if bool == True:
    print('Yes')
else:
    print('No')
import numpy as np
h, w, d = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(h)])
q = int(input())
num = h * w
mass = np.array([[0, 0] for _ in range(num + 1)])
mp = []
for i in range(h):
    for j in range(w):
        a_ij = a[i][j]
        mass[a_ij] = [i, j]
for i in range(d):
    mass_sort = mass[1+i::d]
    mp_i = np.sum(abs(mass_sort[:-1] - mass_sort[1:]), axis=1)
    mp_i = np.append(np.zeros(1, dtype=int), mp_i.cumsum())
    mp.append(mp_i)
for _ in range(q):
    l, r = map(int, input().split())
    n = (r - l) // d
    u = l % d
    index = (l - (d - u)) // 2
    print(mp[-(u+1)][index+n]-mp[-(u+1)][index])


### FIXED CODE ###

import sys
input = sys.stdin.readline
h, w, d = map(int, input().split())
num = h * w
x = [0] * (num + 1)
y = [0] * (num + 1)
mp = [0] * (num + 1)
for i in range(h):
    for j, a in enumerate(map(int, input().split())):
        x[a], y[a] = [i+1, j+1]
for i in range(d+1, num+1):
    mp[i] = mp[i-d] + abs(x[i] - x[i-d]) + abs(y[i] - y[i-d])
q = int(input())
for _ in range(q):
    l, r = map(int, input().split())
    print(mp[r]-mp[l])
a, b, c = map(int,input().split())

aj = a%2
bj = b%2
cj = c%2

if aj == bj == cj == 1 or aj == bj == cj ==0 or (aj == bj ==1 and cj ==0 ) or (aj == cj == 1 and bj == 0) or (aj == 0 and bj==cj==1):
  print("YES")
else:
  p

### FIXED CODE ###

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

for i in range(a,b*a+1,a):
  if i % b == c:
    print("YES")
    exit(0)
print("NO")
myinput_H = input()
mylist = list(map(int, myinput_H.split(" ")))
A = mylist[0]
B = mylist[1]
C = mylist[2]
 
if (A == B & B != C):
  print(Yes)
elif (A == C & B != C):
  print(Yes)
elif (B == C & A != B):
  print(Yes)
else:
  print(No)

### FIXED CODE ###

a = input()
mylist = list(map(int, a.split(" ")))
A = mylist[0]
B = mylist[1]
C = mylist[2]
 
if (A == B and B != C):
  print("Yes")
elif (A == C and B != C):
  print("Yes")
elif (B == C and A != B):
  print("Yes")
else:
  print("No")
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
pre = 0
e1, e2 = 0, 0
for a in A:
  if pre == a:
    if e1 == 0:
      e1 = a
    elif e2 == 0:
      e2 == a
  else:
    pre = a
print(a * b)

### FIXED CODE ###

N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
pre = 0
e1, e2 = 0, 0
for a in A:
  if pre == a:
    if e1 == 0:
      e1 = a
    elif e2 == 0:
      e2 = a
    pre = 0
  else:
    pre = a
print(e1 * e2)
from collections import Counter


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n, k = map(int, input().split())
aaa = list(map(int, input().split()))
MOD = 10 ** 9 + 7
bit = Bit(n + 1)
single = 0
for a in reversed(aaa):
    single += bit.sum(a)
    bit.add(a + 1, 1)
ans = single * k % MOD
coef = k * (k - 1) // 2
cnt = Counter(aaa)
before = 0
for a, c in sorted(cnt.items()):
    ans = (ans + c * before * coef) % MOD
    before += c
print(ans)


### FIXED CODE ###

from collections import Counter


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n, k = map(int, input().split())
aaa = list(map(int, input().split()))
MOD = 10 ** 9 + 7
bit = Bit(2001)
single = 0
for a in reversed(aaa):
    single += bit.sum(a)
    bit.add(a + 1, 1)
ans = single * k % MOD
coef = k * (k - 1) // 2 % MOD
cnt = Counter(aaa)
before = 0
for a, c in sorted(cnt.items()):
    ans = (ans + c * before * coef) % MOD
    before += c
print(ans)
N,M = map(int,input().split())
a_g=[];b_g=[]
for i in range(N):
  a_g.append(list(input()))
for i in range(M):
  b_g.append(list(input()))

for i in range(N-M):
  for j in range(N-M):
    flag=1
    for k in range(M):
      for l in range(M):
        if a_g[k+i][l+j] != b_g[k][l]:
          flag=0
          l=M;k=M
    if flag:
      print("Yes");exit()
print("No")

### FIXED CODE ###

N,M = map(int,input().split())
a_g=[];b_g=[]
for i in range(N):
  a_g.append(list(input()))
for i in range(M):
  b_g.append(list(input()))

for i in range(N-M+1):
  for j in range(N-M+1):
    flag=1
    for k in range(M):
      for l in range(M):
        if a_g[k+i][l+j] != b_g[k][l]:
          flag=0
          break
    if flag:
      print("Yes");exit()
print("No")
a, b, c, d = map(int, input().split(' '))
l = a + b
r = c + d

if l > r:
  print('Left')
elsif l = r:
  print('Balanced')
else:
  print('Right')

### FIXED CODE ###

a, b, c, d = map(int, input().split(' '))
l = a + b
r = c + d

if l > r:
  print('Left')
elif l == r:
  print('Balanced')
else:
  print('Right')
N = int(input())
T = [int(input()) for i in range(N)]

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

def lcm(a, b):
    return a * b // gcd(a, b)

now = lcm(T[0], T[1])
for i in range(1, N):
    now = lcm(now, T[i])

print(now)


### FIXED CODE ###

N = int(input())
T = [int(input()) for i in range(N)]


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


def lcm(a, b):
    return a * b // gcd(a, b)


if N == 1:
    print(T[0])
    exit()

now = lcm(T[0], T[1])
for i in range(1, N):
    now = lcm(now, T[i])

print(now)
import sys
sys.setrecursionlimit(10**5)

def main():
    N, K = map(int, input().split())
    h = [int(x) for x in input().split()]
    memo = [None for i in range(N)]

    def rec(n):
        if memo[n-1] == None:
            if n <= 1:
                memo[n-1] = 0
            elif n == 2:
                memo[n-1] = abs(h[0]-h[1])
            else:
                memo[n-1] = min(abs(h[n-1]-h[j-1]) + rec(j) for j in range(max(0,n-K),n))
        return memo[n-1]

    print(rec(N))

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    h = [int(x) for x in input().split()]
    # memo = [None for i in range(N)]

    # def rec(n):
    #     if memo[n-1] == None:
    #         if n <= 1:
    #             memo[n-1] = 0
    #         elif n == 2:
    #             memo[n-1] = abs(h[0]-h[1])
    #         else:
    #             memo[n-1] = min(abs(h[n-1]-h[j-1]) + rec(j) for j in range(max(0,n-K),n))
    #     return memo[n-1]

    # print(rec(N))

    dp = [-1]*N
    dp[0] = 0
    dp[1] = abs(h[1]-h[0])

    for i in range(2,N):
        dp[i] = min(abs(h[i]-h[j]) + dp[j] for j in range(max(0,i-K),i))
    print(dp[N-1])

# if __name__ == "__main__":
main()
import math
N,X = map(int,input().split())
x = list(map(int,input().split()))

x.append(X)
x.sort()
ans = x[1]-x[0]
for i in range(1,N):
    ans = math.gcd(ans,x[i+1]-x[i])
print(ans)

### FIXED CODE ###

N = int(input())
W = list(input() for _ in range(N))
for i in range(1,N):
    n = len(W[i-1])
    for j in range(0,i):
        if W[i] == W[j]:
            print("No")
            exit()
    if W[i-1][n-1] != W[i][0]:
        print("No")
        exit()
    else:
        continue
print("Yes")
from fractions import gcd

n,k=map(int,input().split())

if n == 1 or k == 1:
    print(0)
else:2 6
    print(gcd(n,k))

### FIXED CODE ###

n,k=map(int,input().split())
n=int(n%k)
result=abs(k-n)

if n==0:
    print(0)
else:
    while result > abs(result-k):
        result=abs(result-k)
    print(result)
a, b = map(int,input().split())
big = max(a, b)
small = min(a, b)
for i in range(1, 100000):
  if big*i % small == 0:
    print(big*i)
    exit()


### FIXED CODE ###

a = int(input())
b = int(input())
print(6-a-b)
n = int(input())

a = [int(input()) for i in range(n)]

a_max = max(a)
a_max_count = a.count(a_max)

if a_max_count > 1:
  for i in range(n):
    print(a_max)
    
else:
  for i in range(n):
    if a[i] == a_max:
      print(max(max(a[:i]), max(a[i+1:])))
      
    else:
      print(a_max)


### FIXED CODE ###

n = int(input())

a = [int(input()) for i in range(n)]

a_max = max(a)
a_max_count = a.count(a_max)

if a_max_count > 1:
  for i in range(n):
    print(a_max)
    
else:
  print(max(a[1:]))
  
  for i in range(1, n-1):
    if a[i] == a_max:
      print(max(max(a[:i]), max(a[i+1:])))
      
    else:
      print(a_max)

  print(max(a[:-1]))
import math
a, b = map(int, input().split())
print( (a*b)//math.gcd(a, b) )


### FIXED CODE ###

import fractions
a, b = map(int, input().split())
print( (a*b)//fractions.gcd(a, b) )
def coins(a,b,c,x):
    num=0
    for i in range(a):
        for w in range(b):
            for q in range(c):
                if 500*i + 100*w + 50*q == x:
                    num += 1
    return num
  
def main():
  a=input()
  b=input()
  c=input()
  x=input()
  print(coins(a,b,c,x))
 
if __name__ == '__main__':
    main()

### FIXED CODE ###

def coins(a,b,c,x):
    num=0
    for i in range(a+1):
        for w in range(b+1):
            for q in range(c+1):
                if 500*i + 100*w + 50*q == x:
                    num += 1
    return num
  
def main():
  a=int(input())
  b=int(input())
  c=int(input())
  x=int(input())
  print(coins(a,b,c,x))
 
if __name__ == '__main__':
    main()
import math


N, K = map(int, input().split())
MOD = 10 ** 9 + 7

def comb(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)

for i in range(1, K + 1):
    if i > N-K+1:
        print(0)
    blue = comb(N-K+1, i) % MOD
    red = comb(K-1, i-1) % MOD

    ans = blue * red % MOD
    print(ans)


### FIXED CODE ###

import math


N, K = map(int, input().split())
MOD = 10 ** 9 + 7

def comb(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)

for i in range(1, K + 1):
    if i > N-K+1:
        print(0)
        continue
    blue = comb(N-K+1, i) % MOD
    red = comb(K-1, i-1) % MOD

    ans = blue * red % MOD
    print(ans)
while(True):
    line = input();

    if(line < 1000):
        print('ABC');
    else:
        print('ABD');


### FIXED CODE ###

line = input();

if(line < 1000):
    print('ABC');
else:
    print('ABD');
N, K = map(int, input().split())
Hs = list(map(int, input().split()))
Hs.sort(reverse=True)
if K == 0:
  print(sum(Hs))
else:
  for i in range(K):
    Hs[i] = 0
  print(sum(Hs))

### FIXED CODE ###

N, K = map(int, input().split())
Hs = list(map(int, input().split()))
Hs.sort(reverse=True)
if K > N:
  print(0)
else:
  for i in range(K):
    Hs[i] = 0
  print(sum(Hs))
def search(v, A, B, flg, p, c, s):
  if flg[v-1] == 1:
    return c
#  print("v=", v, "flg=", flg, "p=", p, "cnt=", c)
  flg[v-1] = 1
  for idx in A:
    if v == A[idx]:
      c += 1
      if search(B[idx], A, B, flg, p, c, s) == 0:
        return 0
  for idx in B:
    if v == B[idx]:
      c += 1
      if search(A[idx], A, B, flg, p, c, s) == 0:
        return 0
#  for idx in A:
#    if v==A[idx] and flg[B[idx]-1] == 0:
#      return 0
#  for idx in B:
#    if v==B[idx] and flg[A[idx]-1] == 0:
#      return 0
  p.append(v)
  return c+1

str = input()
NM = list(map(int,str.split()))
print(NM)
ANS = 1
AB = []
C = []
A = []
B = []
for i in range(0,int(NM[1])):
  str = input()
  AB.append(list(map(int,str.split())))
  A.append(AB[i][0])
  B.append(AB[i][1])
#print(AB)
e = 0  # end vertex
#
# Start path finding
#
for i in range(0,int(NM[1])):  # Choose start vertex
  flg = [] # Clear the flag list
  for j in range(0,int(NM[0])):
    flg.append(0)
  c = AB[i][0]  # current vertex
  s = c   # start vertex
  p = []
#  print("A=", A, "B=", B)
  cnt = search(c, A, B, flg, p, 0, s)
  if cnt != 0:
#    print(p)
    print(cnt)
    for i in p:
      print(i)
    break

### FIXED CODE ###

N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
  len = len + 1
  if state in {0}:	# Initial state
      prev = A[i]
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
    elif A[i] < prev:
      state = 3
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 1
      ANS = ANS + 1
      len = 0
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 1
      ANS = ANS + 1
      len = 0
  prev = A[i]
#  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
print(ANS)
from heapq import*
N,M=map(int,input().split())
t=[list(map(int,input().split())) for _ in range(N)]
q=[0]*M
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v:
    for j in i:heappush(q,-j)
    z-=heappop(q)
print(z)


### FIXED CODE ###

from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*10**5
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v[:M]:
	for j in i:heappush(q,-j)
	z-=heappop(q)
print(z)
n=input()
if n[0]>n[1] or (n[0]=n[1] and n[1]>=n[2]):
    print(int(n[0])*111)
else:
    print(int(n[0])*111+111)

### FIXED CODE ###

n=input()
if n[0]>n[1] or (n[0]==n[1] and n[1]>=n[2]):
    print(int(n[0])*111)
else:
    print(int(n[0])*111+111)
n, k = map(int,input().split())
l = list(input())
a = sorted(l)[::1]
print(sum(a)[-k:])

### FIXED CODE ###

a, b = map(int,input().split())
if a % 3 == 0 or b % 3 == 0 or (a+b) % 3 == 0:
  print("Possible")

else:
  print("Impossible")
N,M=map(int,input().split())
A=[[] for i in range(N)]
for i in range(M):
    Pi,Si=map(str,input().split())
    Pi=int(Pi)
    A[Pi-1].append(Si)

nAC=0
nWA=0
for i in range(N):
    if len(A[i])==0:
        pass
    else:
        j=0 
        while A[i][j]=="WA":
            nWA+=1
            j+=1
            if j==len(A[i])-1:
                break
            else:
                pass
        if A[i][j]=="AC":
            nAC+=1
        else:
            pass
print("{:1}{:2}".format(nAC,nWA))



### FIXED CODE ###

#print("<input>") #
N,M=map(int,input().split())
A=[[] for i in range(N)]
for i in range(M):
    Pi,Si=map(str,input().split())
    Pi=int(Pi)
    A[Pi-1].append(Si)

#print("<test>") #
#print("A=",A) #

nAC=0
nWA=0
inWA=0
j=0
for i in range(N):
    #print("i=",i) #
    if len(A[i])==0:
        pass
    else: 
        while A[i][j]=="WA":
            inWA+=1
            nWA+=1
            #print("j=",j) #
            #print("inWA=",inWA) #
            #print("nWA=",nWA) #
            if j==len(A[i])-1:
                nWA-=inWA
                break
            else:
                pass
            j+=1
        if A[i][j]=="AC":
            nAC+=1
        else:
            pass
        inWA=0
        j=0
        #print("inWA=",inWA) #
        #print("nWA=",nWA) #
        

#print("<ans>") #
print("{0} {1}".format(nAC,nWA))
N,M = map(int, input().split())
ac = [0]*(N+1)
wa = 0
for i in range(M):
    p,s = input().split()
    p = int(p)
    if s == 'AC':
        if ac[p] != -1:
            ac[p] = -1
    else:
        if ac[p] != -1:
            wa +=1
print(ac.count(-1),wa)

### FIXED CODE ###

alp = "abcdefghijklmnopqrstuvwxyz"
C = input()
n = 0
while alp[n] != C:
    n += 1
print(alp[n+1])
a = [list(map(int,input().split())) for i in range(3)]
n = int(input())
b = [int(input()) for i in range(n)]
for number in b:
    for i in range(3):
        if number in a[i]:
            a[i][a[i].index(number)] = 0
if a[0][0] == a[1][1] == a[2][2] == 0　or a[2][0] == a[1][1] == a[0][2]:
    print('Yes')
else:
    for i in range(3):
        if a[i][0] == a[i][1] == a[i][2] == 0:
            print('Yes')
            break
        elif a[0][i] == a[1][i] == a[2][i] == 0:
            print('Yes')
            break
    else:
        print('No')

### FIXED CODE ###

a = [list(map(int,input().split())) for i in range(3)]
n = int(input())
b = [int(input()) for i in range(n)]
for number in b:
    for i in range(3):
        if number in a[i]:
            a[i][a[i].index(number)] = 0
if a[0][0] == a[1][1] == a[2][2] == 0 or a[2][0] == a[1][1] == a[0][2] == 0:
    print('Yes')
else:
    for i in range(3):
        if a[i][0] == a[i][1] == a[i][2] == 0:
            print('Yes')
            break
        elif a[0][i] == a[1][i] == a[2][i] == 0:
            print('Yes')
            break
    else:
        print('No')
n,k = map(int,input().split())
k += 1
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] ==1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be)
if k<= len(be):
  print(be[k])
else:
  w = k-len(be)
  u = w%len(r)-1
  print(r[u]+1)

### FIXED CODE ###

n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be,r)
if k< len(be):
  print(be[k]+1)
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)
s_dash = input()
t = input()


ans = []
for i in range(len(s_dash)-len(t)+1):
  flag = True
  for j in range(len(t)):
    if s_dash[i+j] != "?" and s_dash[i+j] != t[j]:
      flag = false
      break

  if flag:
    #範囲外をaに置き換える
    ans.append(s_dash[:i].replace("?", "a") + t + s_dash[i+len(t):].replace("?", "a"))
    print("".join(ans))
    exit()
        
      
print("UNRESTORABLE")

### FIXED CODE ###

S = input()
T = input()
if len(S) < len(T):
    print("UNRESTORABLE")
    exit()
for i in range(len(S) - len(T), -1, -1):
    for s, t in zip(S[i:i+len(T)], T):
        if s != "?" and s != t:
            break
    else:
        S = list(S)
        S[i:i+len(T)] = list(T)
        S = "".join(S).replace("?", "a")
        print(S)
        exit()
print("UNRESTORABLE")
# -*- coding: utf-8 -*-
import sys, copy, math, itertools
from copy import *
from collections import deque
import numpy as np
from heapq import *
from bisect import *
import scipy as sp
from scipy.optimize import linprog
#from scipy import ceil, ifft
#from scipy.fftpack import fft

A, B, C = list( map(int, input().split() ) )

if A==B: print(C)
if B==C: print(A)
if C==A: print(B)

### FIXED CODE ###

# -*- coding: utf-8 -*-
import numpy as np
from numpy import *
from copy import *

A, B, C = list( map(int, input().split() ) )

if A==B: print(C)
if B==C: print(A)
if C==A: print(B)