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

// GO TO WORK 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <list>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <complex>
#include <queue>
#include <deque>
#include <cmath>
#include <climits>
#include <cfloat>
#include <iomanip>
using namespace std;


#define FOR(i,a,b) for ( int i( a ) ; i < int(b) ; i++)
#define ROF(i,a,b) for ( int i( a ) ; i >= int(b) ; i--)
#define ALL(c) c.begin() , c.end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long ll;
typedef long double ld;
typedef pair <int,int> pii;

const int MAX_N = (int) 1e3 + 20;
int a[MAX_N];
int Par[MAX_N] , rank[MAX_N];
int mark[MAX_N];
int dp[MAX_N][MAX_N];
vector <int> R , ans;
int n , pos;



int find( int v ){ return Par[v] = ( Par[v] == v ? v : find( Par[v] ) ); }
void merge( int v , int u ){
	v = find(v) , u = find(u);


	if ( rank[v] > rank[u] ){
		Par[u] = v;
		rank[v] += rank[u];
	}
	else{
		Par[v] = u;
		rank[u] += rank[v];
	}
}




int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> pos;
	--pos;
	FOR(i,0,n){
		cin >> a[i];
		a[i]--;
	}

	FOR(i,0,n)
		Par[i] = i , rank[i] = 1;
	FOR(i,0,n)
		if ( a[i] != -1 ){
			int v = i , u = a[i];
			merge( v , u );
		}

	FOR(i,0,n){
		int v = find(i);
		if ( !mark[v] && v != find(pos) ){
			mark[v] = 1;
			R.pb( rank[v] );

		}
	}

	sort( ALL(R) );

	int ted = 1 , ver = pos;
	while ( a[ ver ] != -1 ){
		++ted;
		ver = a[ ver ];
	}

	FOR(j,0,R.size())
		FOR(i,0,n){
			if ( i == 0 )
				dp[i][j] = 1;
			else if ( j == 0 && R[0] == i )
				dp[i][j] = 1;
			else{
				dp[i][j] = dp[i][j-1];
				if ( !dp[i][j] && i >= R[j] )
					dp[i][j] = dp[i-R[j]][j-1];
			}
		}

	int Size = (int) R.size()-1;
	FOR(i,0,n)
		if ( dp[i][Size] )
			cout << i + ted << endl;


	return 0;




}
