#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#include <queue>
#include <deque>

using namespace std;

int i,j,n,x;;
vector <int> V;
int siz,anssize;
bool ans[1001];
vector <int> size;

struct GG{
  int urd;
  int hoid;
}GO[1001];

bool dfs(int ii){
  siz++;
  if(ii==x) return 1;
  if(GO[ii].urd==0) return 0;
  dfs(GO[ii].urd);
}

int main(){

  cin >> n >> x;
  int a;

  for(i=1;i<=n;i++){
    GO[i].urd = 0;
    GO[i].hoid = 0;
  }

  for(i=1;i<=n;i++){
    cin >> a;
    GO[a].hoid = i;
    GO[i].urd = a;
  }

  for(i=1;i<=n;i++){
    //cout << i << endl;
    if(GO[i].hoid==0){
      siz = 0;
      if(dfs(i)==0){
        size.push_back(siz);
        //cout << "orson";
      }
      else anssize = siz;
    }
  }
  int len = size.size();
  cout << len << endl;
  for(i=1;i<=len;i++)
    ans[size[i]] = 1;

  for(i=1;i<=len;i++){
    for(j=1;j<=n;j++){
      if(ans[j]==1)ans[j+size[i]] = 1;
    }
  }

  cout << anssize << endl;
  for(i=1;i<=n;i++)
    if(ans[i]==1) cout << i+anssize << endl;
  return 0;
}


/*
Author:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%   LALIT KUNDU      %%%%%%%%
%%%%%%%%   IIIT HYDERABAD   %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
#define lld long long int
#define llu long long unsigned int
int compare(const void * a, const void * b){return *(lld *)a-*(lld *)b;}
#define min(a,b)(a>b?b:a)
#define max(a,b)(a<b?b:a)
#define sort(arr,n) qsort(arr,n,sizeof(arr[0]),compare)
#define sd(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define su(n) scanf("%llu",&n)
#define rep(i,start,end) for(i=start; i<end; i++)
#define pdn(n) printf("%d\n",n)
#define pln(n) printf("%lld\n",n)
#define pun(n) printf("%llu\n",n)
#define pd(n) printf("%d",n)
#define pl(n) printf("%lld",n)
#define pu(n) printf("%llu",n)
#define pn printf("\n")
#define ps printf(" ")
char subset[1009][1009];

void abc(int set[], int n, int sum)
{
    int i,j;
    for (i = 0; i <= n; i++)
        subset[0][i] = 1;
    for (i = 1; i <= sum; i++)
        subset[i][0] = 0;
    for (i = 1; i <= sum; i++)
    {
        for (j = 1; j <= n; j++)
        {
            subset[i][j] = subset[i][j-1];
            if (i >= set[j-1])
                subset[i][j] = subset[i][j] || subset[i - set[j-1]][j-1];
        }
    }

}
int main()
{
    int n,x,arr[1009],ar[1009]={},l=0,set[1009],m=0,j,i,a;
    sd(n),sd(x);
    for(i=1; i<=n; i++)
    {
        sd(a);
        arr[a]=i;
        if(a==0)
            ar[l++]=i;
    }
    int lalit=0;
    for(i=0; i<l; i++)
    {
        if(ar[i]==x)
            lalit=1;
        else
        {
            int count=1;
            j=arr[ar[i]];
            if(j!=0)
            {
                while(1)
                {
                    if(j==x)
                    {
                        lalit=count+1;
                        count=-1;
                        break;
                    }
                    j=arr[j];
                    count++;
                    if(j==0)break;
                }
            }
            if(count!=-1)
                set[m++]=count;
        }
    }
    abc(set, m, n+1);
    pdn(lalit);
    for(i=1; i<=n; i++)
        if(subset[i][m]==1)pdn(lalit+i);
    return 0;
}


#include<iostream>              //------------------------------------------------------------//
#include<cstdio>                //  ___  ___ _____ ______                                     //
#include<algorithm>             //  |  \/  |/  ___|| ___ \     This C++ Template Belongs to   //
#include<cmath>                 //  | .  . |\ `--. | |_/ /        Manish Singh Bisht          //
#include<vector>                //  | |\/| | `--. \| ___ \       http://fb.me/manish05        //
#include<set>                   //  | |  | |/\__/ /| |_/ /    Email: manish05@facebook.com    //
#include<map>                   //  \_|  |_/\____/ \____/                                     //
#include<functional>            //------------------------------------------------------------//
#include<string>
#include<cstring>
#include<bitset>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>

#include<stack>
#include<cstdio>
#include<ctime>

using namespace std;
#define gc getchar_unlocked
#define MEM(a,b) memset(a,(b),sizeof(a))
#define FOR(i,n) for(int i=(0);i<(n);i++)
#define CLEAR(a) memset((a),0,sizeof(a))
#define S(n) scanf("%d", &n)
#define P(k) printf("%lld\n", k)
#define fastS(n) scanint(&n)
#define pb push_back
#define mp make_pair
#define ll long long
#define VI vector<int>
#define PII pair<int, int>
#define ft first
#define sd second
#define inf (1<<30)
#define PNL printf("\n")
#define md 1000000007
#define Swap(a,b) {*b = (*a + *b) - (*a = *b);}
#define swap(a,b) Swap(&a,&b)
#define bigger(a,b) {a>b?a:b}
#define smaller(a,b) {a<b?a:b}

ll modPow(ll a,ll b)
{ll x;
    for(x=1;b>0;b/=2,a=(a*a)%md)
        if(b%2==1)
                x=(x*a)%md;
  return x%md;
}

string convertInt(int number){stringstream ss;ss << number;return ss.str();}
int convertString(string s){int num;stringstream sstr(s);sstr>>num;return num;}
int gcd(int a, int b){ return b?gcd(b,a%b):a; }

char blahh[1009][1009];

void abc(int set[], int n, int sum)
{
    int i,j;
    for (i = 0; i <= n; i++)
        blahh[0][i] = 1;
    for (i = 1; i <= sum; i++)
        blahh[i][0] = 0;
    for (i = 1; i <= sum; i++)
    {
        for (j = 1; j <= n; j++)
        {
            blahh[i][j] = blahh[i][j-1];
            if (i >= set[j-1])
                blahh[i][j] = blahh[i][j] || blahh[i - set[j-1]][j-1];
        }
    }

}
int main()
{
    int n,x,arr[1009],ar[1009]={},l=0,set[1009],m=0,j,i,a;
    cin>>n>>x;
    for(i=1; i<=n; i++)
    {
        cin>>a;
        arr[a]=i;
        if(a==0)
            ar[l++]=i;
    }
    int msb=0;
    for(i=0; i<l; i++)
    {
        if(ar[i]==x)
            msb=1;
        else
        {
            int count=1;
            j=arr[ar[i]];
            if(j!=0)
            {
                while(1)
                {
                    if(j==x)
                    {
                        msb=count+1;
                        count=-1;
                        break;
                    }
                    j=arr[j];
                    count++;
                    if(j==0)break;
                }
            }
            if(count!=-1)
                set[m++]=count;
        }
    }
    abc(set, m, n+1);
    cout<<msb;
    for(i=1; i<=n; i++)
        if(blahh[i][m]==1)cout<<(msb+i);
    return 0;
}


/*
	Author : NNAVNEET
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <assert.h>
using namespace std;
/*========================================Templates=============================================*/
// datatypes
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef float ft;
typedef unsigned int uint;
int gcd( int a, int b ) {  if( !b ) return a;  return gcd( b, a % b ); }

#define GI ({int t;scanf("%d",&t);t;})
#define tests(tc) int tc;scanf("%d",&tc);while(tc--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORN(i,a,b,n) for(int i=(a);i<=(b);i+=n)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define FORRN(i,a,b,n) for(int i=(a);i>=(b);i-=n)
#define CLEAR(arr,v)      memset(arr,v,sizeof(arr))
#define DB(x) cerr<<#x<<" : "<<x<<endl<<flush;
#define DB2(x,y) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl<<flush;
#define DB3(x,y,z) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl<<flush;
#define DBAR(arr,a,b) cout<<#arr<<" : ";FOR(dbar,a,b) cerr<<arr[dbar]<<" ";cerr<<endl;
#define DBAR2(arr,a,b,x,y) cout<<#arr<<endl;FOR(dbar,a,b){ FOR(dbar2,x,y)cerr<<arr[dbar][dbar2]<<" ";cerr<<endl;}
#define INF 1<<30
#define MOD 1000000007

/*======================================Templates Ends========================*/
/* Main Code Starts from here */
int a[1005],imd[1005],end[1005];
bool present[1005]={false};
bool countt[1005]={false};
int main()
{
	int n,x,y;
	n = GI;
	y = GI;
	int i,j,k,l;
	FOR(i,1,n)
	a[i] = GI;
	int cnt =0;
	i=y;
	cnt++;
	present[y] = true;

	while(a[i]!=0)
	{
		imd[i]=-10000;
		i=a[i];
		present[i] = true;
		cnt++;
	}
	imd[i]=cnt;
	cout<<cnt<<endl;
	l=i;
	int ans=cnt;
	FOR(i,1,n)
	{
		cnt=0;
		int flag=0;
		if(!present[i])
		{
			cnt++;
			j=i;
			present[i]= true;
			while(a[j]!=0)
			{
				imd[j]=-10000;
				j=a[j];
				if(a[j])
				present[j]=true;
				if(j==y)
				flag=1;
				cnt++;

			}

			if(a[j]==0&&!flag)
			{
				imd[j]= cnt;
				present[j]=true;
			}

		}
	}
	 k=0;
	FOR(i,1,n)
	{
	//	cout<<imd[i]<<"  "<<k<<endl;
		if(imd[i]!=-10000&&i!=l)
		end[k++]=imd[i];
	}

	sort(end,end+k);

	REP(i,k)
	{
		FOR(j,i,k-1)
		{
			if(!countt[end[j]+ans]&&(end[j]+ans)<=n)
			cout<<end[j]+ans<<endl,countt[end[j]+ans]=true;
		}
		ans+=end[i];
	}

    return 0;
}


/*
	Author : NNAVNEET
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <assert.h>
using namespace std;
/*========================================Templates=============================================*/
// datatypes
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef float ft;
typedef unsigned int uint;
int gcd( int a, int b ) {  if( !b ) return a;  return gcd( b, a % b ); }

#define GI ({int t;scanf("%d",&t);t;})
#define tests(tc) int tc;scanf("%d",&tc);while(tc--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORN(i,a,b,n) for(int i=(a);i<=(b);i+=n)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define FORRN(i,a,b,n) for(int i=(a);i>=(b);i-=n)
#define CLEAR(arr,v)      memset(arr,v,sizeof(arr))
#define DB(x) cerr<<#x<<" : "<<x<<endl<<flush;
#define DB2(x,y) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl<<flush;
#define DB3(x,y,z) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl<<flush;
#define DBAR(arr,a,b) cout<<#arr<<" : ";FOR(dbar,a,b) cerr<<arr[dbar]<<" ";cerr<<endl;
#define DBAR2(arr,a,b,x,y) cout<<#arr<<endl;FOR(dbar,a,b){ FOR(dbar2,x,y)cerr<<arr[dbar][dbar2]<<" ";cerr<<endl;}
#define INF 1<<30
#define MOD 1000000007

/*======================================Templates Ends========================*/
/* Main Code Starts from here */
int a[1005],imd[1005],end[1005];
bool present[1005]={false};
bool countt[1005]={false};
int main()
{
	int n,x,y;
	n = GI;
	y = GI;
	int i,j,k,l;
	FOR(i,1,n)
	a[i] = GI;
	int cnt =0;
	i=y;
	cnt++;
	present[y] = true;

	while(a[i]!=0)
	{
		imd[i]=-10000;
		i=a[i];
		present[i] = true;
		cnt++;
	}
	imd[i]=cnt;
	//cout<<cnt<<endl;
	l=i;
	int ans=cnt;
	FOR(i,1,n)
	{
		cnt=0;
		int flag=0;
		if(!present[i])
		{
			cnt++;
			j=i;
			present[i]= true;
			while(a[j]!=0)
			{
				imd[j]=-10000;
				j=a[j];
				if(a[j])
				present[j]=true;
				if(j==y)
				flag=1;
				cnt++;

			}

			if(a[j]==0&&!flag)
			{
				imd[j]= cnt;
				present[j]=true;
			}

		}
	}
	 k=0;
	FOR(i,1,n)
	{
	//	cout<<imd[i]<<"  ";
		if(imd[i]!=-10000&&i!=l)
		end[k++]=imd[i];
	}

	countt[0]=true;

	FOR(i,0,k-1)
	{
		x=end[i];
		FORR(j,1005,0)
		{
			if(countt[j])
			{countt[j+x]=true;}
		}
	}

	FOR(i,0,n)
	{
		//cout<<countt[i]<<"  "<<i<<endl;
		if(countt[i])
		cout<<i+ans<<endl;
	}

    return 0;
}


/*
	Author : NNAVNEET
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <assert.h>
using namespace std;
/*========================================Templates=============================================*/
// datatypes
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef float ft;
typedef unsigned int uint;
int gcd( int a, int b ) {  if( !b ) return a;  return gcd( b, a % b ); }

#define GI ({int t;scanf("%d",&t);t;})
#define tests(tc) int tc;scanf("%d",&tc);while(tc--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORN(i,a,b,n) for(int i=(a);i<=(b);i+=n)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define FORRN(i,a,b,n) for(int i=(a);i>=(b);i-=n)
#define CLEAR(arr,v)      memset(arr,v,sizeof(arr))
#define DB(x) cerr<<#x<<" : "<<x<<endl<<flush;
#define DB2(x,y) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl<<flush;
#define DB3(x,y,z) cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl<<flush;
#define DBAR(arr,a,b) cout<<#arr<<" : ";FOR(dbar,a,b) cerr<<arr[dbar]<<" ";cerr<<endl;
#define DBAR2(arr,a,b,x,y) cout<<#arr<<endl;FOR(dbar,a,b){ FOR(dbar2,x,y)cerr<<arr[dbar][dbar2]<<" ";cerr<<endl;}
#define INF 1<<30
#define MOD 1000000007

/*======================================Templates Ends========================*/
/* Main Code Starts from here */
int a[1005],anss[1005];
bool flag[1005]={false},cntr[1005]={false};
main()
{
	int n,x;
	n=GI;
	x=GI;
	FOR(i,1,n)
	a[i]=GI;

	int i,j,k,cnt=0,ans=0;

	i=x;
	cnt++;
	flag[x]=true;
	while(a[i]!=0)
	{
		i=a[i];
		flag[i]=true;
		cnt++;
	}
	cout<<cnt<<"\n";
	cntr[cnt] = true;
	//cout<<"flag\n";
//	FOR(i,1,n)
	//cout<<flag[i]<<"  ";
	ans=cnt;
	FOR(i,1,n)
	{
		cnt=0;
		if(!flag[i])
		{
			j=i;
		//	cout<<"egergrehrt"<<a[j]<<"   "<<flag[j]<<endl;
		//	flag[i]=true;
			cnt++;
			while(a[j]!=0)
			{
			//	cout<<"enter\n";
				flag[j]=true;
				j=a[j];
				cnt++;
			}
			//cout<<j<<"  "<<flag[j]<<"  "<<a[j]<<endl;
			if(flag[j]==false&&a[j]==0)
			{
				flag[j]=true;
				anss[j]=cnt;
			}
			else if(flag[j]==true&&a[j]==0)
			{
				anss[j]=cnt;
			}
		}
	}
	int kalu=0;
	FOR(i,1,n)
	{
		int flag =0;
		FOR(j,i,n)
	{
		//cout<<endl<<a[i]<<endl;
		if(a[j]==0&&j!=x)
		{
			if(cntr[anss[j]+ans]==false&&anss[j]+ans<=n)
		{cout<<anss[j]+ans<<"\n"; cntr[anss[j]+ans]=true;}
		if(flag==0)
		{
			k = j;
			kalu = anss[j];
			flag=1;
		}
		}
	}
//	cout<<k<<"    "<<kalu<<endl;
	if(flag==1)
	{i=k;
	ans+= kalu;
    }
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<bitset>
#include<utility>
#include<cmath>
#include<climits>
#define REP(i,n) for(int (i) = 0; (i) < (int)(n); ++(i))
#define REPN(i,s,n) for(int (i) = s; (i) < (int)(n); ++(i))
#define ZERO(x) memset(x,0,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef unsigned long long ULL;
const int maxn = 10000+10;
const int MOD = 1000000007;
int n,x;
int at;
int pa[1010],d[1010],s[1010];
int a[1010];
int findset(int x){
   if(pa[x]!=x){
      int root = findset(pa[x]);
      d[x] += d[pa[x]];
      return pa[x] = root;
   }
   else return x;
}
int dp[1010];
int main()
{
   scanf("%d %d",&n,&x);
   for(int i=0;i<n;++i)scanf("%d",&a[i+1]);
   for(int i=1;i<=n;++i){
      s[i] = 1;pa[i] = i;
   }
   for(int i=1;i<=n;++i){
      if(a[i]!=0){
         pa[i] = a[i];
         d[i] = 1;
         int u = findset(i);
         s[u] += s[i];
      }
   }
   map<int,int> tt;
   int mm = findset(x);
   for(int i=1;i<=n;++i)if(findset(i)!=mm) tt[findset(i)] = s[findset(i)];
   dp[d[x]+1] = 1;
//   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++it)printf("%d\n",it->second);
   int kk=2;
   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++kk,++it)for(int i=1;i<=n;++i)
      if(dp[i]&&dp[i]!=kk && (it->second) + i <=n && dp[it->second+i]!=kk-1){
      //printf("push from %d to %d %d\n",i,i+it->second,kk);
      dp[i+(it->second)] = kk;
      //printf("%d\n",dp[i+(it->second)]);
   }
   for(int i=1;i<=n;++i)if(dp[i])printf("%d\n",i);
}


#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<bitset>
#include<utility>
#include<cmath>
#include<climits>
#define REP(i,n) for(int (i) = 0; (i) < (int)(n); ++(i))
#define REPN(i,s,n) for(int (i) = s; (i) < (int)(n); ++(i))
#define ZERO(x) memset(x,0,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef unsigned long long ULL;
const int maxn = 10000+10;
const int MOD = 1000000007;
int n,x;
int at;
int pa[1010],d[1010],s[1010];
int a[1010];
int findset(int x){
   if(pa[x]!=x){
      int root = findset(pa[x]);
      d[x] += d[pa[x]];
      return pa[x] = root;
   }
   else return x;
}
int dp[1010];
int main()
{
   scanf("%d %d",&n,&x);
   for(int i=0;i<n;++i)scanf("%d",&a[i+1]);
   for(int i=1;i<=n;++i){
      s[i] = 1;pa[i] = i;
   }
   for(int i=1;i<=n;++i){
      if(a[i]!=0){
         pa[i] = a[i];
         d[i] = 1;
         int u = findset(i);
         s[u] += s[i];
      }
   }
   map<int,int> tt;
   int mm = findset(x);
   for(int i=1;i<=n;++i)if(findset(i)!=mm) tt[findset(i)] = s[findset(i)];
   dp[d[x]+1] = 1;
//   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++it)printf("%d\n",it->second);
   int kk=2;
   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++kk,++it)for(int i=1;i<=n;++i)if(dp[i]&&dp[i]!=kk && (it->second) + i <=n){
  //    printf("push from %d to %d %d\n",i,i+it->second,kk);
      dp[i+(it->second)] = kk;
   //   printf("%d\n",dp[i+(it->second)]);
   }
   for(int i=1;i<=n;++i)if(dp[i])printf("%d\n",i);
}


#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<bitset>
#include<utility>
#include<cmath>
#include<climits>
#define REP(i,n) for(int (i) = 0; (i) < (int)(n); ++(i))
#define REPN(i,s,n) for(int (i) = s; (i) < (int)(n); ++(i))
#define ZERO(x) memset(x,0,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef unsigned long long ULL;
const int maxn = 10000+10;
const int MOD = 1000000007;
int n,x;
int at;
int pa[1010],d[1010],s[1010];
int a[1010];
int findset(int x){
   if(pa[x]!=x){
      int root = findset(pa[x]);
      d[x] += d[pa[x]];
      return pa[x] = root;
   }
   else return x;
}
int dp[1010];
int main()
{
   scanf("%d %d",&n,&x);
   for(int i=0;i<n;++i)scanf("%d",&a[i+1]);
   for(int i=1;i<=n;++i){
      s[i] = 1;pa[i] = i;
   }
   for(int i=1;i<=n;++i){
      if(a[i]!=0){
         pa[i] = a[i];
         d[i] = 1;
         int u = findset(i);
         s[u] += s[i];
      }
   }
   map<int,int> tt;
   int mm = findset(x);
   for(int i=1;i<=n;++i)if(findset(i)!=mm) tt[findset(i)] = s[findset(i)];
   dp[d[x]+1] = 1;
//   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++it)printf("%d\n",it->second);
   int kk=2;
   for(map<int,int>::iterator it = tt.begin();it!=tt.end();++kk,++it)for(int i=1;i<=n;++i)
      if(dp[i]&&dp[i]!=kk && (it->second) + i <=n && !dp[it->second+i]){
      //printf("push from %d to %d %d\n",i,i+it->second,kk);
      dp[i+(it->second)] = kk;
      //printf("%d\n",dp[i+(it->second)]);
   }
   for(int i=1;i<=n;++i)if(dp[i])printf("%d\n",i);
}


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>

using namespace std;

int next[1111], a[1111], t[1111];
bool can[1111];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i=1;i<=n;i++) next[i] = 0;
  for (int i=1;i<=n;i++) {
    scanf("%d", a+i);
    next[a[i]] = i;
  }
  int x = 0, kt = 0;
  for (int i=1;i<=n;i++)
    if (a[i] == 0) {
      int p = i, len = 1;
      bool here = false;
      if (p == k) x = 1, here = true;
      while (next[p] != 0) {
        p = next[p];
        len++;
        if (p == k) x = len, here = true;
      }
      if (!here) t[kt++] = len;
    }
  for (int i=1;i<=n;i++) can[i] = false;
  can[x] = true;
  for (int i=0;i<kt;i++)
    for (int j=n;j>=1;j--)
      if (can[j]) can[j+t[i]] = true;
  for (int i=1;i<=n;i++)
    if (can[i]) printf("%d\n", i);
  return 0;
}


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>

using namespace std;

char s[111111];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  bool was[255];
  for (char c='A';c<='J';c++) was[c] = false;
  for (int i=0;i<n;i++)
    if (s[i] >= 'A' && s[i] <= 'J') was[s[i]] = true;
  int ans = 1, q = 10;
  for (char c='A';c<='J';c++)
    if (was[c]) {
      if (q == 10 && s[0] >= 'A' && s[0] <= 'J') ans *= 9;
      else ans *= q;
      q--;
    }
  if (s[0] == '?') ans *= 9;
  printf("%d", ans);
  for (int i=1;i<n;i++)
    if (s[i] == '?') printf("0");
  printf("\n");
  return 0;
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; ++i )
#define FOR1(i,n) for( i = 1 ; i <= n ; ++i )
#define sys_p system( "pause" )
#define pb push_back
#define mp make_pair

typedef long long LL ;
typedef vector < LL > vl ;
typedef vector < vl > vvl ;
typedef vector < vvl > vvvl ;

ifstream Cin( "input.txt" );
ofstream Cout( "output.txt" );

const LL INF = (LL)1e15 ;

main()
{
      LL n, m, i, k, bq, bqc, nb = 0, l, c ; vector < LL > q ; set < LL > x ; set < LL > :: iterator j ; cin >> n >> m ; vector < LL > a(n+1) ; vector < LL > f(n+1) ;
      FOR1( i, n ){ cin >> a[i] ; if( a[i] ) f[ a[i] ] = i ; }
      FOR1( i, n ) if( a[i] == 0 )
      {
            bqc = 0 ; l = 1 ; c = i ; if( c == m ) bqc = 1, nb = 1 ; while( f[c] ) { l++ ; c = f[c] ; if( c == m ) bqc = 1 , nb = l ; } if( c == m ) bqc = 1 , nb = l ;
            if( bqc ) bq = l ; else q.pb( l ) ;
      }
      FOR0( i, (LL)q.size() )
      {
            vector < LL > add ;
            for( j = x.begin() ; j != x.end() ; j++ ) add.pb( *j + q[i] ) ; FOR0( k, (LL)add.size() ) x.insert( add[k] ) ;
            x.insert( q[i] ) ;
      }
      vector < LL > ans ; ans.pb( nb ) ;
      for( j = x.begin() ; j != x.end() ; j++ ) ans.pb( nb + *j ) ;
      FOR0( i, (LL)ans.size() ) cout << ans[i] << endl ;
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; ++i )
#define FOR1(i,n) for( i = 1 ; i <= n ; ++i )
#define sys_p system( "pause" )
#define pb push_back
#define mp make_pair

typedef long long LL ;
typedef vector < LL > vl ;
typedef vector < vl > vvl ;
typedef vector < vvl > vvvl ;

ifstream Cin( "input.txt" );
ofstream Cout( "output.txt" );

const LL INF = (LL)1e15 ;

main()
{
      LL n, m, i, k, bq, bqc, nb = 0, l, c ; vector < LL > q ; set < LL > x ; set < LL > :: iterator j ; cin >> n >> m ; vector < LL > a(n+1) ; vector < LL > f(n+1) ;
      FOR1( i, n ){ cin >> a[i] ; if( a[i] ) f[ a[i] ] = i ; }
      FOR1( i, n ) if( a[i] == 0 )
      {
            bqc = 0 ; l = 1 ; c = i ; while( f[c] ) { l++ ; c = f[c] ; if( c == m ) bqc = 1 , nb = l ; } if( c == m ) bqc = 1 , nb = l ;
            if( bqc ) bq = l ; else q.pb( l ) ;
      }
      FOR0( i, (LL)q.size() )
      {
            vector < LL > add ;
            for( j = x.begin() ; j != x.end() ; j++ ) add.pb( *j + q[i] ) ; FOR0( k, (LL)add.size() ) x.insert( add[k] ) ;
            x.insert( q[i] ) ;
      }
      vector < LL > ans ; ans.pb( nb ) ;
      for( j = x.begin() ; j != x.end() ; j++ ) ans.pb( nb + *j ) ;
      FOR0( i, (LL)ans.size() ) cout << ans[i] << endl ;
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; ++i )
#define FOR1(i,n) for( i = 1 ; i <= n ; ++i )
#define sys_p system( "pause" )
#define pb push_back
#define mp make_pair

typedef long long LL ;
typedef vector < LL > vl ;
typedef vector < vl > vvl ;
typedef vector < vvl > vvvl ;

ifstream Cin( "input.txt" );
ofstream Cout( "output.txt" );

const LL INF = (LL)1e15 ;

main()
{
      LL n, m, i, k, bq, bqc, nb = 0, l, c ; vector < LL > q ; set < LL > x ; set < LL > :: iterator j ; cin >> n >> m ; vector < LL > a(n+1) ; vector < LL > f(n+1) ;
      FOR1( i, n ){ cin >> a[i] ; if( a[i] ) f[ a[i]] = i ; }
      FOR1( i, n ) if( f[i] == 0 )
      {
            bqc = 0 ; l = 1 ; c = i ; while( a[c] ) { l++ ; c = a[c] ; if( c == m ) bqc = 1 , nb = l ; } if( c == m ) bqc = 1 , nb = l ;
            if( bqc ) bq = l ; else q.pb( l ) ;
      }
      FOR0( i, (LL)q.size() )
      {
            vector < LL > add ;
            for( j = x.begin() ; j != x.end() ; j++ ) add.pb( *j + q[i] ) ; FOR0( k, (LL)add.size() ) x.insert( add[k] ) ;
            x.insert( q[i] ) ;
      }
      vector < LL > ans ; ans.pb( nb ) ;
      for( j = x.begin() ; j != x.end() ; j++ ) ans.pb( nb + *j ) ;
      FOR0( i, (LL)ans.size() ) cout << ans[i] << endl ;
}


#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define endll puts("")
#define mod 1000000007
#define mp3(x,y,z) mp(x,mp(y,z))
#define pii pair< int , int >
using namespace std;
typedef long long ll;
const int N = 1e3+3;
int n,m,i,j,k,l,s,t,d,u,ii,jj,x,y,z,h[N],hh[N],ata[N],M[N];
int atab(int x)
{
	return ((x==ata[x])?x:ata[x]=atab(ata[x]));
}
int main()
{
	cin>>n>>k;
	for(i=1;i<=n;i++)ata[i]=i;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		if(x){ata[atab(i)]=atab(x);h[x]=i;}
	}
	z=atab(k);
	while(t++,z!=k)z=h[z];
	x=t;
	z=atab(k);
	for(i=1;i<=n;i++)
		if(i!=z and ata[i]==i)
		{
			t=0;j=i;
			while(t++,h[j])j=h[j];
			hh[++s]=t;
		}
	memset(h,0,sizeof h);
//	cout<<x<<' ';
	//for(i=1;i<=s;i++){dp[i][1]=hh[i];if(!h[hh[i]]){h[hh[i]]=1;cout<<x+hh[i]<<endl;}}
	M[0] = 1;
	for(i=1;i<=s;i++)
		for(j=N;j>=0;j--)
			if(M[ j-hh[i] ])
				M[j] = 1;

	for(i=0;i<=n;i++)
		if(M[i])
			cout<<x+i<<endl;
}


#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define endll puts("")
#define mod 1000000007
#define mp3(x,y,z) mp(x,mp(y,z))
#define pii pair< int , int >
using namespace std;
typedef long long ll;
const int N = 1e3+3;
int n,m,i,j,k,l,s,t,d,u,ii,jj,x,y,z,h[N],hh[N],ata[N],M[2*N];
int atab(int x)
{
	return ((x==ata[x])?x:ata[x]=atab(ata[x]));
}
int main()
{
	cin>>n>>k;
	for(i=1;i<=n;i++)ata[i]=i;
	for(i=1;i<=n;i++)
	{
		cin>>x;
		if(x){ata[atab(i)]=atab(x);h[x]=i;}
	}
	z=atab(k);
	while(t++,z!=k)z=h[z];
	x=t;
	z=atab(k);
	for(i=1;i<=n;i++)
		if(i!=z and ata[i]==i)
		{
			t=0;j=i;
			while(t++,h[j])j=h[j];
			hh[++s]=t;
		}
	M[0] = 1;
	for(i=1;i<=s;i++)
		for(j=2*N;j>=hh[i];j--)
			if(M[ j-hh[i] ])
				M[j] = 1;

	for(i=0;i<=n;i++)
		if(M[i])
			cout<<x+i<<endl;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
int groups[1001];
int after[1001];
vector<int> allgroup;
vector<int> combination;
int ng;
set<int> ans;
vector<int> gsize(ng+1);
int getgroup(int i) {
  if(groups[after[i]]!=0) {
    return groups[after[i]];
  } else {
    return getgroup(after[i]);
  }
}
void makeComb(int offset, int k) {
  if (k == 0) {
    int sum=0;
    //cout<<"Got combination: ";
    //for(int i=0;i<combination.size();++i) cout<<gsize[combination[i]]<<" ";
    //cout<<endl;
    for(int i=0;i<combination.size();++i) sum+=gsize[combination[i]];
    ans.insert(sum);
    return;
  }
  for (int i = offset; i <= allgroup.size() - k; ++i) {
    combination.push_back(allgroup[i]);
    makeComb(i+1, k-1);
    combination.pop_back();
  }
}
int main(){
 ng=1;
 for(int i=0;i<=1000;++i) {
   groups[i]=0;
   after[i]=0;
 }
 int n,bobr;
 cin>>n>>bobr;
 cin.get();
 for(int i=1;i<=n;++i) {
   cin>>after[i];
   if(after[i]==0) {
     groups[i]=ng;
     ++ng;
   }
 }
 --ng;
 cin.get();
 for(int i=1;i<=n;++i) {
   if(after[i]!=0) {
     groups[i]=getgroup(i);
   }
 }
 for(int i=1;i<=ng;++i) {
   gsize[i]=0;
 }
 for(int i=1;i<=n;++i) {
   ++gsize[groups[i]];
 }
 //for(int i=1;i<=ng;++i) {
 //  cout<<"Group "<<i<<"  has size: "<<gsize[i]<<endl;
 //}
 int bobrgroup=groups[bobr];
 int bobrqueue=1;
 int par=bobr;
 while(after[par]!=0) {
   ++bobrqueue;
   par=after[par];
 }
 //for(int i=1;i<=n;++i) {
 //  cout<<groups[i]<<" ";
 //}
 //cout<<endl;
 ans.insert(0); // all ans + bobrqueue
 for(int i=1;i<=ng;++i) {
   if(i!=bobrgroup) allgroup.push_back(i);
 }
 for(int i=1;i<=ng-1;++i) {
   makeComb(0,i);
 }
 set<int>::iterator it;
 for(it=ans.begin();it!=ans.end();++it) {
   cout<<(*it)+bobrqueue<<endl;
 }
 return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
int groups[1001];
int after[1001];
vector<int> allgroup;
vector<int> combination;
int ng;
set<int> ans;
vector<int> gsize(ng+1);
int getgroup(int i) {
  if(groups[after[i]]!=0) {
    return groups[after[i]];
  } else {
    return getgroup(after[i]);
  }
}
void makeComb(int offset, int k) {
  if (k == 0) {
    int sum=0;
    //cout<<"Got combination: ";
    //for(int i=0;i<combination.size();++i) cout<<gsize[combination[i]]<<" ";
    //cout<<endl;
    for(int i=0;i<combination.size();++i) sum+=gsize[combination[i]];
    ans.insert(sum);
    return;
  }
  for (int i = offset; i <= allgroup.size() - k; ++i) {
    combination.push_back(allgroup[i]);
    makeComb(i+1, k-1);
    combination.pop_back();
  }
}
int main(){
 ng=1;
 for(int i=0;i<=1000;++i) {
   groups[i]=0;
   after[i]=0;
 }
 int n,bobr;
 cin>>n>>bobr;
 cin.get();
 for(int i=1;i<=n;++i) {
   cin>>after[i];
   if(after[i]==0) {
     groups[i]=ng;
     ++ng;
   }
 }
 --ng;
 cin.get();
 for(int i=1;i<=n;++i) {
   if(after[i]!=0) {
     groups[i]=getgroup(i);
   }
 }
 for(int i=0;i<=ng;++i) {
   gsize[i]=0;
 }
 for(int i=1;i<=n;++i) {
   ++gsize[groups[i]];
 }
 //for(int i=1;i<=ng;++i) {
 //  cout<<"Group "<<i<<"  has size: "<<gsize[i]<<endl;
 //}
 int bobrgroup=groups[bobr];
 int bobrqueue=1;
 int par=bobr;
 while(after[par]!=0) {
   ++bobrqueue;
   par=after[par];
 }
 //for(int i=1;i<=n;++i) {
 //  cout<<groups[i]<<" ";
 //}
 //cout<<endl;
 ans.insert(0); // all ans + bobrqueue
 for(int i=1;i<=ng;++i) {
   if(i!=bobrgroup && gsize[i]>0) allgroup.push_back(i);
 }
 for(int i=1;i<=ng-1;++i) {
   makeComb(0,i);
 }
 set<int>::iterator it;
 for(it=ans.begin();it!=ans.end();++it) {
   cout<<(*it)+bobrqueue<<endl;
 }
 return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
int groups[1001];
int after[1001];
vector<int> allgroup;
vector<int> combination;
int ng;
set<int> ans;
vector<int> gsize(1001);
int getgroup(int i) {
  if(groups[after[i]]!=0) {
    return groups[after[i]];
  } else {
    return getgroup(after[i]);
  }
}
void makeComb(int offset, int k) {
  if (k == 0) {
    int sum=0;
    //cout<<"Got combination: ";
    //for(int i=0;i<combination.size();++i) cout<<gsize[combination[i]]<<" ";
    //cout<<endl;
    for(int i=0;i<combination.size();++i) sum+=gsize[combination[i]];
    ans.insert(sum);
    return;
  }
  for (int i = offset; i <= allgroup.size() - k; ++i) {
    combination.push_back(allgroup[i]);
    makeComb(i+1, k-1);
    combination.pop_back();
  }
}
int main(){
 ng=1;
 for(int i=0;i<=1000;++i) {
   groups[i]=0;
   after[i]=0;
 }
 int n,bobr;
 cin>>n>>bobr;
 cin.get();
 for(int i=1;i<=n;++i) {
   cin>>after[i];
   if(after[i]==0) {
     groups[i]=ng;
     ++ng;
   }
 }
 if(ng>1) --ng;
 cin.get();
 for(int i=1;i<=n;++i) {
   if(after[i]!=0) {
     groups[i]=getgroup(i);
   }
 }
 for(int i=0;i<=ng;++i) {
   gsize[i]=0;
 }
 for(int i=1;i<=n;++i) {
   ++gsize[groups[i]];
 }
 //for(int i=1;i<=ng;++i) {
 //  cout<<"Group "<<i<<"  has size: "<<gsize[i]<<endl;
 //}
 int bobrgroup=groups[bobr];
 int bobrqueue=1;
 int par=bobr;
 while(after[par]!=0) {
   ++bobrqueue;
   par=after[par];
 }
 //for(int i=1;i<=n;++i) {
 //  cout<<groups[i]<<" ";
 //}
 //cout<<endl;
 ans.insert(0); // all ans + bobrqueue
 for(int i=1;i<=ng;++i) {
   if(i!=bobrgroup && gsize[i]>0) allgroup.push_back(i);
 }
 for(int i=1;i<=ng-1;++i) {
   makeComb(0,i);
 }
 set<int>::iterator it;
 for(it=ans.begin();it!=ans.end();++it) {
   cout<<(*it)+bobrqueue<<endl;
 }
 return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

int last[1010],c[1010],n,x;
int a[1010],m;
bool ex[1010];

int main()
{
    //freopen("2.txt","r",stdin);
    int i,j,pos,tt;
    while(~scanf("%d%d",&n,&x))
    {
        //printf("n=%d\n",n);
        //printf("\n");//zhushicihang
        for(i=1;i<=n;i++)
            scanf("%d",&c[i]);
        m=0;
        for(i=0;i<=n;i++)
            last[i]=-1;
        for(i=1;i<=n;i++)
        {
            if(c[i]==0)continue;
            last[c[i]]=i;
        }
        for(i=1;i<=n;i++)
        {
            if(c[i]!=0)continue;
            j=i;
            c[j]=++m;
            int t=1;
            if(x==j)pos=t;
            while(last[j]!=-1)
            {
                j=last[j];
                c[j]=m;
                t++;
                if(x==j)pos=t;
            }
        }
        //for(i=1;i<=n;i++)
        //    printf("%d ",c[i]);
        //printf("\n");
        //printf("x=%d pos=%d\n",x,pos);
        for(i=0;i<=m;i++)
            a[i]=0;
        for(i=1;i<=n;i++)
            a[c[i]]++;
        for(i=0;i<=1000;i++)
            ex[i]=0;
        ex[0]=1;
        for(j=1;j<=m;j++)
        {
            if(j==c[x])continue;
            for(i=n;i>=1;i--)
            {
                if(i-a[j]<0)break;
                ex[i]|=ex[i-a[j]];
                //if(ex[i])printf("j=%d %d\n",j,i);
            }
        }
        for(i=0;i<=n;i++)
            if(ex[i])printf("%d\n",i+pos);
    }
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

typedef signed long long ll;
typedef unsigned long long ull;

#define _PE(...) printf(__VA_ARGS__); fprintf(stderr, __VA_ARGS__);
#define _E(...) fprintf(stderr, __VA_ARGS__)
#undef _P
#define _P(...) printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define FOR2(x,from,to) for(x=from;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define ZERO2(a,b) memset(a,0,b)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
#define GETs(x) scanf("%s",x);
int GETi() { int i;scanf("%d",&i); return i;}
#define GET1(x) scanf("%d",x);
#define GET2(x,y) scanf("%d%d",x,y);
#define GET3(x,y,z) scanf("%d%d%d",x,y,z);

#define EPS (1e-11)
template <class T> T sqr(T val){ return val*val;}

//-------------------------------------------------------

int N,X;
int A[1001],B[1001],vis[1001],bit[1001];

void solve() {
    int f,r,i,j,k,l, x,y;
    int q=0;

    cin>>N>>X;
    FOR(i,N) {
        cin>>A[i+1];
        B[A[i+1]]=i+1;
    }

    x=1;y=X;
    vis[y]++;
    while(A[y]!=0) {
        x++;
        y=A[y];
        vis[y]++;
    }

    bit[x]=1;
    FOR(i,N) {
        if(A[i+1]!=0 || vis[i+1]) continue;
        x=1;y=i+1;
        while(B[y]!=0) x++,y=B[y];
        for(y=N-x;y>=0;y--) if(bit[y]) bit[y+x]++;
    }

    FOR(i,N+1) if(bit[i]) _P("%d\n",i);
    return;
}

int main(int argc,char** argv){
    if(argc>1) freopen(argv[1], "r", stdin);
    solve();
    return 0;
}


#include <cstdlib>
#include <cstring>
#include <memory>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

typedef signed long long ll;
typedef unsigned long long ull;

#define _PE(...) printf(__VA_ARGS__); fprintf(stderr, __VA_ARGS__);
#define _E(...) fprintf(stderr, __VA_ARGS__)
#undef _P
#define _P(...) printf(__VA_ARGS__)

#define FOR(x,to) for(x=0;x<to;x++)
#define FOR2(x,from,to) for(x=from;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
void _fill_int(int* p,int val,int rep) {int i;  FOR(i,rep) p[i]=val;}
#define FILL_INT(a,val) _fill_int((int*)a,val,sizeof(a)/4)
#define ZERO2(a,b) memset(a,0,b)
#define MINUS(a) _fill_int((int*)a,-1,sizeof(a)/4)
#define GETs(x) scanf("%s",x);
int GETi() { int i;scanf("%d",&i); return i;}
#define GET1(x) scanf("%d",x);
#define GET2(x,y) scanf("%d%d",x,y);
#define GET3(x,y,z) scanf("%d%d%d",x,y,z);

#define EPS (1e-11)
template <class T> T sqr(T val){ return val*val;}

//-------------------------------------------------------

int N,X;
int A[1001],B[1001],vis[1001],bit[1001];

void solve() {
    int f,r,i,j,k,l, x,y;
    int q=0;

    cin>>N>>X;
    FOR(i,N) {
        cin>>A[i+1];
        B[A[i+1]]=i+1;
    }

    x=1;y=X;
    vis[y]++;
    while(A[y]!=0) {
        x++;
        y=A[y];
        vis[y]++;
    }

    bit[x]=1;
    FOR(i,N) {
        if(B[i+1]!=0 || vis[i+1]) continue;
        x=1;y=i+1;
        while(A[y]!=0) x++,y=A[y];
        for(y=N-x;y>=0;y--) if(bit[y]) bit[y+x]++;
    }

    FOR(i,N+1) if(bit[i]) _P("%d\n",i);
    return;
}

int main(int argc,char** argv){
    if(argc>1) freopen(argv[1], "r", stdin);
    solve();
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

vector<int> g[1001];
int tot,head[1001],next[1001];
bool vis[1001],dp[1001];
void dfs(int x){
	vis[x]=true;
	if(head[x]!=0) dfs(head[x]);
	g[tot].push_back(x);
}
int find(int x){
	if(next[x]==x) return x;
	else return next[x]=find(next[x]);
}

int main()
{
	int n,v,num,sta;
	cin>>n>>v;
	for(int i=1;i<=n;i++)
	   cin>>head[i];
	for(int i=1;i<=n;i++)
	   next[i]=i;
	for(int i=1;i<=n;i++)
		if(head[i]!=0)
	   		next[find(head[i])]=find(i);
	for(int i=1;i<=n;i++)
	   if(next[i]==i){
	   	 //cout<<i<<endl;
	   	 tot++;
	   	 dfs(i);
	   }
	bool isok=false;
	for(int i=1;i<=tot;i++){
		if(isok) break;
	    for(int j=0;j<g[i].size();j++)
	        if(g[i][j]==v){
	        	num=i;sta=j+1;isok=true;break;
	        }
	}
	dp[0]=true;
	for(int j=1;j<=tot;j++)
		for(int i=n;i>=1;i--)
		   if(j!=num&&dp[i-g[j].size()]) dp[i]=true;
	for(int i=0;i<=n;i++)
	   if(dp[i])
	   	  cout<<i+sta<<endl;
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

vector<int> g[1001];
int tot,head[1001],next[1001];
bool vis[1001],dp[1001];
void dfs(int x){
	vis[x]=true;
	if(head[x]!=0) dfs(head[x]);
	g[tot].push_back(x);
}
int find(int x){
	if(next[x]==x) return x;
	else return next[x]=find(next[x]);
}

int main()
{
	int n,v,num,sta;
	cin>>n>>v;
	for(int i=1;i<=n;i++)
	   cin>>head[i];
	for(int i=1;i<=n;i++)
	   next[i]=i;
	for(int i=1;i<=n;i++)
		if(head[i]!=0)
	   		next[find(head[i])]=find(i);
	for(int i=1;i<=n;i++)
	   if(next[i]==i){
	   	 //cout<<i<<endl;
	   	 tot++;
	   	 dfs(i);
	   }
	bool isok=false;
	for(int i=1;i<=tot;i++){
		if(isok) break;
	    for(int j=0;j<g[i].size();j++)
	        if(g[i][j]==v){
	        	num=i;sta=j+1;isok=true;break;
	        }
	}
	dp[0]=true;
	for(int j=1;j<=tot;j++)
		for(int i=n;i>=1;i--){
			if(i<g[j].size()) break;
		    if(j!=num&&dp[i-g[j].size()]) dp[i]=true;
		}
	for(int i=0;i<=n;i++)
	   if(dp[i])
	   	  cout<<i+sta<<endl;
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;

int func(int A[], int n, int sum)
{
      if(sum<0)
      return 0;
   int curr_sum = A[0], start = 0, i;
    for (i = 1; i <=n; i++)
    {
        while (curr_sum>sum && start < i-1)
        {
            curr_sum=curr_sum - A[start];
            start++;
        }
      if (curr_sum == sum)
            return 1;

        if (i < n)
          curr_sum = curr_sum + A[i];
    }
    return 0;
}
int main()
{
      int n,k;
      cin>>n>>k;
      int count=0;
      int arr[1031]={},pos[1031]={}, num[1031]={};
      for(int i=0;i<n;i++)
      {
            cin>>pos[i];
            arr[pos[i]]=i+1;
      }
      int dif=0,po=1,d=0;
      for(int i=0;i<n;i++)
      {
            int f=0;
            if(pos[i]==0)
            {
                  int t=0,cur=i+1,j=1;
                  if(k==cur)
                  po=j,f=1;
                  while(arr[cur]!=0)
                  {
                        t++,j++;
                        cur=arr[cur];
                        if(f==0  && cur==k)
                        po=j,f=1;
                  }
                  t++;
                  if(f==0)
                  num[count++]=t;
            }
      }
    // sort(num,num+count);
      for(int i=1;i<=n;i++)
      {
            if((i)==po||func(num,count,i-po))
            cout<<i<<endl;
      }

}


#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
      int n,k;
      cin>>n>>k;
      int count=0;
      int arr[1031]={},pos[1031]={}, num[1031]={};
      for(int i=0;i<n;i++)
      {
            cin>>pos[i];
            arr[pos[i]]=i+1;
      }
      int dif=0,po=1,d=0;
      for(int i=0;i<n;i++)
      {
            int f=0;
            if(pos[i]==0)
            {
                  int t=0,cur=i+1,j=1;
                  if(k==cur)
                  po=j,f=1;
         //         cout<<"strara   "<<cur<<endl;
                  while(arr[cur]!=0)
                  {
                        t++,j++;
                        cur=arr[cur];
                        if(f==0  && cur==k)
                        po=j,f=1;
                  }
                  t++;
                  if(f==0)
                  num[count++]=t;
            }
      }
       bool subset[n+1][count+1];

    // If sum is 0, then answer is true
    for (int i = 0; i <= count; i++)
      subset[0][i] = true;

    // If sum is not 0 and set is empty, then answer is false
    for (int i = 1; i <=n; i++)
      subset[i][0] = false;

     for (int i=1; i<=n; i++)
     {
       for (int j=1;j<=count; j++)
       {
         subset[i][j] = subset[i][j-1];
         if (i >= num[j-1])
           subset[i][j] = subset[i][j] || subset[i - num[j-1]][j-1];
       }
     }

      for(int i=1;i<=n;i++)
      {

            if(i-po>=0 && subset[i-po][count])
            cout<<i<<endl;
      }

}


#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;

int func(int A[], int n, int sum)
{
   int curr_sum = A[0], start = 0, i;
   //  if (curr_sum == sum)
     //       return 1;
    for (i = 1; i <=n; i++)
    {
        while (curr_sum > sum && start < i-1)
        {
            curr_sum = curr_sum - A[start];
            start++;
        }
      if (curr_sum == sum)
            return 1;

        if (i < n)
          curr_sum = curr_sum + A[i];
    }
    return 0;
}
int main()
{
      int n,k;
      cin>>n>>k;

      int count=0;
      int arr[1001]={},pos[1001]={}, num[1001]={};
      for(int i=0;i<n;i++)
      {
            cin>>pos[i];
            arr[pos[i]]=i+1;
      }
      int dif=0,po=1,d=0;
      for(int i=0;i<n;i++)
      {
            int f=0;
            if(pos[i]==0)
            {
                  int t=0,cur=i+1,j=1;
                 // cout<<"dddd  "<<cur<<"  ";
                  if(k==cur)
                  po=j,f=1;
                  while(arr[cur]!=0)
                  {
                   //     cout<<cur<<"   ";
                        t++,j++;
                        cur=arr[cur];
                        if(cur==k)
                        po=j,f=1;
                  }
                  t++;

                  if(f==0)
                  num[count++]=t;
                  else
                  d=t;
            }
      }
     sort(num,num+count);
      for(int i=0;i<n;i++)
      {
            if((i+1)==po||func(num,count,i+1-po))
            cout<<i+1<<endl;
      }

}


#include <cstring>
#include <cstdio>

#define Maxn 1005

char v[Maxn], f[Maxn];
long n, m, x, a[Maxn], b[Maxn];

long gtot = 0;
class gnode {
public:
    long v;
    gnode *next;
    gnode(long V, gnode *ne) : v(V), next(ne) {}
    gnode() {}
    ~gnode() {}
} *g[Maxn], G[Maxn << 1];

void add(long x, long y) {
    g[x] = &(G[gtot++] = gnode(y, g[x]));
    g[y] = &(G[gtot++] = gnode(x, g[y]));
}

void find(long i) {
    v[i] = 1;
    ++b[m];
    for (gnode *e = g[i]; e; e = e->next)
        if (!v[e->v]) {
            v[e->v] = 1;
            find(e->v);
        }
}

int main() {
    static long y, i, j;
    scanf("%ld%ld", &n, &x);
    for (i = 1; i <= n; ++i) {
        scanf("%ld", a + i);
        if (a[i])
            add(a[i], i);
    }
    y = 0;
    memset(v, 0, sizeof v);
    for (i = x; i; i = a[i]) {
        ++y;
        v[i] = 1;
    }
    find(x);
    m = 0;
    for (i = n; i >= 1; --i) if (!v[i]) {
        b[++m] = 0;
        find(i);
    }
    memset(f, 0, sizeof f);
    f[0] = 1;
    for (i = 1; i <= m; ++i)
        for (j = n; j >= 1; --j)
            if (j >= b[i])
                f[j] |= f[j - b[i]];
    for (i = 0; i <= n; ++i)
        if (f[i])
            printf("%ld\n", i + y);
    return 0;
}


#include <cstring>
#include <cstdio>

#define Maxn 1005

char v[Maxn], f[Maxn];
long n, m, x, a[Maxn], b[Maxn];

long gtot = 0;
class gnode {
public:
    long v;
    gnode *next;
    gnode(long V, gnode *ne) : v(V), next(ne) {}
    gnode() {}
    ~gnode() {}
} *g[Maxn], G[Maxn];

void add(long x, long y) {
    g[x] = &(G[gtot++] = gnode(y, g[x]));
    g[y] = &(G[gtot++] = gnode(x, g[y]));
}

void find(long i) {
    v[i] = 1;
    ++b[m];
    for (gnode *e = g[i]; e; e = e->next)
        if (!v[e->v]) {
            v[e->v] = 1;
            find(e->v);
        }
}

int main() {
    static long y, i, j;
    scanf("%ld%ld", &n, &x);
    for (i = 1; i <= n; ++i) {
        scanf("%ld", a + i);
        if (a[i])
            add(a[i], i);
    }
    y = 0;
    memset(v, 0, sizeof v);
    for (i = x; i; i = a[i]) {
        ++y;
        v[i] = 1;
    }
    find(x);
    m = 0;
    for (i = n; i >= 1; --i) if (!v[i]) {
        b[++m] = 0;
        find(i);
    }
    memset(f, 0, sizeof f);
    f[0] = 1;
    for (i = 1; i <= m; ++i)
        for (j = n; j >= 1; --j)
            f[j] |= f[j - b[i]];
    for (i = 0; i <= n; ++i)
        if (f[i])
            printf("%ld\n", i + y);
    return 0;
}


#include <cstring>
#include <cstdio>

#define Maxn 1005

char v[Maxn], f[Maxn];
long n, m, x, a[Maxn], b[Maxn];

long gtot = 0;
class gnode {
public:
    long v;
    gnode *next;
    gnode(long V, gnode *ne) : v(V), next(ne) {}
    gnode() {}
    ~gnode() {}
} *g[Maxn], G[Maxn];

void add(long x, long y) {
    g[x] = &(G[gtot++] = gnode(y, g[x]));
    g[y] = &(G[gtot++] = gnode(x, g[y]));
}

void find(long i) {
    v[i] = 1;
    ++b[m];
    for (gnode *e = g[i]; e; e = e->next)
        if (!v[e->v]) {
            v[e->v] = 1;
            find(e->v);
        }
}

int main() {
    static long y, i, j;
    scanf("%ld%ld", &n, &x);
    for (i = 1; i <= n; ++i) {
        scanf("%ld", a + i);
        if (a[i])
            add(a[i], i);
    }
    y = 0;
    memset(v, 0, sizeof v);
    for (i = x; i; i = a[i]) {
        ++y;
        v[i] = 1;
    }
    find(x);
    m = 0;
    for (i = n; i >= 1; --i) if (!v[i]) {
        b[++m] = 0;
        find(i);
    }
    memset(f, 0, sizeof f);
    f[0] = 1;
    for (i = 1; i <= m; ++i)
        for (j = n; j >= 1; --j)
            if (j >= b[i])
                f[j] |= f[j - b[i]];
    for (i = 0; i <= n; ++i)
        if (f[i])
            printf("%ld\n", i + y);
    return 0;
}


#include "math.h"
#include "iostream"
//#include "fstream"
#include "algorithm"
#include "string"
#include "vector"
#include "map"
//#include "map"
#pragma comment(linker, "/STACK:94777216")
using namespace std;
int v[1001],vv=0,n,k;
int ms[1001];
int m[1001];
int din[1001];
int g,b=-1;
int dfs(int t,int h)
{
    if(t==k-1)
    {
         g=h;
         b=vv;
    }
if(m[t+1]==0)
{
    if(b==vv)
    {
        //g=h-g;
        b=-1;
        return 0;
    }
    v[vv]=h;
    vv++;
    return 0;
}
dfs(m[t+1]-1,h+1);
}
int main()
{
      cin>>n>>k;
     for(int i=0;i<n;i++)
     {
      cin>>ms[i];
      if(ms[i]!=0)
      m[ms[i]]=i+1;
     }
     for(int i=0;i<n;i++)
         if(ms[i]==0)
         dfs(i,1);
     if(g==0)
         g=1;
     din[g]=1;
         for(int j=0;j<vv;j++)
             for(int i=n;i>=g;i--)
             if(din[i]==1)
             {
                 din[i+v[j]]=1;
             }
             for(int i=0;i<=n;i++)
                 if(din[i]==1)
                     cout<<i<<endl;
    return 0;
}


#include "math.h"
#include "iostream"
//#include "fstream"
#include "algorithm"
#include "string"
#include "vector"
#include "map"
//#include "map"
#pragma comment(linker, "/STACK:94777216")
using namespace std;
int v[1000],vv=0,n,k;
int ms[1000];
int m[1000];
int din[1000];
string st[1000];
int g,b=-1;
int dfs(int t,int h)
{
    if(t==k-1)
    {
         g=h;
         b=vv;
    }
if(m[t+1]==0)
{
    if(b==vv)
    {
        //g=h-g;
        b=-1;
        return 0;
    }
    v[vv]=h;
    vv++;
    return 0;
}
dfs(m[t+1]-1,h+1);
}
int main()
{
      cin>>n>>k;
     for(int i=0;i<n;i++)
     {
      cin>>ms[i];
      if(ms[i]!=0)
      m[ms[i]]=i+1;
     }
     for(int i=0;i<n;i++)
         if(ms[i]==0)
         dfs(i,1);
     if(g==0)
         g=1;
     din[g]=1;
     st[g].assign(vv,0);
     for(int i=0;i<vv;i++)
             st[g][i]='1';
     for(int i=g+1;i<=n;i++)
         for(int j=0;j<vv;j++)
             if((i-v[j]>0)&&(din[i-v[j]]==1)&&(st[i-v[j]][j]=='1'))
             {
                 din[i]=1;
                 st[i]=st[i-v[j]];
                 st[i][j]='0';
                 break;
             }
             for(int i=0;i<=n;i++)
                 if(din[i]==1)
                     cout<<i<<endl;
    return 0;
}


#include<iostream>
#include<cctype>
using namespace std;

int sig[1004],l=0, strt[1004],tama[1004];
bool esta[1004],lugar[1004];
int main(){
    int N,X,tam,per,A=0,B=-1;
    cin >> N >> X;
    for(int i=0;i<=N;i++) esta[i]=true;
    for(int i=1;i<=N;i++){
        cin >> sig[i];
        esta[sig[i]]=false;
    }
    for(int i=1;i<=N;i++){
        if(esta[i]){
            strt[l]=per=i;
            tam=0;
            while(per!=0){
                if(per==X){
                    A=tam;
                    B=l;
                }
                per=sig[per];
                tam++;
            }
            tama[l++]=tam;
        }
    }
    lugar[(B>=0)? tama[B]-A:1]=true;
    for(int i=1;i<=l;i++){
        for(int j=N-tama[i];j>0;j--) if(lugar[j]) lugar[j+tama[i]]=true;
    }
    for(int i=1;i<=N;i++){
        if(lugar[i]) cout << i<< endl;
    }
}


#include<iostream>
#include<cctype>
using namespace std;

int sig[1004],l=0, strt[1004],tama[1004];
bool esta[1004],lugar[1004];
int main(){
    int N,X,tam,per,A,B;
    cin >> N >> X;
    for(int i=0;i<=N;i++) esta[i]=true;
    for(int i=1;i<=N;i++){
        cin >> sig[i];
        esta[sig[i]]=false;
    }
    for(int i=1;i<=N;i++){
        if(esta[i]){
            strt[l]=per=i;
            tam=0;
            while(per!=0){
                if(per==X){
                    A=tam;
                    B=l;
                }
                per=sig[per];
                tam++;
            }
            tama[l++]=tam;
        }
    }
    lugar[tama[B]-A]=true;
    for(int i=1;i<=l;i++){
        for(int j=1;j<=N-tama[i];j++) if(lugar[j]) lugar[j+tama[i]]=true;
    }
    for(int i=1;i<=N;i++){
        if(lugar[i]) cout << i<< endl;
    }
}


#include<iostream>
#include<cctype>
using namespace std;

int sig[1004],l=0, strt[1004],tama[1004];
bool esta[1004],lugar[1004];
int main(){
    int N,X,tam,per,A=0,B=-1;
    cin >> N >> X;
    for(int i=0;i<=N;i++) esta[i]=true;
    for(int i=1;i<=N;i++){
        cin >> sig[i];
        esta[sig[i]]=false;
    }
    for(int i=1;i<=N;i++){
        if(esta[i]){
            strt[l]=per=i;
            tam=0;
            while(per!=0){
                if(per==X){
                    A=tam;
                    B=l;
                }
                per=sig[per];
                tam++;
            }
            tama[l++]=tam;
        }
    }
    lugar[(B>=0)? tama[B]-A:1]=true;
    for(int i=0;i<l;i++){
        if(i==B) continue;
        for(int j=N-tama[i];j>0;j--) if(lugar[j]) lugar[j+tama[i]]=true;
    }
    for(int i=1;i<=N;i++){
        if(lugar[i]) cout << i<< endl;
    }
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n, false);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  vector<int> pos(n, false);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= 0; j--) {
      if(j - chunks[i] >= 0)
        pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  vector<int> pos(n);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= 0; j--) {
      pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n, false);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  vector<int> pos(n, false);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    cout << chunks[i] << ' ' ;
  }
  cout << '\n';
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= 0; j--) {
      pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n, false);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  vector<int> pos(n, false);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= 0; j--) {
      pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n, false);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  vector<int> pos(n, false);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= chunks[i]; j--) {
        pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> before(n, -1);
  vector<int> after(n, -1);
  vector<int> visited(n);
  vector<int> chunks;
  int bpos = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if(a >= 0)
      after[a] = i;
    before[i] = a;
  }
  for(int i = 0; i < n; i++) {
    if(!visited[i]) {
      int cur = i;
      while(before[cur] != -1) {
        cur = before[cur];
      }
      int count = 0;
      bool beaver = false;
      while(cur != -1) {
        visited[cur] = true;
        if(cur == x) {
          beaver = true;
          bpos = count;
        }
        cur = after[cur];
        count++;
      }
      if(!beaver) {
        chunks.push_back(count);
      }
    }
  }
  cout << '\n';
  vector<int> pos(n);
  pos[0] = true;
  for(int i = 0; i < chunks.size(); i++) {
    for(int j = n-1; j >= 0; j--) {
      pos[j] = pos[j] || pos[j - chunks[i]];
    }
  }
  for(int i = 0; i < n; i++) {
    if(pos[i])
      cout << i+bpos+1 << '\n';
  }
  return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
if(f[x]==0)return x;
int tmp=getf(f[x]);
p[x]=p[f[x]]+1;
return f[x]=tmp;
}
int main()
{
scanf("%d%d",&n,&x);
for(int i=1;i<=n;++i)
{
int y;
scanf("%d",&y);
f[i]=y;
}
for(int i=1;i<=n;++i)
{
int t=getf(i);
if(p[i]+1>a[t]) a[t]=p[i]+1;
}
a[getf(x)]=0;
int ans=p[x]+1;
dp[0]=1;
for(int i=1;i<=n;++i)
if(a[i])
for(int j=n-1;j-a[i]>=0;--j)
if(dp[j-a[i]])dp[j]=1;
for(int i=0;i<n;++i)if(dp[i] && ans+i<=n)
printf("%d\n",ans+i);
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s)-1; i>=(e); i--)
#define CLR(a,x) memset(a, x, sizeof(a))
#define EXP(i,l) for (int i=(l); i; i=qn[i])
#define N 1005
using namespace std;
int n, m, a[N], nob[N], off, dp[N];
int main()
{
scanf("%d%d", &n, &m);
FOE(i,1,n) scanf("%d", &a[i]), nob[a[i]] = 1;
dp[0] = 1;
FOE(i,1,n) if (!nob[i])
{
int cnt = 0, x = i, f = 0;
while (x)
{
if (f) ++off;
if (x == m) f = 1;
++cnt;
x = a[x];
}
if (!f)
{
for (int i=n; i>=cnt; i--) dp[i] |= dp[i - cnt];
}
}
FOE(i,0,n) if (dp[i]) printf("%d\n", i + off + 1);
}


#include <cstdio>
#include <cctype>
#include <set>
using namespace std;
int n, x, m, a[1001], b[1001], c[1001];
bool d[1001] = {true, true};
set<int> s;
int main()
{
scanf("%d%d", &n, &x);
for (int i(1); i <= n; ++i)
{
scanf("%d", a + i);
if (a[i])
b[a[i]] = i;
else
s.insert(i);
}
while (a[x])
x = a[x], ++m;
s.erase(x);
for (set<int>::iterator i(s.begin()); i != s.end(); ++i)
{
int k(1);
for (int j(*i); b[j]; j = b[j])
++k;
++c[k];
}
for (int i(1); i <= n; ++i)
for (int j(1); j <= c[i]; ++j)
for (int k(n - j * i); k >= 1; --k)
if (d[k])
d[k + j * i] = true;
for (int i(1); i + m <= n; ++i)
if (d[i])
printf("%d\n", i + m);
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff

int g[1010];
int mark[2010];
int save[1010];
int bin[1100];
int num[110];

int find(int x)
{
    if(x==bin[x]) return x;
    return bin[x]=find(bin[x]);
}

int main()
{
    //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
    //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
    int n,x;
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&g[i]);
    }
    for(int i=0;i<=n;i++)
    {
        num[i]=1;
        bin[i]=i;
    }
    int flag=0;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(g[i]!=0)
        {
            int a=find(i);
            int b=find(g[i]);
            bin[a]=b;
            num[b]=num[a]+num[b];
            num[a]=0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(bin[i]==i && find(x) != i)
        {
            save[cnt++]=num[i];
        }
    }
    /*
    printf("%d\n",cnt);
    for(int i=0;i<cnt;i++)
        printf("%d\n",save[i]);
    printf("#################");*/
    memset(mark,0,sizeof(mark));
    mark[0]=1;
    for(int i=0;i<cnt;i++)
    {
        for(int j=1000;j>=0;j--)
        {
            if(mark[j]==1)
            {
                mark[j+save[i]]=1;
            }
        }
    }
    int sum=0;
    while(g[x])
    {
        sum++;
        x=g[x];
    }
    for(int i=0;i<=1000;i++)
        if(mark[i]==1)
            printf("%d\n",sum+i+1);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff

int g[1010];
int mark[2010];
int save[1010];
int bin[1100];
int num[1100];

int find(int x)
{
    if(x==bin[x]) return x;
    return bin[x]=find(bin[x]);
}

int main()
{
    //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
    //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
    int n,x;
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&g[i]);
    }
    for(int i=0;i<=n;i++)
    {
        num[i]=1;
        bin[i]=i;
    }
    int flag=0;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(g[i]!=0)
        {
            int a=find(i);
            int b=find(g[i]);
            bin[a]=b;
            num[b]=num[a]+num[b];
            num[a]=0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(bin[i]==i && find(x) != i)
        {
            save[cnt++]=num[i];
        }
    }
    /*
    printf("%d\n",cnt);
    for(int i=0;i<cnt;i++)
        printf("%d\n",save[i]);
    printf("#################");*/
    memset(mark,0,sizeof(mark));
    mark[0]=1;
    for(int i=0;i<cnt;i++)
    {
        for(int j=1000;j>=0;j--)
        {
            if(mark[j]==1)
            {
                mark[ j+save[i] ]=1;
            }
        }
    }
    int sum=0;
    while(g[x])
    {
        sum++;
        x=g[x];
    }
    for(int i=0;i<=n;i++)
        if(mark[i]==1)
            printf("%d\n",sum+i+1);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <list>
#include <stack>
using namespace std ;
const int lim = 105 ;

int N, K ;
int sig[lim], ant[lim], prof[lim] ;
int dp[lim] ;
bool mrk[lim] ;

int dfs( int pos ) {
    if( pos==0 )
        return 0 ;
    if( prof[pos] )
        return prof[pos] ;
    prof[pos] = dfs(sig[pos]) +1 ;
    mrk[pos] = mrk[sig[pos]] ;
    if( pos==K )
        mrk[pos] = 1 ;
    return prof[pos] ;
}

int main() {
    scanf("%d%d", &N, &K ) ;
    for( int i=1; i<=N; i++ )
        scanf("%d", &sig[i] ) ;

    for( int i=1; i<=N; i++ ) {
        ant[sig[i]] = i ;
        dfs(i) ;
    }

    dp[0] = 1 ;
    for( int i=1; i<=N; i++ ) {
        if( ant[i] || mrk[i] )
            continue ;
        for( int j=N; j>=prof[i]; j-- )
            dp[j] |= dp[j-prof[i]] ;
    }

    for( int i=0; i<=N; i++ ) {
        if( dp[i] && i+prof[K]<=N )
            printf("%d\n", i+prof[K] ) ;
    }

    return 0 ;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <list>
#include <stack>
using namespace std ;
const int lim = 1005 ;

int N, K ;
int sig[lim], ant[lim], prof[lim] ;
int dp[lim] ;
bool mrk[lim] ;

int dfs( int pos ) {
    if( pos==0 )
        return 0 ;
    if( prof[pos] )
        return prof[pos] ;
    prof[pos] = dfs(sig[pos]) +1 ;
    mrk[pos] = mrk[sig[pos]] ;
    if( pos==K )
        mrk[pos] = 1 ;
    return prof[pos] ;
}

int main() {
    scanf("%d%d", &N, &K ) ;
    for( int i=1; i<=N; i++ )
        scanf("%d", &sig[i] ) ;

    for( int i=1; i<=N; i++ ) {
        ant[sig[i]] = i ;
        dfs(i) ;
    }

    dp[0] = 1 ;
    for( int i=1; i<=N; i++ ) {
        if( ant[i] || mrk[i] )
            continue ;
        for( int j=N; j>=prof[i]; j-- )
            dp[j] |= dp[j-prof[i]] ;
    }

    for( int i=0; i<=N; i++ ) {
        if( dp[i] && i+prof[K]<=N )
            printf("%d\n", i+prof[K] ) ;
    }

    return 0 ;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int d[maxn];
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    if(!d[x])
    {
        p[x]=p[f[x]]+1;
        d[x]=1;
    }
    return f[x]=tmp;
}
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    int tmp=a[getf(x)];
    a[getf(x)]=0;
    int ans=p[x]+1;
    dp[0]=1;
    for(int i=1;i<=n;++i)
        if(a[i])
        for(int j=n-tmp;j-a[i]>=0;--j)
            if(dp[j-a[i]])dp[j]=1;
    for(int i=0;i<=n-tmp;++i)if(dp[i])printf("%d\n",ans+i);
    return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    p[x]=p[f[x]]+1;
    return f[x]=tmp;
}
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    int tmp=a[getf(x)];
    a[getf(x)]=0;
    int ans=p[x]+1;
    dp[0]=1;
    for(int i=1;i<=n;++i)
        if(a[i])
        for(int j=n-tmp;j-a[i]>=0;--j)
            if(dp[j-a[i]])dp[j]=1;
    for(int i=0;i<=n-tmp;++i)if(dp[i])printf("%d\n",ans+i);
    return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    p[x]=p[f[x]]+1;
    return f[x]=tmp;

}
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    a[getf(x)]=0;
    int ans=p[x];
    for(int i=1;i<=n;++i)
        if(a[i]!=0)b[m++]=a[i];
    //for(int i=1;i<=n;++i)printf("%d\n",a[i]);
    dp[0]=1;
    for(int i=0;i<m;++i)
        for(int j=n;j-b[i]>=0;--j)
            if(dp[j-b[i]])dp[j]=1;
    for(int i=0;i<n;++i)if(dp[i])printf("%d\n",ans+i+1);
    return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    p[x]=p[f[x]]+1;
    return f[x]=tmp;

}
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    a[getf(x)]=0;
    int ans=p[x];
    for(int i=1;i<=n;++i)
        if(a[i]!=0)b[m++]=a[i];
    //for(int i=1;i<=n;++i)printf("%d\n",a[i]);
    dp[0]=1;
    for(int i=0;i<m;++i)
        for(int j=n;j-b[i]>=0;--j)
            if(dp[j-b[i]])dp[j]=1;
    for(int i=0;i<=n;++i)if(dp[i] && ans+i+1<=n)printf("%d\n",ans+i+1);
    return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    p[x]=p[f[x]]+1;
    return f[x]=tmp;

}
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    a[getf(x)]=0;
    int ans=p[x];
    for(int i=1;i<=n;++i)
        if(a[i]!=0)b[m++]=a[i];
    //for(int i=1;i<=n;++i)printf("%d\n",a[i]);
    dp[0]=1;
    for(int i=0;i<m;++i)
        for(int j=n;j-b[i]>=0;--j)
            if(dp[j-b[i]])dp[j]=1;
    for(int i=0;i<=n;++i)if(dp[i])printf("%d\n",ans+i+1);
    return 0;
}


#include <cstdio>
#include <algorithm>
#define maxn 1008
int a[maxn],f[maxn],dp[maxn],p[maxn],b[maxn],m;
int n,x;
int getf(int x)
{
    if(f[x]==0)return x;
    int tmp=getf(f[x]);
    p[x]=p[f[x]]+1;
    return f[x]=tmp;
}
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;++i)
    {
        int y;
        scanf("%d",&y);
        f[i]=y;
    }
    for(int i=1;i<=n;++i)
    {
       int t=getf(i);
       if(p[i]+1>a[t]) a[t]=p[i]+1;
    }
    a[getf(x)]=0;
    int ans=p[x]+1;
    dp[0]=1;
    for(int i=1;i<=n;++i)
        if(a[i])
        for(int j=n-1;j-a[i]>=0;--j)
            if(dp[j-a[i]])dp[j]=1;
    for(int i=0;i<n;++i)if(dp[i] && ans+i<=n)printf("%d\n",ans+i);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

int following[1005];
int queueLength[1005];
int okQueue[1005];

int main(){
	int n, k;
	cin >> n >> k;
	int head = k, shift = 1, tmp;
	for (int i=1; i<=n; i++){
		cin >> following[i];
		queueLength[i]++;
		tmp = i;
		while(following[tmp] != 0) {
			queueLength[following[tmp]] += queueLength[i];
			tmp = following[tmp];
		}
	}
	while(following[head]){
		head = following[head];
		shift++;
	}
	vector<int> queues;
	for (int i=1; i<=n; i++) {
		if (following[i] == 0 && i != head){
			// not following, head
			queues.push_back(queueLength[i]);
			//printf("xdd: %d\n", queueLength[i]);
		}
	}
	sort(queues.begin(), queues.end());
	okQueue[0] = 1;
	for (unsigned i=0; i<queues.size(); i++) {
		for (int j=n - queues[i]; j>=0; j--) {
			if (okQueue[j]){
				okQueue[j+queues[i]] = 1;
				//printf("zzz: %d by %d\n", j+queues[i], i);
			}
		}
	}
	for (int i=0; i<n; i++){
		if (okQueue[i])
			printf("%d\n", i+shift);
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int following[1005];
int queueLength[1005];
int okQueue[1005];

int main(){
	int n, k;
	cin >> n >> k;
	int head = k, shift = 1, tmp;
	for (int i=1; i<=n; i++){
		cin >> following[i];
		queueLength[i]++;
		tmp = i;
		while(following[tmp] != 0) {
			queueLength[following[tmp]] += queueLength[i];
			tmp = following[tmp];
		}
	}
	while(following[head]){
		head = following[head];
		shift++;
	}
	vector<int> queues;
	for (int i=1; i<=n; i++) {
		if (following[i] == 0 && i != head){
			// not following, head
			queues.push_back(queueLength[i]);
			//printf("xdd: %d\n", queueLength[i]);
		}
	}
	sort(queues.begin(), queues.end());
	okQueue[0] = 1;
	for (int i=0; i<queues.size(); i++) {
		for (int j=n - queues[i]; j>=0; j--) {
			if (okQueue[j]){
				okQueue[j+queues[i]] = 1;
				//printf("zzz: %d by %d\n", j+queues[i], i);
			}
		}
	}
	for (int i=0; i<n; i++){
		if (okQueue[i])
			printf("%d\n", i+shift);
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int following[1005];
int queueLength[1005];
int okQueue[1005];

int main(){
	int n, k;
	cin >> n >> k;
	int head = k, shift = 1, tmp;
	for (int i=1; i<=n; i++){
		cin >> following[i];
		queueLength[i]++;
		tmp = i;
		while(following[tmp] != 0) {
			queueLength[following[tmp]] += queueLength[i];
			tmp = following[tmp];
		}
	}
	while(following[head]){
		head = following[head];
		shift++;
	}
	vector<int> queues;
	for (int i=1; i<=n; i++) {
		if (following[i] == 0 && i != head){
			// not following, head
			queues.push_back(queueLength[i]);
			//printf("xdd: %d\n", queueLength[i]);
		}
	}
	sort(queues.begin(), queues.end());
	okQueue[0] = 1;
	for (unsigned i=0; i<queues.size(); i++) {
		for (int j=n - queues[i]; j>=0; j--) {
			if (okQueue[j]){
				okQueue[j+queues[i]] = 1;
				//printf("zzz: %d by %d\n", j+queues[i], i);
			}
		}
	}
	for (int i=0; i<n; i++){
		if (okQueue[i])
			printf("%d\n", i+shift);
	}

	return 0;
}


/// ///////////////////// ///
///                       ///
///      Silver Rain      ///
///                       ///
///         JU            ///
///                       ///
/// ///////////////////// ///

#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstdlib>
#include<cctype>

typedef long long ll;
typedef unsigned long long ull;

template <class T> T _diff(T a,T b) {return (a<b?b-a:a-b);}
template <class T> T _abs(T a) {return(a<0?-a:a);}
template <class T> T _max(T a, T b) {return (a>b?a:b);}
template <class T> T _min(T a, T b) {return (a<b?a:b);}
template <class T> T max3(T a, T b, T c) {return (_max(a,b)>c ? _max(a,b) : c);}
template <class T> T min3(T a, T b, T c) {return (_min(a,b)<c ? _min(a,b) : c);}
template< class T >T GCD(T a,T b) {
    a=_abs(a);b=_abs(b);T tmp;while(a%b){ tmp=a%b; a=b; b=tmp; } return b;
}
template< class T >T LCM(T a,T b) {
    a=_abs(a);b=_abs(b);return (a/GCD(a,b))*b;
}
#define pb(a) push_back(a)
#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define PN() puts("")
#define PCASE() printf("Case %d: ",kk++)
#define eps 1e-9
#define pi 2.0*acos(0.0)
#define inf 2000000000
#define mod 1000000007
#define MX  1000000
using namespace std;

int dp[1005][1005],n,x,sm,m;
vector<int>v;
int a[1005],nw,i,tmp;
bool f[1005];

void func(int p, int sum)
{
    if(sum+sm>n)return;
    f[sum+sm]=1;
    if(p>n)return;
    if(dp[p][sum]!=-1)return;
    dp[p][sum]=1;
    func(p+1,sum);
    func(p+1,sum+v[p]);
}

int main()
{
    v.clear();
    CLR(f);
    S(n);S(x);
    for(i=1;i<=n;i++)S(a[i]);
    sm=1;
    nw=x;
    f[nw]=1;
    while(a[nw])
    {
        sm++;
        nw=a[nw];
        f[nw]=1;
    }
    for(i=1;i<=n;i++)
    {
        if(a[i]==0 && f[i]==0)
        {
            tmp=1;
            nw=i;
            bool f1=1;
            while(f1)
            {
                f1=0;
                for(int j=1;j<=n;j++)
                {
                    if(a[j]==nw)
                    {
                        tmp++;
                        f1=1;
                        f[j]=1;
                        nw=j;
                        break;
                    }
                }
            }
            v.pb(tmp);

        }
    }
    CLR(f);
    SET(dp);
    m=v.size();
    func(0,0);
    for(i=1;i<=n;i++)
    {
        if(f[i])cout<<i<<endl;
    }
    return 0;
}


/// ///////////////////// ///
///                       ///
///      Silver Rain      ///
///                       ///
///         JU            ///
///                       ///
/// ///////////////////// ///

#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstdlib>
#include<cctype>

typedef long long ll;
typedef unsigned long long ull;

template <class T> T _diff(T a,T b) {return (a<b?b-a:a-b);}
template <class T> T _abs(T a) {return(a<0?-a:a);}
template <class T> T _max(T a, T b) {return (a>b?a:b);}
template <class T> T _min(T a, T b) {return (a<b?a:b);}
template <class T> T max3(T a, T b, T c) {return (_max(a,b)>c ? _max(a,b) : c);}
template <class T> T min3(T a, T b, T c) {return (_min(a,b)<c ? _min(a,b) : c);}
template< class T >T GCD(T a,T b) {
    a=_abs(a);b=_abs(b);T tmp;while(a%b){ tmp=a%b; a=b; b=tmp; } return b;
}
template< class T >T LCM(T a,T b) {
    a=_abs(a);b=_abs(b);return (a/GCD(a,b))*b;
}
#define pb(a) push_back(a)
#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define S(a) scanf("%d",&a)
#define P(a) printf("%d\n",a)
#define PN() puts("")
#define PCASE() printf("Case %d: ",kk++)
#define eps 1e-9
#define pi 2.0*acos(0.0)
#define inf 2000000000
#define mod 1000000007
#define MX  1000000
using namespace std;

int n,x,sm,m;
vector<int>v;
int a[1005],nw,i,tmp;
bool f[1005];
bool dp[1005][1005];

void func(int p, int sum)
{
    if(sum+sm>n)return;
    f[sum+sm]=1;
    if(p>=m)return;
    if(dp[p][sum])return;
    dp[p][sum]=1;
    func(p+1,sum);func(p+1,sum+v[p]);
}

int main()
{
    v.clear();
    CLR(f);
    S(n);S(x);
    for(i=1;i<=n;i++)S(a[i]);
    sm=1;
    nw=x;
    f[nw]=1;
    while(a[nw])
    {
        sm++;
        nw=a[nw];
        f[nw]=1;
    }
    for(i=1;i<=n;i++)
    {
        if(a[i]==0 && f[i]==0)
        {
            tmp=1;
            nw=i;
            bool f1=1;
            while(f1)
            {
                f1=0;
                for(int j=1;j<=n;j++)
                {
                    if(a[j]==nw)
                    {
                        tmp++;
                        f1=1;
                        f[j]=1;
                        nw=j;
                        break;
                    }
                }
            }
            v.pb(tmp);
            //cout<<tmp<<endl;
        }
    }
    //cout<<endl;
    CLR(f);
    m=v.size();
//    for(i=0;i<=1048576;i++)
//    {
//        sum=0;
//        for(int j=0;j<m;j++)
//        {
//            if(((1<<j)&i )!=0) sum+=v[j];
//        }
//        f[sum+sm]=1;
//    }
    CLR(dp);
    func(0,0);
    for(i=1;i<=n;i++)if(f[i]){P(i);}
    return 0;
}


/// ///////////////////// ///
///                       ///
///      Silver Rain      ///
///                       ///
///         JU            ///
///                       ///
/// ///////////////////// ///

#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstdlib>
#include<cctype>

typedef long long ll;
typedef unsigned long long ull;

template <class T> T _diff(T a,T b) {return (a<b?b-a:a-b);}
template <class T> T _abs(T a) {return(a<0?-a:a);}
template <class T> T _max(T a, T b) {return (a>b?a:b);}
template <class T> T _min(T a, T b) {return (a<b?a:b);}
template <class T> T max3(T a, T b, T c) {return (_max(a,b)>c ? _max(a,b) : c);}
template <class T> T min3(T a, T b, T c) {return (_min(a,b)<c ? _min(a,b) : c);}
template< class T >T GCD(T a,T b) {
    a=_abs(a);b=_abs(b);T tmp;while(a%b){ tmp=a%b; a=b; b=tmp; } return b;
}
template< class T >T LCM(T a,T b) {
    a=_abs(a);b=_abs(b);return (a/GCD(a,b))*b;
}
#define pb(a) push_back(a)
#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define S(a) scanf("%d",&a)
#define P(a) printf("%d\n",a)
#define PN() puts("")
#define PCASE() printf("Case %d: ",kk++)
#define eps 1e-9
#define pi 2.0*acos(0.0)
#define inf 2000000000
#define mod 1000000007
#define MX  1000000
using namespace std;

int n,x,sm,m,sum;
vector<int>v;
int a[1005],nw,i,tmp;
bool f[1005];

int main()
{
    v.clear();
    CLR(f);
    S(n);S(x);
    for(i=1;i<=n;i++)S(a[i]);
    sm=1;
    nw=x;
    f[nw]=1;
    while(a[nw])
    {
        sm++;
        nw=a[nw];
        f[nw]=1;
    }
    for(i=1;i<=n;i++)
    {
        if(a[i]==0 && f[i]==0)
        {
            tmp=1;
            nw=i;
            bool f1=1;
            while(f1)
            {
                f1=0;
                for(int j=1;j<=n;j++)
                {
                    if(a[j]==nw)
                    {
                        tmp++;
                        f1=1;
                        f[j]=1;
                        nw=j;
                        break;
                    }
                }
            }
            v.pb(tmp);

        }
    }
    CLR(f);
    m=v.size();
    for(i=0;i<=1048576;i++)
    {
        sum=0;
        for(int j=0;j<m;j++)
        {
            if(((1<<j)&i )!=0) sum+=v[j];
        }
        f[sum+sm]=1;
    }
    for(i=1;i<=n;i++)if(f[i]){P(i);}
    return 0;
}


#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<string>
#include<bitset>
#include<memory.h>
#include<cassert>

class string_operations
{
      private :
               long long length;
               std::vector <long long> power,hash;
               std::vector<std::vector<char> > bor;
               long long HashConst,AlphabetSize;
               std::string CurrentString;
      public:
             inline void Assign(std::string s)
             {
                  CurrentString = s,HashConst = 29LL,AlphabetSize=30;
                  length =  s.size();
                  power.resize(length+1),hash.resize(length+1);
                  power.clear(),hash.clear();
             }
             void BuildHash()
             {
                  power[0] = 1;
                  for(int i = 0 ; i < length; i ++ )
                   hash[i+1] = (hash[i] + (long long)(CurrentString[i]-'a'+1)*power[i]),
                   power[i+1]=power[i]*HashConst;
             }
             inline long long get(int l,int r)
             {
                        return ((long long)(hash[r]-hash[l-1])*power[length-r]);
             }
             inline bool EqualSubstring(int l1,int r1,int l2,int r2)
             {
                  if (l1>l2) std::swap(l1,l2),std::swap(r1,r2);
                  if (r2-l2!=r1-l1) return 0;
                  return (get(l1,r1)==get(l2,r2));
             }
};

class Graph_operations
{
      private:
              int vertex,edge;
              std::vector< std::vector<int> > g;
              std::vector< std::vector<long long> > cost;
              std::vector<int> used;
              std::vector<int> parent,tin,tout;
              bool two_sides,cycle,timer;
              inline bool DijekstraOutline(std::vector<long long > &D,std::set<std::pair<long long,int> > &s)
              {
                     if (s.empty()) return 0;
                     std::pair<long long , int> v = *s.begin();
                     s.erase(s.begin());
                     while (!s.empty() && v.first != D[v.second]) v = *s.begin(),s.erase(s.begin());
                     if (v.first!=D[v.second]) return 0;
                     used[v.second] = 1;
                     for (int i = 0 ; i < g[v.second].size() ;i ++ )
                     {
                         int u = g[v.second][i];
                         if (!used[u] && D[u] > v.first + cost[v.second][i])
                          D[u] = D[v.second] + cost[v.second][i] , s.insert(std::make_pair(D[u],u));
                     }
                     return 1;
              }
      public:
             inline void InputGraph(int n,int m,bool size,bool orient)
             {
                  vertex = n,edge = m;
                  two_sides = !orient,cycle = 0,timer = 0;
                  g.resize( vertex+1 ) , used.resize(vertex + 1,0) , parent.resize(vertex + 1);
                  cost.resize( vertex + 1);
                  int x,y;
                  long long z;
                  for (int i = 1 ; i <= edge ;i ++ )
                  {
                      scanf("%d%d",&x,&y);
                      g[x].push_back(y);
                      if (!orient) g[y].push_back(x);
                      if (!size) cost[x].push_back(1),cost[y].push_back(1);
                      else {
                      scanf("%I64d",&z);
                      cost[x].push_back(z);
                      if (!orient) cost[y].push_back(z);
                      }
                  }
                  if (!size)
                  for (int i = 1; i <= vertex ; i ++ )
                   std::sort(g[i].begin(),g[i].end()),
                   g[i].resize(std::distance(g[i].begin(),std::unique(g[i].begin(),g[i].end()))),
                   cost[i].resize(std::distance(g[i].begin(),g[i].end()));
             }
             inline void dfs(int v = 1 , int p = -1)
             {
                  if (p<0) timer = 0 , tin.assign(vertex + 1 , 0),
                   tout.assign(vertex + 1, 0);
                  tin[v] = ++timer;
                  parent[v] = p;
                  used[v] = timer;
                  for (int i = 0 ;i < g[v].size() ;i ++ )
                  {
                      int u = g[v][i];
                      if (used[u] && (!two_sides || u!=p)) cycle = 1;
                      if (!used[u]) dfs(u , v);
                  }
                  tout[v] = ++timer;
             }
             inline bool IsTree() { return !cycle; }
             inline std::vector<long long> Dijekstra(int v)
             {
                    std::vector<long long> D(vertex + 1, 2000000000000000000LL);
                    used.assign(vertex + 1 , 0);
                    std::set<std::pair<long long ,int> > s;
                    D[v] = 0;
                    s.insert(std::make_pair(D[v] , v));
                    for (int i = 0 ; i < vertex ; i ++ )
                         if (!DijekstraOutline(D,s)) break;
                    return D;
             }
};
class Fenwick_tree
{
      private:
              std::vector<long long> t;
              int size;
              long long get0(int l)
              {
                   long long res = 0;
                   for (; l>0 ;l = (l&(l+1)) - 1)
                       res += t[l];
                   return res;
              }
      public:
             void Assign(int n)
             {
                  size = n;
                  t.assign(size + 1 , 0);
             }
             void Build(std::vector<long long> &a)
             {
                  for (int i = 1 ;i <= size ;i ++ )
                      t[i] = a[i];
             }
             long long get(int l,int r)
             {
                  return get0(r) - get0(l-1);
             }
             void update(int r,long long x)
             {
                  for ( ; r <= size; r |=(r+1))
                      t[r]+=x;
             }
};
class Interval_tree
{
      private:
              int size;
              struct record
              {
                     int l,r;
                     long long dest;
              };
              std::vector<record> tree;
              long long CriticalF;
              long long F(int l,int r)
              {
                   return (tree[l].dest+tree[r].dest);
              }
              void InBuild(std::vector<long long> &a,int v ,int l,int r)
              {
                   tree[v].l = l,tree[v].r = r;
                   if (l == r) tree[v].dest = a[l];
                   else
                   {
                       int m = (l + r)/2;
                       InBuild(a,v+v,l,m);
                       InBuild(a,v+v+1,m+1,r);
                       tree[v].dest = F(v+v,v+v+1);
                   }
              }
      public:
             void Build(std::vector<long long> &a,int n)
             {
                  size = n;
                  CriticalF = 0;
                  tree.resize(4*size + 1 );
                  InBuild(a,1,1,n);
             }
             long long get(int v  ,int l,int r)
             {
                  if (l>r) return CriticalF;
                  if (tree[v].l == l && tree[v].r == r)
                   return tree[v].dest;
                  int m = (tree[v].l + tree[v].r)/2;
                  return get(v+v,l,m) + get(v+v+1,m+1,r);
             }
             void update(int v,int num,long long value)
             {
                  if (tree[v].l == tree[v].r)
                   return(void(tree[v].dest+=value));
                  int m = (tree[v].l + tree[v].r)/2;
                  if (num<=m) update(v+v,num,value);
                  else
                   update(v+v+1,num,value);
             }
};
//================================================================== */
using namespace std;

#define For(i,l,r) for (int i = l ;i <= r ;i ++ )
#define ForI(it , s , T) for (T :: iterator it = s.begin(); it != s.end() ;it ++ )
#define LL long long
#define READ(l,r,a) For(i,l,r) scanf("%I64d",&a[i]);
#define iinf 2000000000
#define linf 2000000000000000000LL
#define MOD  1000000007
#define bit(mask,i) (((mask&(1<<i))>0)?(1):(0))
#define pb(x) push_back(x)
#define mk(x) make_pair(x)
#define fir first
#define sec second

vector<LL> root,a;
bool dp[1500][1500]={0};

int dfs(int x)
{
    if (root[x]!=0) return root[x];
    else
     return (root[x] = dfs(a[x]) );
}
int main()
{
    int n,x;
    cin >> n >> x;
    a.resize(n+1);
    READ(1,n,a);
    int roots = 0;
    vector<LL> cnt(n+1);
    root.resize(n+1);
    For(i,1,n)
     if (a[i] == 0) root[i] = ++roots ,cnt[roots] = 1;
    int preIn = 0;
    For(i,1,n)
    {
              if (a[i] == 0) continue;
              root[i] = dfs(a[i]);
              cnt[root[i]] ++;
    }
    swap(cnt[roots] , cnt[root[x]]);
    --roots;
    cnt.resize(roots + 1);
    int r = x;
    while (a[r]!=0) preIn++,r=a[r];
    /*For(i,1,roots)
     cout << cnt[i] << endl;
    cout << preIn << endl;
    */
    set<int> ans;
    sort(cnt.begin() + 1 , cnt.end());
    dp[0][0] = 1;
    for (int i = 1 ;i <= roots ;i ++ )
     for (int j = 0 ; j <= n ;j ++ )
     {
         dp[i][j] = dp[i-1][j];
         if (j>=cnt[i])
         dp[i][j] |= dp[i-1][j-cnt[i]];
     }
    For(i,0,n)
     if (dp[roots][i])
      ans.insert(1 + preIn + i);
    ForI(it , ans , set<int> )
     printf("%d\n",*it);
    //cin.get();cin.get();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN=2000;

int n,x,sum;
int num[MAXN],belong[MAXN],front[MAXN],back[MAXN],size[MAXN];
bool f[MAXN];

int main()
{
	scanf("%d%d",&n,&x);
	for (int i=1;i<=n;++i)
		scanf("%d",&front[i]),back[front[i]]=i;
	for (int i=1;i<=n;++i)
		if (!front[i])
		{
			++sum;
			for (int j=i;j;j=back[j])
				belong[j]=sum,num[j]=++size[sum];
		}
	memset(f,0,sizeof(f));
	f[0]=1;
	for (int i=1;i<=sum;++i)
		if (i!=belong[x])
			for (int j=100;j>=size[i];--j)
				f[j]|=f[j-size[i]];
	for (int i=0;i<100;++i)
		if (f[i]) printf("%d\n",i+num[x]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN=2000;

int n,x,sum;
int num[MAXN],belong[MAXN],front[MAXN],back[MAXN],size[MAXN];
bool f[MAXN];

int main()
{
	scanf("%d%d",&n,&x);
	for (int i=1;i<=n;++i)
		scanf("%d",&front[i]),back[front[i]]=i;
	for (int i=1;i<=n;++i)
		if (!front[i])
		{
			++sum;
			for (int j=i;j;j=back[j])
				belong[j]=sum,num[j]=++size[sum];
		}
	memset(f,0,sizeof(f));
	f[0]=1;
	for (int i=1;i<=sum;++i)
		if (i!=belong[x])
			for (int j=n;j>=size[i];--j)
				f[j]|=f[j-size[i]];
	for (int i=0;i<n;++i)
		if (f[i]) printf("%d\n",i+num[x]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN=200;

int n,x,sum;
int num[MAXN],belong[MAXN],front[MAXN],back[MAXN],size[MAXN];
bool f[MAXN];

int main()
{
    scanf("%d%d",&n,&x);
    for (int i=1;i<=n;++i)
        scanf("%d",&front[i]),back[front[i]]=i;
    for (int i=1;i<=n;++i)
        if (!front[i])
        {
            ++sum;
            for (int j=i;j;j=back[j])
                belong[j]=sum,num[j]=++size[sum];
        }
    memset(f,0,sizeof(f));
    f[0]=1;
    for (int i=1;i<=sum;++i)
        if (i!=belong[x])
            for (int j=100;j>=size[i];--j)
                f[j]|=f[j-size[i]];
    for (int i=0;i<100;++i)
        if (f[i]) printf("%d\n",i+num[x]);
    return 0;
}


#include<bits/stdc++.h>
#define ll          long long int
#define lld          long double
#define pb          push_back
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define vec         vector<ll>
#define all(a)     (a).begin(),(a).end()
#define F           first
#define S           second
#define mod         1000000007
#define rep(i,a,b)	for(ll  i=a;i<b;i++)
#define repi(i,a,b)	for(ll  i=a;i<=b;i++)
#define per(i,a,b)  for(ll i=a;i>=b;i--)
#define mp          make_pair
#define mit         map<ll,ll>::iterator
#define sit         set<ll>::iterator
#define pit         pair<ll,ll>::iterator
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y)
{
   return (x | y) & (~x | ~y);
}
ll bin_Expo(ll x,ll n)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return bin_Expo(x*x,n/2);
    else                             //n is odd
        return x*bin_Expo(x*x,(n-1)/2);
}
ll mod_Expo(ll x,ll n,ll M)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return mod_Expo((x*x)%M,n/2,M);
    else                             //n is odd
        return (x*mod_Expo((x*x)%M,(n-1)/2,M))%M;

}
ll NcR(int n, int r)
{
    ll p = 1, k = 1;
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
            ll m=__gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
    return p;
}
bool prime_check(ll x)
{
    bool prime = (x >= 2);
    for (ll i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
        {
            prime = false;
            break;
        }
    }
    return prime;
}
ll logg(ll base,ll x) {
    return (ll)(log(x) / log(base));
}
bool vis[100005];
vec adj[100005];
ll h=-1,hh=0,x;
ll dfs(ll k)
{
	ll r=1;
	if(k==x)
	hh=1;
	vis[k]=true;
	rep(i,0,adj[k].size())
	{
		if(!vis[adj[k][i]])
		{
			r+=dfs(adj[k][i]);
		}
	}
	return r;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
    while(TESTS--)
    {
    	ll n,i,j,l,m;
    	cin>>n>>x;
    	ll k[n+1],dp[n+1],v[n+1];
    	repi(i,1,n)
    	{
    		cin>>k[i];
    		if(k[i]!=0)
    		{
    			adj[i].pb(k[i]);
    			adj[k[i]].pb(i);
			}
    		vis[i]=false;
    		v[i]=0;
    		dp[i]=0;
		}
		l=1;m=0;
		j=k[x];
		while(j!=0)
		{
			l++;
			j=k[j];
		}
		repi(i,1,n)
    	{
    		if(!vis[i])
    		{
    			j=dfs(i);
    			if(hh==0)
    			v[m++]=j;
    			else
    			hh=0;
			}
		}
		dp[0]=1;
		rep(i,0,m)
		{
			for(j=n-1;j>=0;j--)
			{
				if(j-v[i]>=0 && dp[j-v[i]])
				dp[j]=1;
			}
		}
		rep(i,0,n)
		if(dp[i])
		cout<<i+l<<'\n';
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n, x; cin >> n >> x;
    vector<int> prv(n + 1), nxt(n + 1);
    for (int i = 1; i <= n; i++) cin >> prv[i], nxt[prv[i]] = i;
    vector<int> dp(n + 1), seen(n + 1);
    {
        int cnt = 1, cur = x; seen[cur] = true;
        while (prv[cur]) cnt += seen[cur = prv[cur]] = true;
        dp[cnt] = true;
    }
    for (int i = 1; i <= n; i++) {
        if (prv[i] || seen[i]) continue;
        int cnt = 1, cur = i;
        while (nxt[cur]) cnt++, cur = nxt[cur];
        for (int j = n - cnt; j >= 0; j--) dp[j + cnt] |= dp[j];
    }
    for (int i = 1; i <= n; i++) if (dp[i]) cout << i << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int maxn = (int)1e6 + 10;
const int mod = (int)1e9;
const int inf = (1<<30) - 1;

int p[maxn];
int a[maxn];
int n;
int x;
int dp[maxn];
int u[maxn];

int dfs(int v) {
    u[v] = 1;
    if(a[v] == 0) return 1;
    return dfs(a[v]) + 1;
}
int rdfs(int v) {
    u[v] = 1;
    if(p[v] == 0) return 1;
    return rdfs(p[v])+1;
}

void solve() {
    cin >> n >> x;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        p[a[i]] = i;
    }
    int sz1 = dfs(x);
    int sz2 = rdfs(x);
    dp[0] = 1;
    for(int i = 1; i <= n; i++) {
        if(u[i]) continue;
        int y = dfs(i) + rdfs(i) - 1;
        for(int j = n; j >= y; j--) {
            dp[j] |= dp[j-y];
        }
    }
    for(int i = 0; i<=n; i++) {
        if(dp[i]) cout << i + sz1 << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //srand(time(0));
    int t = 1;
    //scanf("%d", &t);
    //cin >> t;
    for(int i = 1; i <= t; i++) {
        //printf("Case %d: ", i);
        solve();
    }
    return 0;
}