#50139: python 土法煉鋼解法


sandfish98@gmail.com (Sandfish)

學校 : 不指定學校
編號 : 215351
來源 : [27.51.90.76]
最後登入時間 :
2025-09-27 16:07:02

n,t=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
tp=[0]*n
brown=0
while True:
    if tp[t]==1:
        break
    elif y[t]==1:
        brown+=1
    tp[t]+=1
    t=x[t]
print(brown)