Harryxun/stf_run huggingface dataset:
n = int(input())
botan = [int(input()) for i in range(n)]
light = 1 # 1番目のボタンが初め光ってる
count = 1
exi = [1]

if 2 is not in botan:
    print(-1)
    exit()

while True:
    light = botan[light - 1]
    if light in exi:
        print(-1)
        exit()
    exi.append(light)
    
    if 2 in exi:
        print(count)
        exit()
    count += 1

### FIXED CODE ###

n = int(input())
botan = [int(input()) for i in range(n)]
light = 0 # 1番目のボタンが初め光ってる
count = 0

for i in range(n):
    light = botan[light] - 1
    if light == 1:
        break
    
if i == n - 1:
    print(-1)
else:
    print(i + 1)
import math


def sieve_of_erastosthenes(num):
    input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)]
    input_list[0] = input_list[1] = False
    input_list[2] = input_list[3] = input_list[5] = True
    sqrt = math.sqrt(num)

    for serial in range(3, num, 2):
        
        if serial >= sqrt:
            return input_list

        for s in range(serial ** 2, num, serial): 
            input_list[s] = False


if __name__ == '__main__':
    while True:
        try:
            n = int(input())
        except:
            break

        input_list = sieve_of_erastosthenes(n)
        
        print(sum(input_list))

### FIXED CODE ###

a = [True for i in range(1000000)]
i = 3
while i * i < 1000000:
    for j in range(3 * i, 1000000, 2 * i): a[j] = False
    i += 2
 
while True:
    try:
        n = int(input())
    except:
        break
 
    count = int(n >= 2) + sum(a[i] for i in range(3, n + 1, 2))
    print(count)
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix
inf = 10**6


N, M, K = map(int, input().split())
graph = np.zeros((N, N), np.int32)
for _ in range(M):
    A, B = map(int, input().split())
    graph[A-1][B-1] = 1
    graph[B-1][A-1] = 1
for _ in range(K):
    C, D = map(int, input().split())
    graph[C-1][D-1] = inf
    graph[D-1][C-1] = inf


csgraph = csr_matrix(graph)
shortest_paths = shortest_path(csgraph, directed=False, method="BF")


ok = sorted(list(zip(*np.where(graph < inf))))
now = 0
ans = 0
for x, y in ok:
    if x == now:
        if shortest_paths[x][y] > 1:
            ans += 1
    else:
        print(ans, end=" ")
        ans = 0
        now += 1
        while now < x:
            if now != N - 1:
                print(0, end=" ")
            else:
                print(0, end="")
            now += 1
        if shortest_paths[x][y] != 1:
            ans += 1

while now <= N-1:
    if ans != 0:
        print(ans , end=" ")
        continue
    if now != N-1:
        print(0, end=" ")
    else:
        print(0, end="")
    now += 1
print()



### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M, K = map(int, input().split())
uf = UnionFind(N)
not_friend_candidates = [1]*N
for _ in range(M):
    A, B = map(int, input().split())
    uf.union(A-1, B-1)
    not_friend_candidates[A-1] += 1
    not_friend_candidates[B-1] += 1
for _ in range(K):
    C, D = map(int, input().split())
    if uf.same(C-1, D-1):
        not_friend_candidates[C-1] += 1
        not_friend_candidates[D-1] += 1
for hito in range(N):
    print(uf.size(hito) - not_friend_candidates[hito], end=" ")
print()
K = int(input())

even = K // 2
odd = K - enve
print(even * odd)

### FIXED CODE ###

K = int(input())

even = K // 2
odd = K - even
print(even * odd)
n = int(input())
l = [int(x) for x in input().split()]
a = 0
b = 0
for i in range(n):
  if i % 2 == 0:
    a += max(l)
    l = l.remove(max(l))
  elif i % 2 == 1:
    b += max(l)
    l = l.remove(max(l))
print(a-b)

### FIXED CODE ###

n = int(input())
l = [int(x) for x in input().split()]
a = 0
b = 0
for i in range(n):
  if i % 2 == 0:
    a += max(l)
    l.remove(max(l))
  elif i % 2 == 1:
    b += max(l)
    l.remove(max(l))
print(a-b)
N=int(input())
A=list(map(int, input().split()))

if N%2:
  A = A+ [0]
  
print(A)
  
Adict={}
for a in A:
  Adict[a] = Adict.get(a, 0) +1
  
#print(Adict)

# 0,0,2,2,4,4  5   (i//2)*2 + 1- N%2 0,2,4
# 1,1,3,3,5,5  6

#print(range(1-N%2, N, 2))
#print(all([Adict[i]==2 for i in range(1-N%2, N, 2)]))

if all([Adict[i]==2 for i in range(1-N%2, N, 2)]):
  answer = 1
  for _ in range(N//2):
    answer = (answer * 2)%1000000007
  print(answer)
else:
  print(0)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
mod=int(1e9+7)

memo={i:0 for i in range(n)}
for a in A:
  memo[a]=memo.get(a,0)+1

answer=1
if n%2:
  if memo[0]!=1:
    print(0)
    exit()
  for i in range(1, (n+1)//2):
    if memo[2*i]!=2:
      print(0)
      exit()
    else:
      answer = 2*answer %mod
elif not n%2:
  for i in range(n//2):
    if memo[2*i+1]!=2:
      print(0)
      exit()
    else:
      answer = 2*answer %mod
      
print(answer)
a=input()b=700
if a[0]=="o":
    b+=100
if a[1]=="o":
    b+=100
if a[2]=="o":
    b+=100
print(b)

### FIXED CODE ###

a=input()
b=700
if a[0]=="o":
    b+=100
if a[1]=="o":
    b+=100
if a[2]=="o":
    b+=100
print(b)
array1 = [int(input()) for i in range(5)]
array2 = [i for i in array1 if i % 10 == 0]
array3 = [i for i in array1 if i % 10 != 0]
array4 = [i % 10 for i in array3]
idx = array4.index(min(array4))
sum1 = 0
sum2 = sum(array2)
for k, i in enumerate(array3):
  if (k == idx):
    sum1 += i
  else:
    sum1 += i + (10 - i % 10)
print(sum1 + sum2)


### FIXED CODE ###

array1 = [int(input()) for i in range(5)]
array2 = [i for i in array1 if i % 10 == 0]
array3 = [i for i in array1 if i % 10 != 0]
array4 = [i % 10 for i in array3]
sum1 = 0
sum2 = sum(array2)
if (len(array3) != 0):
  idx = array4.index(min(array4))
  for k, i in enumerate(array3):
    if (k == idx):
      sum1 += i
    else:
      sum1 += i + (10 - i % 10)
print(sum1 + sum2)
import sys

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

SS = []

for i in range(N):
  if S[i] == S[K]:
    SS.append(S[i])
  else:
    SS.append("*")
    
print(''.join(SS))

### FIXED CODE ###

import sys

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

SS = []

for i in range(N):
  if S[i] == S[K-1]:
    SS.append(S[i])
  else:
    SS.append("*")
    
print(''.join(SS))
K = int(input())
A, B = map(int,input().split())
n_A = A//K
n_B = B//K
if n_B - b_A >= 1:
  print("Yes"):
else:
  print("No")

### FIXED CODE ###

K = int(input())
A, B = map(int,input().split())
m = B // K
if m*K >= A:
  print("OK")
else:
  print("NG")
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1
count = min(countn, countp)
print(count)

### FIXED CODE ###

a = input()
a = a.upper()
b = a.split()

s = b[0]
s = s[0]
t = b[1]
t = t[0]
r = b[2]
r = r[0]

q = s + t + r
print (q)
a=input().split()
if(a[0]==a[2]):
 print("Yes")
else:
 print("No")

### FIXED CODE ###

a = input()
a.split()

if(a[0] == a[2]):
  print("Yes")
else:
  print("No")
l = [int(input()) for i in range(4)]

for i in range(4):
  for j in range(4):
    if l[i]*l[j]>l[4]:
      print(":(")
      break
    else:
      print("Yay!")

### FIXED CODE ###

l = [int(input()) for i in range(6)]

if l[4]-l[0]>l[5]:
    print(":(")
else:
    print("Yay!")
import math
num=list(map(int,input().split()))
A=num[0]
B=num[1]
S=(A*B)//math.gcd(A,B)
print(S)


### FIXED CODE ###

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

num=list(map(int,input().split()))
A=num[0]
B=num[1]
S=lcm(A,B)
print(S)
import sys
from itertools import combinations_with_replacement
from numba import njit


@njit('(i8,i8,i8[:,:])')
def solve(n, m, query):
    li = [0] * m
    for i in range(1, m + 1):
        li[i - 1] = i
    max_point: int = 0
    for a in combinations_with_replacement(li, n):
        point = 0
        for qer in query:
            if a[qer[1] - 1] - a[qer[0] - 1] == qer[2]:
                point += qer[3]
        if point > max_point:
            max_point = point
    print(max_point)


def main():
    input = sys.stdin.buffer.readline
    n, m, q = map(int, input().split())
    query = [list(map(int, input().split())) for _ in range(q)]
    solve(n, m, query)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from itertools import combinations_with_replacement


def main():
    input = sys.stdin.buffer.readline
    n, m, q = map(int, input().split())
    query = [list(map(int, input().split())) for _ in range(q)]
    li = [0] * m
    for i in range(1, m + 1):
        li[i - 1] = i
    max_point = 0
    for a in combinations_with_replacement(li, n):
        point = 0
        for qer in query:
            if a[qer[1] - 1] - a[qer[0] - 1] == qer[2]:
                point += qer[3]
        if point > max_point:
            max_point = point
    print(max_point)


if __name__ == '__main__':
    main()
n = int(input())
s = list(map(int,input().split()))
ans = 0
for i in range(n-2):
    a,b,c=p[i:i+3]
    if a<b<c or c<b<a:  ans += 1
print(ans)


### FIXED CODE ###

n = int(input())
s = list(map(int,input().split()))
ans = 0
for i in range(n-2):
    a,b,c = s[i:i+3]
    if a<b<c or c<b<a:  ans += 1
print(ans)
# coding: UTF-8
from collections import deque
N, M = map(int,input().split())
p = {}
link = set()
q = {}

for u in range(1,N+1):
  q[u] = deque()

for j in range(M):
  L,R,D = map(int,input().split())
  link.add((L,R,D))
  #link.add((R,L,-D))
  q[L].append((R,D))
  q[R].append((L,-D))
  
def depthfirstsearch(start):
  while q[start]:
    target,d = q[start].pop()
    if target not in p:
      p[target] = p[start] + d
      depthfirstsearch(target)



for start in range(1,N+1):
  if start not in p:
    p[start] = 0
    depthfirstsearch(start)

#for u in range(1,N+1):
  #print("position of " + str(u) + " = " + str(p[u]))

for L,R,D in link:
  if p[R] != p[L] + D:
    #print((L,R))
    #print((p[L],D,p[R]))
    print("No")
    exit()

print("Yes")


### FIXED CODE ###

# coding: UTF-8
from collections import deque
N,M = map(int,input().split())
link = [ [] for _ in range(N+1)]
value = [ -1 for _ in range(N+1)]

for _ in range(M):
  L,R,D = map(int,input().split())
  link[L].append((R,D))
  link[R].append((L,-D))

searched = set()
queue = deque()
for u in range(N+1):
  if u not in searched:
    queue.append(u)
    searched.add(u)
    value[u] = 0
    while queue:
      v = queue.popleft()
      for w,d in link[v]:
        if w not in searched:
          value[w] = value[v] + d
          queue.append(w)
          searched.add(w)

#verification
for v in range(N+1):
  for w,d in link[v]:
    if value[w] != value[v] + d:
      print("No")
      exit()
print("Yes")
import math
count = 0
n,d = map(int,input().split())
for i in range(n):
  x,y = map(int,input().split())
  if x**2+y**2 ** 0.5 <= d:
    count += 1
print(count)

### FIXED CODE ###

count = 0
x = 0
y = 0
n,d = map(int,input().split())
for i in range(n):
  x,y = map(int,input().split())
  if (x**2+y**2 )** 0.5 <= d:
    count += 1
print(count)
あ

### FIXED CODE ###

A,B=map(int,input().split())
import math
print(math.ceil((B-1)/(A-1)))
# coding: utf-8
from collections import defaultdict, deque

import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    P = list(map(int, args[1].split()))
    X = list(map(int, args[2].split()))

    T = defaultdict(set)
    for i, p in enumerate(P):
        T[p-1].add(i+1)

    stack = []
    Q = deque()
    Q.append(0)
    while Q:
        i = Q.popleft()
        stack.append(i)
        for c in T[i]:
            Q.append(c)

    ret = 'POSSIBLE'
    inf = 2**32-1
    Y = [0]*n
    for i in stack[::-1]:
        dp = np.full(X[i]+1, inf, np.int64)
        dp[0] = 0
        x_acc, y_acc = 0, 0
        for c in T[i]:
            x, y = X[c], Y[c]
            x_acc += x
            y_acc += y
            dp = np.minimum(
                np.concatenate((dp[:x], dp[:-x]+y)),
                np.concatenate((dp[:y], dp[:-y if 0 < y else len(dp)]+x))
            )

        A = np.sort(dp[1:])
        if len(T[i]) == 0:
            Y[i] = 0
        elif A[0] < inf:
            Y[i] = A[0]
        elif y_acc != 0:
            ret = 'IMPOSSIBLE'
            break
        else:
            Y[i] = x_acc

    return ret


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))


### FIXED CODE ###

# coding: utf-8
from collections import defaultdict, deque

import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    P = list(map(int, args[1].split()))
    X = list(map(int, args[2].split()))

    T = defaultdict(set)
    for i, p in enumerate(P):
        T[p-1].add(i+1)

    stack = []
    Q = deque()
    Q.append(0)
    while Q:
        i = Q.popleft()
        stack.append(i)
        for c in T[i]:
            Q.append(c)

    ret = 'POSSIBLE'
    inf = 2**32-1
    Y = [0]*n
    for i in stack[::-1]:
        prev = np.full(X[i]+1, inf, np.int64)
        prev[0] = 0
        x_acc, y_acc = 0, 0
        for c in T[i]:
            cur = np.full(X[i]+1, inf, np.int64)
            x, y = X[c], Y[c]
            x_acc += x
            y_acc += y
            cur[x:] = np.minimum(cur[x:], y+prev[:-x if 0 < x else len(prev)])
            cur[y:] = np.minimum(cur[y:], x+prev[:-y if 0 < y else len(prev)])
            prev = np.copy(cur)

        y = np.min(prev)
        if y < inf:
            Y[i] = y
        else:
            ret = 'IMPOSSIBLE'
            break

    return ret


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))
from functools import reduce
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, a, b = map(int, read().split())
MOD = 10 ** 9 + 7


def combo_mod(n, a):
    t = reduce(lambda x, y: x * y % MOD, range(n, n - a, -1))
    b = reduce(lambda x, y: x * y % MOD, range(1, a + 1))
    return t // b


ans = pow(2, n, MOD) - combo_mod(a) - combo_mod(b) - 1
ans %= MOD

print(ans)


### FIXED CODE ###

from functools import reduce
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, a, b = map(int, read().split())
MOD = 10 ** 9 + 7


def comb_mod(n, a):
    num = reduce(lambda x, y: x * y % MOD, range(n, n - a, -1))
    den = reduce(lambda x, y: x * y % MOD, range(1, a + 1))
    return num * pow(den, MOD - 2, MOD) % MOD


ans = pow(2, n, MOD) - comb_mod(n, a) - comb_mod(n, b) - 1
ans %= MOD

print(ans)
N = int(input())
A_list = list(map(int, input()).split())

a_sum = 0
b_sum = 0
for i in range(N//2):
  a,b = (A_list[2*i], A_list[2*i + 1]) if A_list[2*i] > A_list[2*i + 1] else (A_list[2*i + 1], A_list[2*i])
  a_sum += a
  b_sum += b

print(a_sum - b_sum)
 

### FIXED CODE ###

N = int(input())
A_list = sorted(list(map(int, input().split())))[::-1]
a_sum = 0
b_sum = 0

for i in range(N):
    if i % 2 == 0:
        a_sum += A_list[i]
    else:
        b_sum += A_list[i]
print(a_sum - b_sum)
n,m = map(int,input().split())
h = list(map(int,input().split()))
#h.sort()

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

print(l)
  
c = 0
for j in range(n):
  if len(l[j]) == 0:
    c += 1
  else:
    m = 0
    for k in l[j]:
      if h[j] <= h[k]:
        m = 1
        break
    if m == 0:

      c += 1
      
print(c)
  

### FIXED CODE ###

n,m = map(int,input().split())
h = list(map(int,input().split()))
#h.sort()
l = [[] for _ in range(n)]

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

c = 0
for j in range(n):
  if len(l[j]) == 0:
    c += 1
  else:
    m = 0
    for k in l[j]:
      if h[j] <= h[k]:
        m = 1
        break
    if m == 0:

      c += 1
      
print(c)
A = []
for i in range(3):
    A.append(list(input().split()))
N = int(input())
B = set()
for i in range(N):
    B.append(input())
if {A[0]} <= B or {A[1]} <= B or {A[2]} <= B or {A[0][0], A[1][0], A[2][0]} <= B or {A[0][1], A[1][1], A[2][1]} <= B or \
{A[0][2], A[1][2], A[2][2]} <= B or {A[0][0], A[1][1], A[2][2]} <= B or {A[2][2], A[1][1], A[2][0]} <= B:
print("Yes")
else:
    print("No")

### FIXED CODE ###

A = []
for i in range(3):
    A.append(list(input().split()))
N = int(input())
B = []
for i in range(N):
    B.append(input())
B=set(B)
if set(A[0]) <= B or set(A[1]) <= B or set(A[2]) <= B or {A[0][0], A[1][0], A[2][0]} <= B or\
{A[0][1], A[1][1], A[2][1]} <= B or {A[0][2], A[1][2], A[2][2]} <= B \
or {A[0][0], A[1][1], A[2][2]} <= B or {A[0][2], A[1][1], A[2][0]} <= B:
    print("Yes")
else:
    print("No")
count = 0
for i in range(min(k+1, s + 2)):
    for j in range(i + 1, s + 2):
        x = i
        y = j - i - 1
        z = s - y - x
        if x <= k and y <= k and z <= k:
            count += 1

### FIXED CODE ###

ks = input()
k, s = [int(x) for x in ks.split(" ")]
comb = []
for x in range(min(k,s) + 1)[::-1]:
    for y in range(min(x + 1, s - x + 1))[::-1]:
        z = s - x - y
        if (x <= k and y <= k and z <= k and y <= x and z <= y):
            comb.append([x,y,z])
count = 0
for c in comb:
    l = len(set(c))
    if l == 1:
        count += 1
    elif l == 2:
        count += 3
    else:
        count += 6
print(count)
n,m=map(int,input().split())
lst=[]
for i in range(m):
  l=int(input())
  r=int(input())
  for j in range(l,r+1):
    lst.append(j)
print(len(set(lst)))


### FIXED CODE ###

n,m=map(int,input().split())
l=[]
r=[]
for i in range(m):
  a,b=map(int,input().split())
  l.append(a)
  r.append(b)
lm=max(l)
rm=min(r)
if rm-lm<0:
  print(0)
else:
  print(rm-lm+1)
n = int(input())
a = list(map(int ,input().split()))
cnt = 0
for i in range(1, n-1):
  if(max(a[i-1:i+1]) != a[i] and min(a[i-1:i+1] != a[i]):
     cnt += 1
print(cnt)

### FIXED CODE ###

n = int(input())
a = list(map(int ,input().split()))
cnt = 0
for i in range(1, n-1):
  hoge = [a[i-1], a[i], a[i+1]]
  if(max(hoge) != a[i] and min(hoge) != a[i]):
     cnt += 1
print(cnt)
n = int(input())
a = int(input())
if (n%500)-a =<0:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

n = int(input())
a = int(input())
if (n%500)-a <=0:
  print("Yes")
else:
  print("No")
s,t=input(),input()
dp=[[0]*(len(t)+1) for i in range(len(s)+1)]
for i in range(len(s)+1):
    for j in range(len(t)+1):
        if i<len(s):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j])
        if j<len(t):
            dp[i][j+1]=max(dp[i][j+1],dp[i][j])
        if i<len(s) and j<len(t):
            dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+(s[i]==t[j]))
ans=""
def move(i,j):
    global dp,ans
    if dp[i][j]>dp[i-1][j-1] and s[i-1]==t[j-1]:
        ans=s[i-1]+ans
        if dp[i-1][j-1]!=0:
            move(i-1,j-1)
    else:
        if dp[i][j]==dp[i][j-1]:
            move(i,j-1)
        else:
            move(i-1,j)
move(len(s),len(t))
print(ans)

### FIXED CODE ###

from sys import setrecursionlimit
setrecursionlimit(10**7)
s,t=input(),input()
dp=[[0]*(len(t)+1) for i in range(len(s)+1)]
for i in range(len(s)+1):
    for j in range(len(t)+1):
        if i<len(s):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j])
        if j<len(t):
            dp[i][j+1]=max(dp[i][j+1],dp[i][j])
        if i<len(s) and j<len(t):
            dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+(s[i]==t[j]))
ans=""
def move(i,j):
    global dp,ans
    if i==0 or j==0:
        return
    elif dp[i][j]>dp[i-1][j-1] and s[i-1]==t[j-1]:
        ans=s[i-1]+ans
        move(i-1,j-1)
    else:
        if dp[i][j]==dp[i][j-1]:
            move(i,j-1)
        else:
            move(i-1,j)
move(len(s),len(t))
print(ans)
N,*A  =map(int,open(0).read().split())

L = [0 for i in range(N+3)]

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

ans = 0
for i in range(N+2):
  S = sum(L[i:i+3])
  if ans < S:
    ans = S
  
print(ans)

### FIXED CODE ###

N,*A  =map(int,open(0).read().split())

L = [0 for i in range(int(1e5)+1)]

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

ans = 0
for i in range(int(1e5)+1):
  S = sum(L[i:i+3])
  if ans < S:
    ans = S
  
print(ans)
s = set(input())
on = {a, b, c, d, e, f, g, h ,i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z}
if s == on:
  print("None")
print(min(on-s))

### FIXED CODE ###

s = set(input())
on = set("abcdefghijklmnopqrstuvwxyz")
try:
  print(min(on-s))
except ValueError:
  print("None")
import math
A, B, C, D = map(int, input().split())

def lcm(x, y):
    return x*y/math.gcd(x, y)

num = lcm(C, D)
if(A%C==0):
    num1 = A//C-1
else:
    num1 = A//C
if(A%D==0):
    num2 = A//D-1
else:
    num2 = A//D
if(A%num == 0):
    num3 = A//num-1
else:
    num3 = A//num
print(int(B-A+1-((B//C-num1)+(B//D-num2)-(B//num-num3))))

### FIXED CODE ###

import fractions
A, B, C, D = map(int, input().split())

num = (C*D)//(fractions.gcd(C, D))
if(A%C==0):
    num1 = A//C-1
else:
    num1 = A//C
if(A%D==0):
    num2 = A//D-1
else:
    num2 = A//D
if(A%num == 0):
    num3 = A//num-1
else:
    num3 = A//num
print(int(B-A+1-((B//C-num1)+(B//D-num2)-(B//num-num3))))
mi=-(10**)
n,k=[int(x) for x in input().split()]
p=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]

#ループの最大値を求める
mxs=mi
otl=mi
lpc={-1:1}
for i in range(n):
    chkr={i:1}
    wks=0
    nxt=i
    for _ in range(n):
        nxt=p[nxt]-1
        if chkr.get(nxt):
            wks+=c[nxt]
            break
        chkr[nxt]=1
        wks+=c[nxt]
        otl=max(otl,wks)
    if mxs<wks:
        mxs=wks
        lpc=chkr
dv=k//len(lpc)
rst=k%len(lpc)

mxr=0
for i in lpc.keys():
    wks=0
    nxt=i
    for _ in range(rst):
        nxt=p[nxt]-1
        wks+=c[nxt]
        mxr=max(mxr,wks)

ans=max(dv*mxs+mxr,otl)
print(ans)


### FIXED CODE ###

n,k=[int(x) for x in input().split()]
p=[int(x)-1 for x in input().split()]
c=[int(x) for x in input().split()]

ans=max(c)
if ans<0:
    print(ans)
    exit()

for i in range(n):
    #最後に自分という順番
    nxt=i
    lp=[]
    while 1:
        nxt=p[nxt]
        lp.append(nxt)
        if nxt==i:
            break
    lpc=sum(c[x] for x in lp)
    nxt=i
    lpl=len(lp)
    wks=0
    for j in range(lpl):
        nxt=p[nxt]
        wks+=c[nxt]
        ans=max(ans,(k-j-1)//lpl*max(0,lpc)+wks)
        if j==k-1:
            break
print(ans)