20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

#include <iostream>
using namespace std;
int prev[1003],after[1003],a[1003];
bool dp[1003];
int n,x,cnt;
int main(){
	cin>>n>>x;
	dp[0]=true;
	for(int i=1;i<=n;i++){cin>>prev[i];if(prev[i]!=0)after[prev[i]]=i;}
	int smart=0;
	int now=x;
	while(after[now]!=0){
						 now=after[now];
						 smart++;
	}
	for(int i=1;i<=n;i++)if(prev[i]==0){
			int now=i,sum=0;
			while(after[now]!=0){
							   now=after[now];
							   sum++;
			}
			a[cnt++]=sum;
	}
	for(int i=0;i<cnt;i++)
	for(int j=1;j<=n;j++){
			if(dp[j-a[i]])dp[j]=true;
	}
	for(int i=1;i<=n;i++)if(dp[i])cout<<smart+i-1;
	return 0;
}
	
