#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,sum=0,sum1=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]<b)
		{
		sum1+=arr[i];
		}
		if(sum1>=d)
		{
			sum+=1;
			sum1=0;
		}
	}

      cout<<sum<<endl;

	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,noorange=0,nototal=0,ws=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];

		if(arr[i]<=b)
		{
		noorange+=1;
        nototal+=arr[i];
		}
		if(nototal>d)
		{
		 ws++;
		 nototal=0;

		}
	}

      cout<<ws<<endl;

	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,sum=0,sum1=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]<=b)
		{
		sum1+=arr[i];
		}
		if(sum1>=d)
		{
			sum+=1;
			sum1=0;
		}
	}

      cout<<sum<<endl;

	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,noorange=0,nototal=0,ws=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]<b)
		{
		noorange+=1;
        nototal+=arr[i];
		}
		if(nototal>=d)
		{
		 ws+=1;
		 nototal=0;

		}
	}
if(noorange==1)
{
	ws=1;
}
      cout<<ws<<endl;

	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,sum=0,sum1=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]<b)
		{
		sum1+=arr[i];
		}
		if(sum1>=d)
		{
			sum+=1;
		}
	}

      cout<<sum<<endl;

	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
void main()
{

	long int arr[100000],b=0,d=0,n=0,noorange=0,nototal=0,ws=0;
	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(arr[i]<b)
		{
		noorange+=1;
        nototal+=arr[i];
		}
		if(nototal>=d)
		{
		 ws+=1;
		}
	}
if(noorange==1)
{
	ws=1;
}
      cout<<ws<<endl;

	//system("pause");
}


#include<iostream>

using namespace std;

int main()
{
    long long a,b,c,q,m=0,x=0;
    cin>>a>>b>>c;
    for( long long i=0 ; i<a ; i++ ){
        cin>>q;
        if( q<=b ){
            m=m+q;
        }
        else{
            m=m+0;
        }
        if( m>=c ){
            m=0;
            x++;
        }
    }

    cout<<x;

}


#include<iostream>

using namespace std;

int main()
{
    long long a,b,c,q,m=0,z;
    cin>>a>>b>>c;
    for( long long i=0 ; i<a ; i++ ){
        cin>>q;
        if( q<b ){
            m=m+q;
        }
        else{
            m=m+0;
        }
    }
    z=m/c;
    cout<<z;

}


#include<iostream>

using namespace std;

int main()
{
    long long a,b,c,q,m=0,x=0;
    cin>>a>>b>>c;
    for( long long i=0 ; i<a ; i++ ){
        cin>>q;
        if( q<=b ){
            m=m+q;
        }
        else{
            m=m+0;
        }
        if( m>c ){
            m=0;
            x++;
        }
    }

    cout<<x;

}


#include<iostream>

using namespace std;

int main()
{
    long long a,b,c,arr[100000],m=0,z;
    cin>>a>>b>>c;
    for( long long i=0 ; i<a ; i++ ){
        cin>>arr[i];
        if( arr[i]<b )
            m=m+arr[i];
    }
    z=m/c;
    cout<<z;

}


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

int main()
{
    int n , b , d ,waste = 0 , cnt = 0 ;
    cin >> n >> b >> d;

    vector<int> ar(n);
    for(int i = 0 ; i < n ; i++)
        cin >> ar[i] ;

    for(int i = 0 ; i < n ; i++)
    {
        if ( ar[i] <= b )
         {
             waste += ar[i];

        if(waste > d )
        {
            cnt++;
            waste = 0 ;
        }
         }

    }
    cout << cnt;

    return 0 ;
}


#include<iostream>

using namespace std;

int main()
{
    int n , b , d ,waste = 0 , cnt = 0 ;
    cin >> n >> b >> d;
    int ar[n];
    for(int i = 0 ; i < n ; i++)
        cin >> ar[i] ;

    for(int i = 0 ; i < n ; i++)
    {
        if ( ar[i] <= b )
            waste += ar[i];
        if(waste >= d )
        {cnt++;
        waste = 0 ;}

    }
    cout << cnt;

    return 0 ;
}


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

int main()
{
    int n , b , d ,waste = 0 , cnt = 0 ;
    cin >> n >> b >> d;

    vector<int> ar(n);
    for(int i = 0 ; i < n ; i++)
        cin >> ar[i] ;

    for(int i = 0 ; i < n ; i++)
    {
        if ( ar[i] <= b )
            waste += ar[i];
        if(waste >= d )
        {cnt++;
        waste = 0 ;}

    }
    cout << cnt;

    return 0 ;
}


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

int main()
{
    int n , b , d ,waste = 0 , cnt = 0 ;
    cin >> n >> b >> d;

    vector<int> ar[n];
    for(int i = 0 ; i < n ; i++)
        cin >> ar[i] ;

    for(int i = 0 ; i < n ; i++)
    {
        if ( ar[i] <= b )
            waste += ar[i];
        if(waste >= d )
        {cnt++;
        waste = 0 ;}

    }
    cout << cnt;

    return 0 ;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n,b,d,count=0 ;
	int sum=0,x,arr[10000],wast=0;

	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		if(x<=b)
			arr[count++]=x;
	}

	for(int i=0;i<count;i++)
	{
		sum+=arr[i];
		if(sum==d)
			sum=0;
		if(sum>d)
		{
			wast++;
			sum=0;
		}

	}
	cout<<wast;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
	long long n,b,d,count=0 ;
	long long sum=0,x,arr[100000],wast=0;

	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>x;

		if(x<=b)
			arr[count++]=x;
	}

	for(int i=0;i<count;i++)
	{
		sum+=arr[i];
		//if(sum==d)
		//	sum=0;
		if(sum>d)
		{
			wast++;
			sum=0;
		}

	}
	cout<<wast;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
	long long n,b,d,count=0 ;
	long long sum=0,x,arr[100000],wast=0;

	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		if(x<=b)
			arr[count++]=x;
	}

	for(int i=0;i<count;i++)
	{
		sum+=arr[i];
		if(sum==d)
			sum=0;
		if(sum>d)
		{
			wast++;
			sum=0;
		}

	}
	cout<<wast;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n,b,d,count=0 ;
	int sum=0,x,arr[100000],wast=0;

	cin>>n>>b>>d;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		if(x<=b)
			arr[count++]=x;
	}

	for(int i=0;i<count;i++)
	{
		sum+=arr[i];
		if(sum==d)
			sum=0;
		if(sum>d)
		{
			wast++;
			sum=0;
		}

	}
	cout<<wast;
}


#include <iostream>
using namespace std;
int main()
{
	int n, b, d,a;
	cin >> n >> b >> d;
	int x = 0,y=0,i=0;
	while (i<n) {
		cin >> a;
		if (a < b) {
			y += a;
			if (y >= d) {
				x++;
				y = 0;
			}

		}
		i++;
	}
	cout << x;

}


#include <iostream>
using namespace std;
int main()
{
	int n, b, d,a;
	cin >> n >> b >> d;
	int x = 0,y=0,i=0;
	while (i<n) {
		cin >> a;
		if (a <= b) {
			y += a;
			if (y >= d) {
				x++;
				y = 0;
			}

		}
		i++;
	}
	cout << x;

	//system("pause");
}


#include <iostream>
using namespace std;
int main()
{
	int n, b, d, a;
	cin >> n >> b >> d;
	int x = 0, y = 0, i = 0;
	while (i < n) {
		cin >> a;
		if (a <= b) {
			y += a;
			if (y > d) {
				x++;
				y = 0;
			}

		}
		i++;
	}

	cout << x;

}


#include <iostream>
using namespace std;
int main()
{
	int n, b, d,a[100000];
	cin >> n >> b >> d;
	int x = 0,y=0;
	for (int i = 0; i < n;i++) {
		cin >> a[i];
		if (a[i] > b) {
			if (a[i] >= d)
				x++;
			else if (y>=d)
				x++;
			else
				y += a[i];
		}

	}
	cout << x;

}


#include <iostream>
using namespace std;
int main()
{
	int n, b, d, a;
	cin >> n >> b >> d;
	int x = 0, y = 0, i = 0;
	while (i < n) {
		cin >> a;
		if (a <= b) {
			y += a;
			if (y >= d) {
				x++;
				y = 0;
			}

		}
		i++;
	}
	if (b == d && n == 1)
		x = 0;
	cout << x;

	//system("pause");
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	long long n1, n2, n3;
	cin >> n1 >> n2 >> n3;
	long long*arr = new long long[n1];
	long long sum = 0,count=0;
	for (long long i = 0; i < n1; i++)
	{
		cin >> arr[i];
		if (arr[i] > n2)
			arr[i] = 0;
		sum += arr[i];
	}
	while (sum > n3)
	{
		count++;
		sum -= n3;
	}
	cout << count;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int n1, n2, n3;
	cin >> n1 >> n2 >> n3;
	int*arr = new int[n1];
	int sum = 0,count=0;
	for (int i = 0; i < n1; i++)
	{
		cin >> arr[i];
		if (arr[i] > n2)
			arr[i] = 0;
		sum += arr[i];
	}
	while (sum > n3)
	{
		count++;
		sum -= n3;
	}
	cout << count;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int n1, n2, n3;
	cin >> n1 >> n2 >> n3;
	int*arr = new int [n1];
	long long sum = 0;
	int count=0;
	for (int i = 0; i < n1; i++)
	{
		cin >> arr[i];
		if (arr[i] <= n2)
			sum += arr[i];
		if (sum > n3)
		{
			count++;
			sum = 0;
		}
	}

	cout << count;
	return 0;
}


#include <iostream>
using namespace std;

void main()
{
  int n,b,d,a;
  cin>>n>>b >>d;

  int totalSize =0, wasteCount=0;
  for(int i =0; i <n;i++){
     cin>>a;
      if(a<=b){
     totalSize+=a;
     if(totalSize >d){
        wasteCount++;
        totalSize=0;}
   }
}
cout<<wasteCount;
}


#include <iostream>
using namespace std;

void main()
{
  int n,b,d,a;
  cin>>n>>b >>d;

  int totalSize =0, wasteCount=0;
  for(int i =0; i <n;i++){
     cin>>a;
      if(a<b){
     totalSize+=a;
     if(totalSize >=d){
        wasteCount++;
        totalSize-=d;}
   }
}
cout<<wasteCount;
}


#include <iostream>
using namespace std;

void main()
{
  int n,b,d,a;
  cin>>n>>b >>d;

  int totalSize =0, wasteCount=0;
  for(int i =0; i <n;i++){
     cin>>a;
      if(a<b){
     totalSize+=a;
     if(totalSize >d){
        wasteCount++;
        totalSize-=d;}
   }
}
cout<<wasteCount;
}


#include <iostream>
using namespace std;

void main()
{
  int n,b,d,a;
  cin>>n>>b >>d;

  int totalSize =0, wasteCount=0;
  for(int i =0; i <n;i++){
     cin>>a;
      if(a<b){
     totalSize+=a;
     if(totalSize >d){
        wasteCount++;
        totalSize=0;}
   }
}
cout<<wasteCount;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>

using namespace std;

class DSU {
public:
  DSU () {}

  DSU (int size) {
    reset(size);
  }

  void reset (int size) {
    parent.resize(size);
    rank.resize(size, 1);
    for (int i = 0; i < size; ++i) {
      parent[i] = i;
    }
  }

  int getSetId (int v) {
    if (parent[v] == v) return v;
    parent[v] = getSetId(parent[v]);
    return parent[v];
  }

  int getRank (int v) {
    if (parent[v] == v) return rank[v];
    parent[v] = getSetId(parent[v]);
    return getRank(parent[v]);
  }

  void join (int u, int v) {
    int first_set_id = getSetId(u);
    int second_set_id = getSetId(v);
    if (first_set_id == second_set_id) {
      return;
    }

    if (rank[first_set_id] < rank[second_set_id]) {
      swap(first_set_id, second_set_id);
    }
    parent[second_set_id] = first_set_id;
    rank[first_set_id] += rank[second_set_id];
  }

  bool inOneSet (int u, int v) {
    return getSetId(u) == getSetId(v);
  }

private:
  vector <int> parent;
  vector <int> rank;
};

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, x;
    cin >> n >> x;
    --x;

    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      --a[i];
    }

    int pos = 1;
    int cur = x;
    while (a[cur] != -1) {
      pos++;
      cur = a[cur];
    }

    DSU chains(n);
    for (int i = 0; i < n; ++i) {
      if (a[i] != -1) chains.join(i, a[i]);
    }

    vector <int> sizes;
    for (int i = 0; i < n; ++i) {
      if (a[i] == -1 && chains.getSetId(x) != chains.getSetId(i))
        sizes.push_back(chains.getRank(i));
    }

    vector <int> ans(1, pos);
    for (int i = 0; i < sizes.size(); ++i) {
      int cur_size = ans.size();
      for (int j = 0; j < cur_size; ++j) {
        ans.push_back(ans[j] + sizes[i]);
      }
      if (ans.size() > 100000) {
        sort(ans.begin(), ans.end());
        ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
      }
    }

    sort(ans.begin(), ans.end());
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());

    for (int i = 0; i < ans.size(); ++i) {
      cout << ans[i] << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>

using namespace std;

class DSU {
public:
  DSU () {}

  DSU (int size) {
    reset(size);
  }

  void reset (int size) {
    parent.resize(size);
    rank.resize(size, 1);
    for (int i = 0; i < size; ++i) {
      parent[i] = i;
    }
  }

  int getSetId (int v) {
    if (parent[v] == v) return v;
    parent[v] = getSetId(parent[v]);
    return parent[v];
  }

  int getRank (int v) {
    if (parent[v] == v) return rank[v];
    parent[v] = getSetId(parent[v]);
    return getRank(parent[v]);
  }

  void join (int u, int v) {
    int first_set_id = getSetId(u);
    int second_set_id = getSetId(v);
    if (first_set_id == second_set_id) {
      return;
    }

    if (rank[first_set_id] < rank[second_set_id]) {
      swap(first_set_id, second_set_id);
    }
    parent[second_set_id] = first_set_id;
    rank[first_set_id] += rank[second_set_id];
  }

  bool inOneSet (int u, int v) {
    return getSetId(u) == getSetId(v);
  }

private:
  vector <int> parent;
  vector <int> rank;
};

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, x;
    cin >> n >> x;
    --x;

    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      --a[i];
    }

    int pos = 1;
    int cur = x;
    while (a[cur] != -1) {
      pos++;
      cur = a[cur];
    }

    DSU chains(n);
    for (int i = 0; i < n; ++i) {
      if (a[i] != -1) chains.join(i, a[i]);
    }

    vector <int> sizes;
    for (int i = 0; i < n; ++i) {
      if (a[i] == -1 && chains.getSetId(x) != chains.getSetId(i))
        sizes.push_back(chains.getRank(i));
    }

    vector <int> ans(1, pos);
    for (int i = 0; i < sizes.size(); ++i) {
      int cur_size = ans.size();
      for (int j = 0; j < cur_size; ++j) {
        ans.push_back(ans[j] + sizes[i]);
      }
    }

    sort(ans.begin(), ans.end());
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());

    for (int i = 0; i < ans.size(); ++i) {
      cout << ans[i] << endl;
    }

    return 0;
}


#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef __int64 ll;
const int N = 100005;
int lcm[N];
int set[N];
int stack[N],top;
int n,x,xpos;
int cnt[N];

int cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int i,j;
    int root;
    int add;
    while(scanf("%d%d",&n,&x) != EOF)
    {
        memset(set,0,sizeof(set));
        for(i = 1;i <= n;i ++)
            scanf("%d",&lcm[i]);
        i = x;
        xpos = 1;
        while(lcm[i])
        {
            xpos ++;
            i = lcm[i];
        }
        set[i] = -1;
        for(i = 1;i <= n;i ++)
        {
            root = i;
            while(lcm[root])
            {
                root = lcm[root];
            }
            if(set[root] == -1)
                continue;
            else
            {
                set[root] ++;
            }
        }
        //for(i = 1;i <= n;i ++)
        //    printf("%d\n",set[i]);
        sort(set + 1,set + 1 + n,cmp);

        top = 0;
        memset(cnt,0,sizeof(cnt));
        stack[top ++] = xpos;
        cnt[xpos] = 1;
        for(i = 1;set[i];i ++)
        {
            add = 0;
            for(j = 0;j < top;j ++)
            {
                if(set[i] + stack[j] <= n && cnt[set[i] + stack[j]] == 0)
                {
                    cnt[set[i] + stack[j]] = 1;
                    stack[top + add] = set[i] + stack[j];
                    add ++;
                }
            }
            top += add;
        }
        for(i = 1;i <= n;i ++)
            if(cnt[i])
                printf("%d\n",i);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<deque>
#include<utility>
#include<map>
#include<math.h>
#include<memory.h>

#define li long long
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair

using namespace std;

int INF = 1e9 + 7;

inline int mod(int x){
	if(x < 0) return -x;
	else
		return x;
}

int p[1005], a[1005];
bool b[1005];
bool pos[1005];
int n, x, i;

int dfs(int v){
	if(v == x) return -INF;
	if(p[v] == 0)
		return 1;
	else
		return 1 + dfs(p[v]);
}

int main(){
	//freopen("A-small-attempt0 (1).in", "r", stdin);
	//freopen("OUTPUT.txt", "w", stdout);
	//cout.setf(ios::fixed);
	//cout.precision(10);
	cin >> n >> x;
	for(i = 1; i <= n; i++)
		b[i] = false;
	for(i = 1; i <= n; i++){
		cin >> p[i];
		b[p[i]] = true;
	}
	int k = 1;
	int R;
	for(i = 1; i <= n; i++)
		if(!b[i]){
			if(dfs(i) > 0){
				a[k] = dfs(i);
				k++;
			}
			else{
				if(p[x] == 0)
					R = 1;
				else
					R = dfs(p[x]) + 1;
			}
		}
	pos[0] = true;
	for(i = 1; i < k; i++){
		bool pos1[1005];
		memset(pos1, 0, sizeof(pos1));
		for(int j = 0; j <= n - a[i]; j++)
			if(pos[j])
				pos1[j + a[i]] = true;
		for(int j = 0; j <= n; j++)
			if(pos1[j])
				pos[j] = true;
	}
	for(i = 0; i <= n; i++)
		if(pos[i])
			cout << i + R << "\n";
	return 0;
}


//#pragma comment(linker, "/STACK:20000000")
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <ctime>

using namespace std;
typedef long long int64;
#ifdef HOME
	#define E(c) cerr<<#c
	#define Eo(x) cerr<<#x<<" = "<<(x)<<endl
	#define Ef(...) fprintf(stderr, __VA_ARGS__)
#else
	#define E(c) ((void)0)
	#define Eo(x) ((void)0)
	#define Ef(...) ((void)0)
#endif

const int SIZE = 1024;

int n, x;
int mprev[SIZE];
int mnext[SIZE];

bool used[SIZE];

int k, xx;
int arr[SIZE];

bool res[2][SIZE];

int main() {
#ifdef HOME
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &x);
	x--;
	memset(mprev, -1, sizeof(mprev));
	memset(mnext, -1, sizeof(mnext));
	for (int i = 0; i<n; i++) {
		scanf("%d", &mprev[i]);
		mprev[i]--;
		if (mprev[i] >= 0) mnext[mprev[i]] = i;
	}

	for (int i = 0; i<n; i++) if (!used[i] && mprev[i] < 0) {
		int xpos = -1;

		int id = 0;
		int curr = i;
		while (curr >= 0) {
			int t = mnext[curr];

			used[curr] = true;
			if (curr == x) xpos = id;

			curr = t;
			id++;
		}

		if (xpos < 0)
			arr[k++] = id;
		else
			xx = xpos;
	}

	res[0][0] = true;
	for (int i = 0; i<k; i++) {
		memset(res[(i+1)&1], 0, sizeof(res[0]));
		for (int j = 0; j<=n; j++) if (res[i&1][j]) {
			res[(i+1)&1][j] = true;
			res[(i+1)&1][j+arr[i]] = true;
		}
	}

	for (int i = 0; i<=n; i++)
		if (res[k&1][i])
			printf("%d\n", i + xx + 1);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <fstream>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <math.h>
#include <iomanip>

using namespace std;

#define rep(i,o,n) for (int i=o;i<=n;i++)
#define rd cin >>
#define w cout <<
#define wl cout << endl
#define road pair<int,int>
//#define cin inf
//#define cout of
#define ll long long

int s1,r=0,s,q,e=0,b[1001],d[1001],n,x,a[1001],f[1001]={0},c,h[1001];

void dfs(int i)
{
	if (i==x) { s1 = c; r = 1; }
	c++;
	if (a[i]!=0) dfs(a[i]);
}

int main()
{
	ifstream inf("input.txt");
	rd n >> x;
	rep(i,1,n)
	{
		rd a[i];
		f[a[i]] = 1;
	}
	rep(i,1,n)
	{
		c = 0;
		if (f[i]==0) dfs(i);
		if (c==0) continue; else
		if (r==0) { e++; b[e] = c; } else { s = c-s1-1; r=0; }
	}
	d[0] = 1;
	/*rep(i,1,e)
		w b[i] << ' ';
	wl;*/
	rep(i,1,e)
		for (int i1=n;i1>=b[i];i1--)
			if (d[i1-b[i]]==1) d[i1] = 1;
	rep(i,0,n)
		if (d[i]==1) w i+s+1 << endl;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX(x,y) (x > y ? x : y)
#define MIN(x,y) (x > y ? y : x)
int fl[20005];
int  mas[20005];
int res[3004];
int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	memset(fl,0,sizeof(fl));
	memset(res,0,sizeof(res));
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		{
			cin>>mas[i];
			fl[mas[i]]=1;
	}
	vector<int> arr(0);
	int d=1;
	for(int i=1;i<=n;i++)
	{
		if(fl[i])continue;
		int x=i;
		int len=1;
		bool f=false;
		if(x==m)f=true;
		while(mas[x])
		{
			if(x==m)
			{
				f=true;
				len=1;
			}
			len++;
			x=mas[x];
		}
		if(f)d=len;
		else arr.push_back(len);
	}
	res[0]=1;
	for(int i=0;i<arr.size();i++)
		for(int j=1000;j>=0;j--)if(res[j])res[j+arr[i]]=1;
	//for(int i=0;i<=n;i++)cout<<res[i]<<endl;
	int i=0;
	//cout<<d<<endl;
	while(i+d<=n)
	{
		if(res[i])cout<<(i+d)<<endl;
		i++;
	}
	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX(x,y) (x > y ? x : y)
#define MIN(x,y) (x > y ? y : x)
int fl[20005];
int  mas[20005];
int res[3004];
int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	memset(fl,0,sizeof(fl));
	memset(res,0,sizeof(res));
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		{
			cin>>mas[i];
			fl[mas[i]]=1;
	}
	vector<int> arr(0);
	int d=1;
	for(int i=1;i<=n;i++)
	{
		if(fl[i])continue;
		int x=i;
		int len=1;
		bool f=false;
		if(x==m)f=true;
		while(mas[x])
		{

			len++;
			x=mas[x];
			if(x==m)
			{
				f=true;
				len=1;
			}
		}
		if(f)d=len;
		else arr.push_back(len);
	}
	res[0]=1;
	for(int i=0;i<arr.size();i++)
		for(int j=1000;j>=0;j--)if(res[j])res[j+arr[i]]=1;
	//for(int i=0;i<=n;i++)cout<<res[i]<<endl;
	int i=0;
	//cout<<d<<endl;
	while(i+d<=n)
	{
		if(res[i])cout<<(i+d)<<endl;
		i++;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <string>
#include <map>

using namespace std;

const int SIZE = 1e5 + 5;

int main()
{
    int n, x, z;
    cin >> n >> x;
    vector<int> a(n), b(n, -1);
    vector<int> used(n, false);
    vector<int> mas;
    --x;

    for (int i = 0; i < n; ++i)
    {
        cin >> z;
        --z;
        if (z != -1)
            b[z] = i;
        a[i] = z;
    }

    for (int i = 0; i < n; ++i)
    {
        if (!used[i])
        {

            int curr = i, dl = -1, bobr = 0;

            while (curr != -1)
            {
                if (curr == x)
                {
                    bobr = 1;
                }
                used[curr] = 1;
                curr = b[curr];
                ++dl;
            }

            curr = i;

            while (curr != -1)
            {
                if (curr == x)
                {
                    bobr = 1;
                }
                used[curr] = 1;
                curr = a[curr];
                ++dl;
            }

            if (!bobr)
            {
                mas.push_back(dl);
            }
        }
    }

    vector<int> r(n);

    r[0] = 1;

    for (int j = 0; j < mas.size(); ++j)
    {
        for (int i = n - 1; i > -1; --i)
        {
            if (r[i])
            {
                r[i + mas[j]] = 1;
            }
        }
    }

    int curr = x, dlb = 0;

    while (curr != -1)
    {
        curr = a[curr];
        ++dlb;
    }

    for (int i = 0; i < n; ++i)
    {
        if (r[i])
        {
            cout << i + dlb << endl;
        }
    }

    return 0;
}


#if 1
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <sstream>
#include <iostream>
#include <bitset>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int , int> pii;
typedef vector <int> veci;
typedef vector <veci> graph;
const LD eps = 1e-9;
const LD pi = acos(-1.0);
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define iss istringstream
#define oss ostringstream
#define dbg(x) {cerr << #x << " = " << x << endl;}
#define dbgv(x) {cerr << #x << " ={"; for (int _i = 0; _i < x.size(); _i++) {if (_i) cerr << ", "; cerr << x[_i];} cerr << "}" << endl;}
#define NAME "problem"

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
    //freopen(NAME ".in","r",stdin);freopen(NAME ".out","w",stdout);

    int n, x;
    cin >> n >> x;
    --x;
    vector<int> a(n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        --a[i];
    }
    vector<int> gr(n, -1);
    vector<int> pos(n, -1);

    int cur_gr = 0;
    for(int i = 0; i < n; ++i)
        if(a[i] < 0)
        {
            gr[i] = cur_gr++;
            pos[i] = 0;
        }

    for(int i = 0; i < n; ++i) if(gr[i] < 0)
    {
        int cur = i;
        int cnt = 0;
        while(gr[cur] < 0)
        {
            ++cnt;
            cur = a[cur];
        }
        int root = cur;
        cur = i;
        while(gr[cur] < 0)
        {
            gr[cur] = gr[root];
            pos[cur] = pos[root] + cnt;
            --cnt;
            cur = a[cur];
        }
    }

    vector<int> cnt(cur_gr);
    for(int i = 0; i < n; ++i)
        cnt[gr[i]]++;

    int our = gr[x];
    int wh = pos[x] + 1;
    cnt.erase(cnt.begin() + our);

    vector<int> dp(n + 1);
    dp[0] = 1;
    for(int i = 0; i < cnt.size(); ++i)
        for(int j = n; j >= cnt[i]; --j)
            if(dp[j - cnt[i]])
                dp[j] = true;

    for(int i = 0; i <= n; ++i)
        if(dp[i])
        {
            cout << i + wh << endl;
        }

    return 0;
}
/*************************
*************************/
#endif


#if 0
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <sstream>
#include <iostream>
#include <bitset>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int , int> pii;
typedef vector <int> veci;
typedef vector <veci> graph;
const LD eps = 1e-9;
const LD pi = acos(-1.0);
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define iss istringstream
#define oss ostringstream
#define dbg(x) {cerr << #x << " = " << x << endl;}
#define dbgv(x) {cerr << #x << " ={"; for (int _i = 0; _i < x.size(); _i++) {if (_i) cerr << ", "; cerr << x[_i];} cerr << "}" << endl;}
#define NAME "problem"

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
    //freopen(NAME ".in","r",stdin);freopen(NAME ".out","w",stdout);

    int n, x;
    cin >> n >> x;
    --x;
    vector<int> a(n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        --a[i];
    }
    vector<int> gr(n, -1);
    vector<int> pos(n, -1);

    int cur_gr = 0;
    for(int i = 0; i < n; ++i)
        if(a[i] < 0)
        {
            gr[i] = cur_gr++;
            pos[i] = 0;
        }

    for(int i = 0; i < n; ++i) if(gr[i] < 0)
    {
        int cur = i;
        int cnt = 0;
        while(gr[cur] < 0)
        {
            ++cnt;
            cur = a[cur];
        }
        int root = cur;
        cur = i;
        while(gr[cur] < 0)
        {
            gr[cur] = gr[root];
            pos[cur] = pos[root] + cnt;
            --cnt;
            cur = a[cur];
        }
    }

    vector<int> cnt(cur_gr);
    for(int i = 0; i < n; ++i)
        cnt[gr[i]]++;

    int our = gr[x];
    int wh = pos[x] + 1;
    cnt.erase(cnt.begin() + our);

    vector<int> dp(n + 1);
    dp[0] = 1;
    for(int i = 0; i < cnt.size(); ++i)
        for(int j = n; j >= cnt[i]; --j)
            if(dp[j - cnt[i]])
                dp[j] = true;

    for(int i = 0; i <= n; ++i)
        if(dp[i])
        {
            cout << i + wh << endl;
        }

    return 0;
}
/*************************
*************************/
#endif


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <memory.h>
#include <cassert>
#include <time.h>
using namespace std;
#pragma comment(linker, "/STACK:20000000")

typedef vector<int> vi;
#define sz(a) int((a).size())
#define all(c) (c).begin(),(c).end()

string problem_name = "a";

void init(){
  freopen((problem_name+".in").c_str(),"rt",stdin);
//  freopen((problem_name+".out").c_str(),"wt",stdout);
}

int p[2110];
int ms[2111];

int u[2100];

int main()
{
  //  init();

	u[0]=1;

	int n,x;
	cin >> n >> x;

	for (int i=1;i<=n;i++)
	{
		cin >> ms[i];
		p[ms[i]]=1;
	}

	int ps = 0;
	for (int i=1;i<=n;i++)
	if (!p[i])
	{
		int c=0;
		int cur = i;
		int findme = 0;
		int len=0;
		do
		{
			c++;
			if (cur==x)
			{
				findme=1;
			}
			if (findme) ps++;
			cur = ms[cur];
		} while (cur);
		if (!findme)
		{
			for (int j=2000;j>=c;j--)
				if (u[j-c]) u[j]=1;
		}
	}

	for (int i=0;i<2000;i++)
		if (u[i])
			cout << i+ps << endl;

	return 0;
}


// rc.cpp
//

#include <math.h>
#include <string.h>

#include <iterator>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <algorithm>
#include <numeric>

using namespace std;

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned __int64 uint64;

#define PI 3.1415926535897932384626433832795
#define EPS 1E-8

uint s[1010],
	 v[1010],
	 nv;

bool l[1010];

int main(int argc, char* argv[])
{
	uint n, k;
	cin >> n >> k;

	for (uint i = 0; i < n; ++i)
	{
		cin >> s[i];
		l[s[i] - 1] = true;
	}

	uint nk = -1;

	for (uint i = 0; i < n; ++i)
	{
		if (!l[i])
		{
			bool w = false;
			uint x = i + 1,
				 c = 0;

			if (x == k)
				nk = 0, w = true;

			while (x)
			{
				x = s[x - 1];
				++c;
				if (x == k)
					nk = c, w = true;
			}

			if (w)
				nk = c - nk;

			if (!w)
				v[nv++] = c;
		}
	}

	fill(l, l + 1000, false);
	uint mx = nk - 1;
	l[nk - 1] = true;

	for (uint i = 0; i < nv; ++i)
	{
		for (uint j = mx; j != -1; --j)
		{
			if (l[j])
				l[j + v[i]] = true;
		}

		mx += v[i];
	}

	for (uint i = 0; i < 1000; ++i)
	{
		if (l[i])
			cout << i + 1 << endl;
	}

	return 0;
}


// rc.cpp
//

#include <math.h>
#include <string.h>

#include <iterator>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <algorithm>
#include <numeric>

using namespace std;

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned __int64 uint64;

#define PI 3.1415926535897932384626433832795
#define EPS 1E-8

uint s[1000],
	 v[1000],
	 nv;

bool l[1000];

int main(int argc, char* argv[])
{
	uint n, k;
	cin >> n >> k;

	for (uint i = 0; i < n; ++i)
	{
		cin >> s[i];
		l[s[i] - 1] = true;
	}

	uint nk = -1;

	for (uint i = 0; i < n; ++i)
	{
		if (!l[i])
		{
			bool w = false;
			uint x = i + 1,
				 c = 0;

			if (x == k)
				nk = 0, w = true;

			while (x)
			{
				x = s[x - 1];
				++c;
				if (x == k)
					nk = c, w = true;
			}

			if (w)
				nk = c - nk;

			if (!w)
				v[nv++] = c;
		}
	}

	fill(l, l + 1000, false);
	uint mx = nk - 1;
	l[nk - 1] = true;

	for (uint i = 0; i < nv; ++i)
	{
		for (uint j = mx; j != -1; --j)
		{
			if (l[j])
				l[j + v[i]] = true;
		}

		mx += v[i];
	}

	for (uint i = 0; i < 1000; ++i)
	{
		if (l[i])
			cout << i + 1 << endl;
	}

	return 0;
}


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

using namespace std;

vector<int> allSums(vector<int>& a)
{
	int n = accumulate(a.begin(), a.end(), 0);
	sort(a.begin(), a.end());
	vector<bool> sums(n+1, false);
	sums[0] = true;

	for (int i = 0, s = 0; i < a.size(); ++i )
	{
		for (int j = s, k = a[i]; j >= 0; --j)
			if (sums[j])
				sums[j+k] = true;
		s += a[i];
	}
	vector<int> res;
	for (int i = 0; i <= n; ++i)
		if (sums[i])
			res.push_back(i);
	return res;
}

int main()
{
	int n, x;
	cin>>n>>x;
	--x;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		--a[i];
	}

	vector<int> numberOfElements;
	vector<int> number(n, -1);
	for (int i = 0; i < n; ++i)
		if (number[i] == -1)
		{
			int t = i, k = 0;
			while (t != -1 && number[t] == -1)
			{
				t = a[t];
				++k;
			}
			int nn = t == -1 ? numberOfElements.size() : number[t];
			if (nn == numberOfElements.size())
				numberOfElements.push_back(0);
			numberOfElements[nn] += k;

			t = i;
			while (t != -1 && number[t] == -1)
			{
				number[t] = nn;
				t = a[t];
			}
		}

	int t = x, nQue = 1;
	while (a[t] != -1)
	{
		t = a[t];
		++nQue;
	}
	int nn = number[x];
	numberOfElements.erase(numberOfElements.begin() + nn);
	vector<int> s = allSums(numberOfElements);
	for (int i = 0; i < s.size(); ++i)
		cout<<s[i] + nQue<<endl;

	return 0;
}


#pragma comment(linker, "/STACK:512000000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <memory.h>
#include <limits.h>
#include <assert.h>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <functional>
#include <bitset>
#include <stack>
#include <float.h>
#include <time.h>
#include <queue>
#include <deque>

#define PI 3.14159265358979323846
#define ull unsigned long long int
#define ll long long int
#define EPS 1e-9
#define pb push_back
#define EOS(c) (c != EOF && c != '\0' && c != '\n' && c != ' ')
#define oo 1000000005
#define ALL(v) v.begin(), v.end()
#define mp make_pair
#define sqr(a) (a) * (a)

using namespace std;

#define FILES "sort"
//#define OUT "put.txt"

template<class telem>telem MAX(telem a, telem b)
{
    return (a > b) ? a : b;
}

template<class telem>telem MIN(telem a, telem b)
{
    return (a < b) ? a : b;
}

template<class telem>telem GCD(telem a, telem b)
{
    return b ? GCD(b, a % b) : a;
}

template<class telem>telem LCM(telem a, telem b)
{
    return a / GCD(a, b) * b;
}

template<class telem>telem UNSIGNED(telem a)
{
    return (a > 0) ? a : a * (-1);
}

template<class telem>void bez_swop(telem &a, telem &b)
{
    telem c = a;
    a = b;
    b = c;
}

void swop(int &a, int &b)
{
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}

int n;
vector<int> g[1003];
int mx;
int d = 1;
int m;
bool f;
int fst;
vector<int> st;
char was[1003];

void dfs(int v)
{
    if(v == m)
    {
        f = true;
        fst = d;
    }

    mx = max(mx, d);

    d++;

    for(int i = 0; i < g[v].size(); i++)
    {
        dfs(g[v][i]);
    }

    d--;
}

int main()
{

#ifndef ONLINE_JUDGE
#ifdef OUT
    freopen("in"OUT, "r", stdin);
    freopen("out"OUT, "w", stdout);
#endif

#ifdef FILES
    freopen(FILES".in", "r", stdin);
    freopen(FILES".out", "w", stdout);
#endif
#endif

    ios_base::sync_with_stdio(false);
    srand(time(NULL));
    time_t start = clock();

    scanf("%d %d", &n, &m);

    for(int i = 1, a; i <= n; i++)
    {
        scanf("%d", &a);

        g[a].push_back(i);
    }

    for(int i = 0; i < g[0].size(); i++)
    {
        mx = 0;
        f = false;
        dfs(g[0][i]);

        if(!f)
            st.push_back(mx);
    }

    was[0] = 1;

    for(int i = 0; i < st.size(); i++)
    {
        for(int j = n + 1; j >= 0; j--)
        {
            if(was[j])
                was[j + st[i]] = 1;
        }
    }

    for(int i = 0; i <= n; i++)
    {
        if(was[i])
            printf("%d\n", i + fst);
    }

#ifndef ONLINE_JUDGE
    fprintf(stderr, "\n%0.3lf\n", (clock() - start) * 1.0 / CLOCKS_PER_SEC);
#endif

    return 0;
}


#ifndef CANT_USE_TEMPLATE
#define  _CRT_SECURE_NO_WARNINGS
#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
using namespace std;

#define MP make_pair
#define ALL(v) v.begin(), v.end()
#define SZ(v) (int)v.size()
#define sqr(x) ((x)*(x))
#endif

const int N = 3000;
int n, x;
int a[N];
bool can[N][N];

int size[N],
	id[N];
vector < int > v;
set < int > ID;

int root(int v)
{
	if (id[v] == v)
		return id[v];
	return id[v] = root(id[v]);
}

bool isConnected(int u, int v)
{
	return root(u) == root(v);
}

void unite(int u, int v)
{
	if (isConnected(u, v))
		return;
	u = root(u);
	v = root(v);
	if (size[u] > size[v])
	{
		id[v] = u;
		size[u] += size[v];
	}
	else
	{
		id[u] = v;
		size[v] += size[u];
	}
}

void knapsack()
{
	if (v.empty())
		return;
	can[0][v[0]] = true;
	for (int i = 1; i < SZ(v); i++)
	{
		for (int j = 0; j < N; j++)
			can[i][j] = can[i - 1][j];
		for (int j = N - 1; j >= 0; j--)
			if (can[i][j] && j + v[i] < N)
				can[i][j + v[i]] = true;
		can[i][v[i]] = true;
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	for (int i = 0; i < N; i++)
	{
		id[i] = i;
		size[i] = 1;
	}
	cin >> n >> x;
	x--;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] != 0)
			unite(i, a[i] - 1);
	}

	for (int i = 0; i < n; i++)
		if (root(x) != root(i) && ID.find(root(i)) == ID.end())
		{
			ID.insert(root(i));
			v.push_back(size[root(i)]);
		}
	int tmp = 0;
	while (a[x] != 0)
		tmp++, x = a[x] - 1;

	knapsack();
	for (int i = 0; i < n; i++)
	{
		bool flag = tmp == i;
		if (!v.empty())
			for (int j = 0; j < N; j++)
				if (can[SZ(v) - 1][j] && j + tmp == i)
					flag = true;
		if (flag)
			cout << i + 1 << endl;
	}

	return 0;
}


#ifndef CANT_USE_TEMPLATE
#define  _CRT_SECURE_NO_WARNINGS
#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
using namespace std;

#define MP make_pair
#define ALL(v) v.begin(), v.end()
#define SZ(v) (int)v.size()
#define sqr(x) ((x)*(x))
#endif

const int N = 2000;
int n, x;
int a[N];
bool can[N][N];

int size[N],
	id[N];
vector < int > v;
set < int > ID;

int root(int v)
{
	if (id[v] == v)
		return id[v];
	return id[v] = root(id[v]);
}

bool isConnected(int u, int v)
{
	return root(u) == root(v);
}

void unite(int u, int v)
{
	if (isConnected(u, v))
		return;
	u = root(u);
	v = root(v);
	if (size[u] > size[v])
	{
		id[v] = u;
		size[u] += size[v];
	}
	else
	{
		id[u] = v;
		size[v] += size[u];
	}
}

void knapsack()
{
	if (v.empty())
		return;
	can[0][v[0]] = true;
	for (int i = 1; i < SZ(v); i++)
	{
		for (int j = 0; j < N; j++)
			can[i][j] = can[i - 1][j];
		for (int j = N - 1; j >= 0; j--)
			if (can[i][j] && j + v[i] < N)
				can[i][j + v[i]] = true;
		can[i][v[i]] = true;
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	for (int i = 0; i < N; i++)
	{
		id[i] = i;
		size[i] = 1;
	}
	cin >> n >> x;
	x--;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] != 0)
			unite(i, a[i] - 1);
	}

	for (int i = 0; i < n; i++)
		if (root(x) != root(i) && ID.find(root(i)) == ID.end())
		{
			ID.insert(root(i));
			v.push_back(size[root(i)]);
		}
	int tmp = 0;
	while (a[x] != 0)
		tmp++, x = a[x] - 1;

	knapsack();

	for (int i = 0; i < n; i++)
	{
		bool flag = tmp == i;
		for (int j = 0; j < N; j++)
			if (can[SZ(v) - 1][j] && j + tmp == i)
				flag = true;
		if (flag)
			cout << i + 1 << endl;
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cmath>
#include <ctime>
#include <assert.h>
#include <string.h>
#include <limits>
using namespace std;

#define INF 100000000
#define PI 3.1415926535897932384626433832795
#define PI_HALF 1.570796327
#define EPS 1e-9

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair< int, int > PII;
typedef pair< LL, LL > PLL;
typedef vector< int > VI;
typedef vector< LL > VLL;
typedef vector< string > VS;
typedef vector< PII > VPII;
typedef vector< PLL > VPLL;

#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define SZ(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort( ALL( c ) )
#define SORT_(c,f) sort( ALL( c ), f )
#define FILL(x,v) memset( x, v, sizeof( x ) );

#define REP(i,n) for ( int i = 0, max_ = (n); i < max_; ++i )
#define REPD(i,n) for ( int i = (n) - 1; i >= 0; --i )
#define FOR(i,a,b) for ( int i = (a), max_ = (b); i < max_; ++i )
#define FORD(i,a,b) for ( int i = (a) - 1, min_ = (b); i >= min_; --i )

int ToInt ( string & s, int len ) { int r = 0; REP(i,len) { r *= 10; r += s[i] - '0'; } return r; }
int GCD ( int a, int b ) { return b != 0 ? GCD(b, a % b) : a; }
int LCM ( int a, int b ) { return a*(b / GCD(a,b)); }
LL Pow ( LL n, LL e ) { if ( e == 0 ) return 1; if ( e == 1 ) return n; else if ( e & 1 ) { LL t = Pow(n,e/2); return n*t*t; } else { LL t = Pow(n,e/2); return t*t; } }

bool vis[ 3005 ];
int prevv[ 3005 ], nextt[ 3005 ];

int GetSize ( int id )
{
    vis[ id ] = true;
    if ( prevv[ id ] == -1 ) return 1;
    return 1 + GetSize( prevv[ id ] );
}

set< int > res;

bool dp[ 3005 ][ 3005 ];
VI v;
void Solve ( int id, int sum )
{
    if ( dp[ id ][ sum ] ) return;

    dp[ id ][ sum ] = true;
    res.insert( sum );

    if ( id == SZ(v) ) return;

    Solve( id + 1, sum );
    Solve( id + 1, sum + v[ id ] );
}

int main ()
{
    FILL(prevv,-1);
    FILL(nextt,-1);

    int N, X;
    scanf( "%d %d", &N, &X );
    REP(i,N)
    {
        int t;
        scanf( "%d", &t );
        --t;
        prevv[ i ] = t;
        nextt[ t ] = i;
    }

    // vyresimu skupinu v ktere je:
    int sizeX = 0;
    {
        sizeX = GetSize( X - 1 );

        int pos = X - 1;
        while ( nextt[ pos ] != -1 ) pos = nextt[ pos ];
        GetSize( pos );
    }

    // vyresime ostatni skupiny:
    REP(i,N)
    {
        if ( vis[ i ] ) continue;
        vis[ i ] = true;
        if ( nextt[ i ] != -1 ) continue;

        v.PB( GetSize( i ) );
    }

    FILL(dp,false);
    Solve( 0, sizeX );
    auto it = res.begin();
    while ( it != res.end() )
    {
        printf( "%d\n", *it );

        ++it;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <sstream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <limits.h>
#include <queue>
#include <cstring>

using namespace std;
typedef long long ll;

const int mod = 1e9;

int main(){
    set<int> pr;
    set<int> res;
    int n,x;
    cin>>n>>x;
    for(int i=1; i<=n; i++){
        pr.insert(i);
    }
    pr.erase(x);
    vector<int> arr(n);
    for(int i=0; i<n; i++){
        cin>>arr[i];
        pr.erase(arr[i]);
    }
    int f=1;
    int ptr;
    if(arr[x-1] != 0){
        for(ptr=x-1; arr[ptr] != 0; ptr = arr[ptr]-1){
            f++;
        }
    }
    for(set<int>::iterator it = pr.begin(); it!=pr.end(); it++){
        int q = *it;
        int kol = 1;
        bool fl = false;
        int p;
        for(p=q-1; arr[p] != 0; p = arr[p]-1){
            if(p == x-1){
                fl = true;
                break;
            }
            kol++;
        }
        if(fl || p == x-1)
            continue;
        vector<int> buf;
        for(set<int>::iterator j = res.begin(); j != res.end(); j++){
            int r = kol + *j;
            buf.push_back(r);
        }
        for(int i=0; i<buf.size(); i++){
            res.insert(buf[i]);
        }
        res.insert(kol);
    }
    cout<<f<<"\n";
    for(set<int>::iterator it = res.begin(); it != res.end(); it++){
        cout<<(f+*it)<<"\n";
    }
    return 0;
}


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

long long n, s, idx, ans;
long long p[1010];
long long u[1010];
long long d[1010];
long long r[1010];
bool l[1010];

void dfs(long long x, long long am)
{
    if (u[x] == -1)
    {
        if (p[x] == -1)
        {
            u[x] = idx;
            d[x] = 1;
            r[idx] = am;
            idx++;
        }
        else
        {
            dfs(p[x], am + 1);
            u[x] = u[p[x]];
            d[x] = d[p[x]] + 1;
        }
    }
    else
    {
        r[u[x]] += am - 1;
    }
}

int main()
{
    //freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
    cin >> n >> s;

    for (int i = 0; i < n; i++)
    {
        cin >> p[i];
        p[i]--;
        u[i] = -1;
    }
    idx = 0;
    for (int i = 0; i < n; i++)
        if (u[i] == -1)
        {
            if (p[i] == -1)
            {
                u[i] = idx;
                d[i] = 1;
                r[idx] = 1;
                idx++;
            }
            else
            {
                dfs(i, 1);
            }
        }
    s--;
    ans = d[s];
    r[u[s]] = 0;
    l[ans] = 1;
    for (int i = 0; i < idx; i++)
    {
        for (int j = n; j >= ans; j--)
            if (l[j] && j + r[i] <= n)
                l[j + r[i]] = true;
    }

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


#include <iostream>
#include <functional>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

void print(const vector <int>& v)
{
    for (int i = 0; i < v.size(); ++i)
        cout << v[i] << " ";
    cout << endl;
}

int main()
{
    int n, nomer, tex = 0;
    cin >> n >> nomer;
    vector <int> a(n), answer;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 1; i <= n; ++i)
    {
        bool b = true;
        for (int j = 0; j < a.size(); ++j)
            if (a[j] == i)
                b = false;
        if (b)
        {
            int k = i - 1;
            vector <int> t;
            while (a[k] != 0)
            {
                t.push_back(k + 1);
                k = a[k] - 1;
            }
            t.push_back(k + 1);
            //print(t);
            bool tr = true;
            for (int i = 0; i < t.size(); ++i)
                if (t[i] == nomer)
                {
                    tex = t.size() - i;
                    tr = false;
                }
            if (tr)
                answer.push_back(t.size());
        }
    }

    vector <int> knap(n + 1, 0);
    knap[tex] = 1;
    for (int i = 0; i < answer.size(); ++i)
    {
        for (int j = n; j >= 0; --j)
            if (knap[j] == 1 && j + answer[i] <= n)
                knap[j + answer[i]] = 1;
    }
    for (int i = 1; i <= n; ++i)
        if (knap[i] == 1)
            cout << i << endl;

}


#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <climits>

using namespace std;

typedef long long int64;

#define fore(i,a,b) for (int i = (int)a; i < (int)b; i++)
#define forf(i,n) for (int i = 0; i < (int)n; i++)
#define forb(i,n) for (int i = (int)n-1; i >= 0; i--)

#define INF (int)1E9
#define INF64 (int64)1E18

void qs(int *a, int l, int r)
{
	int i = l;
	int j = r;
	int y,p = a[(l+r)>>1];
	do
	{
		while (a[i] < p && i < r) i++;
		while (a[j] > p && j > l) j--;
		if (i <= j)
		{
			y = a[i];
			a[i] = a[j];
			a[j] = y;
			i++;
			j--;
		}
	} while (i <= j);
	if (i < r) qs(a,i,r);
	if (j > l) qs(a,l,j);
}

int n,x,t,p = 0,q = 0;
int *a;
bool *b;
int c;
int *d;
int *e;

int f(int i)
{
	if (i == x)
	{
		if (!a[i]) c = 1;
		else c = 1+f(a[i]);
		return -INF;
	}
	if (!a[i]) return 1;
	else return (1+f(a[i]));
}

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	cin >> n >> x;
	a = new int [n+1];
	b = new bool [n+1];
	d = new int [n];
	e = new int [n];
	forf(i,n+1) b[i] = 0;
	fore(i,1,n+1)
	{
		cin >> a[i];
		b[a[i]] = 1;
	}
	fore(i,1,n+1)
	{
		if (!b[i])
		{
			t = f(i);
			if (t > 0) d[p++] = t;
		}
	}
	qs(d,0,p-1);
	forf(i,n+1) b[i] = 0;
	int mx = 0;
	forf(i,p)
	{
		q = 0;
		fore(j,1,mx+1)
		{
			if (b[j]) e[q++] = j+d[i];
		}
		forf(j,q)
		{
			b[e[j]] = 1;
			if (e[j] > mx) mx = e[j];
		}
		b[d[i]] = 1;
		if (d[i] > mx) mx = d[i];
	}
	cout << c << endl;
	fore(i,1,n+1)
	{
		if (b[i]) cout << i+c << endl;
	}
	return 0;
}


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

int dp[1003], in[1003];
vector<int> V;
int a[1003];

int main()
{
	int n, x, i, cnt, now, flag, z, my, sz;
	int u, j;

	scanf("%d %d", &n, &x);
	for(i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		in[a[i]] = 1;
	}

	for(i = 1; i <= n; i++)
	{
		if(in[i]) continue;

		now = i;
		cnt = 0;
		flag = 0;
		while(now)
		{
			cnt++;
			if(now == x) flag = 1, z = cnt;
			now = a[now];
		}

		if(flag) my = cnt - z;
		else V.push_back(cnt);
	}

	dp[0] = 1;
	sz = V.size();
	for(i = 0; i < sz; i++)
	{
		u = V[i];
		for(j = n - 1; j >= 0; j--)
			if(dp[j] && j + u <= n)
				dp[j + u] = 1;
	}

	for(i = 0; i <= n; i++)
		if(dp[i])
			printf("%d\n", i + my + 1);

	return 0;
}


#define _USE_MATH_DEFINES

#include <cstdio>
#include <iostream>
#include <fstream>
#include <climits>
#include <ctime>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>

using namespace std;

// #define FILE

#define INF INT_MAX
#define LINF LLONG_MAX
#define EPS 1e-9
#define MOD 1000000007 // 10**9 + 7
#define MAXN 100010
#define ALPH 26 // Latin alphabet size
#define DALPH 52 // Double Latin alphabet size

#define mp(a, b) make_pair(a, b)

typedef signed short int int16;
typedef unsigned short int uint16;
typedef unsigned long int uint;
typedef signed long long int int64;
typedef unsigned long long int uint64;
typedef long double real; // in msvc same as double

typedef pair<int, int> pii;

int n, x, k;
vector<int> list, segments, sum;
vector<bool> last;

int main() {
    #ifdef FILE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    scanf("%d %d", &n, &x);
    list.resize(n + 1);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &list[i]);

    last.resize(n + 1, true);
    for(int i = 1; i <= n; ++i)
        last[ list[i] ] = false;

    for(int i = 1; i <= n; ++i) {
        if(!last[i]) continue;
        int t = 0;
        bool wasx = false;

        int ind = i;
        do {
            if(ind == x) t = 0, wasx = true;
            ind = list[ind], ++t;
        } while(ind);

        if(wasx)
            k = t;
        else
            segments.push_back(t);
    }

    sum.resize(3 * n, 0);
    sum[0] = 2;
    for(int i = 0; i < segments.size(); ++i) {
        for(int j = 0; j <= n; ++j)
            if(sum[j] == 2)
                sum[ j + segments[i] ] = max(1, sum[ j + segments[i] ]);

        for(int j = 0; j <= n; ++j)
            if(sum[j] == 1)
                ++sum[j];
    }

    for(int i = 0; i <= n; ++i)
        if(sum[i])
            printf("%d\n", i + k);
}


#include<stdio.h>
int n,y,a[1111];
int next[1111];
bool isStart[1111];
bool g[1111];
int main(){
	scanf("%d%d",&n,&y);
	for(int i=1; i<=n; i++){
		int prev;
		scanf("%d",&prev);
		if(prev!=0)
			next[prev]=i;
		else
			isStart[i]=true;
	}
	g[0]=true;
	int p=-1;
	for(int i=1; i<=n; i++)
		if(isStart[i]){
			int x=0;
			bool flag=false;
			for(int j=i; j; j=next[j]){
				x++;
				if(j==y){
					p=x;
					flag=true;
				}
			}
			if(!flag)
			for(int j=n-x; j>=0; j--)
				if(g[j])g[j+x]=true;
		}
	for(int i=0; i<n; i++)
		if(g[i])
			printf("%d\n",i+p);
	return 0;
}


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

using namespace std;

struct st
{
	int pr;
	int next;
	bool in;
	int id;
};
int main()
{
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	int N;
	cin >> N;
	st q;
	q.next = -1;
	q.in = 0;
	q.pr = -1;
	vector<st>v(N,q);
	int K;
	cin >> K;
	K--;
	for(int i = 0; i < N; i++)
	{
		v[i].id = i;
		int a;
		cin >> a;
		if(a == 0)
		{
			v[i].next = -1;
		}
		else
		{
			a--;
			v[i].next = a;
			v[a].pr = i;
		}
	}
	int e = 1;
	int k = 1;
	vector<int>cake;
	for(int i = 0; i < N; i++)
	{
		if(v[i].in)
			continue;
		st t = v[i];
		while(t.next != -1)
		{
			t = v[t.next];
		}
		int cnt = 1;
		bool is_e = 0;
		while(t.pr != -1)
		{
			v[t.id].in = 1;
			if(t.id == K)
			{
				is_e = 1;
				k = cnt;
			}
			t = v[t.pr];
			cnt++;
		}
		v[t.id].in = 1;
		if(t.id == K)
		{
			is_e = 1;
			k = cnt;
		}
		if(is_e)
			e = cnt;
		else
			cake.push_back(cnt);
	}

	sort(cake.begin(), cake.end());
	int Sum = 0;
	for(int i = 0; i < cake.size(); i++)
	{
		Sum += cake[i];
	}
	vector< vector<bool> >dp(cake.size(), vector<bool>(Sum+1, 0));
	for(int i = 0; i < cake.size(); i++)
	{
		for(int j = 1; j < Sum+1; j++)
		{
			if(j == cake[i])
			{
				dp[i][j] = 1;
				continue;
			}
			if(j < cake[i])
			{
				if(i != 0)
					dp[i][j] = dp[i-1][j];
				else
					dp[i][j] = 0;
				continue;
			}
			if(i != 0)
			{
				dp[i][j] = (dp[i-1][j] || dp[i-1][j-cake[i]]);
				continue;
			}
			else
			{
				dp[i][j] = 0;
			}
		}
	}
	cout << k << endl;
	for(int j = 1; j < Sum+1; j++)
	{
		if(dp[cake.size()-1][j])
			cout << k + j << endl;
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <string>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
#include <map>
#include <iterator>
#include <sstream>
#include <fstream>
#include <list>
#include <set>
#include <stack>
#include <bitset>
#include <ctime>

#pragma comment(linker, "/STACK:256000000")

#define EPS 1e-7

const long double PI = 3.1415926535897932384626433832795;

using namespace std;

int aabs(int a){
	if (a<0) return -a;
	return a;
}

int main(){
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	cout.setf(ios::fixed);
	cout.precision(6);
	int n,k;
	cin>>n>>k;
	k--;
	int a[1013];
	int g[1013];
	memset(g,255,sizeof(g));
	for (int i=0;i<n;i++){
		cin>>a[i];
		a[i]--;
		if (a[i]>=0){
			g[a[i]]=i;
		}
	}
	int nk=-1;
	vector <int> nb;
	for (int i=0;i<n;i++){
		if (a[i]<0){
			bool pp=0;
			int cnt=1;
			if (i==k){
				nk=0;
				pp=1;
			}
			int cr=g[i];
			while (cr!=-1){
				if (cr==k){
					nk=cnt;
					pp=1;
				}
				cnt++;
				cr=g[cr];
			}
			if (!pp){
				nb.push_back(cnt);
			}
		}
	}
	bool d[2013];
	memset(d,0,sizeof(d));
	d[0]=1;
	for (int j=0;j<nb.size();j++){
		for (int i=1000;i>=0;i--){
			if (d[i]){
				d[i+nb[j]]=1;
			}
		}
	}
	for (int i=0;i<=1000;i++){
		if (d[i]){
			cout<<i+nk+1<<'\n';
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <string>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
#include <map>
#include <iterator>
#include <sstream>
#include <fstream>
#include <list>
#include <set>
#include <stack>
#include <bitset>
#include <ctime>

#pragma comment(linker, "/STACK:256000000")

#define EPS 1e-7

const long double PI = 3.1415926535897932384626433832795;

using namespace std;

int aabs(int a){
	if (a<0) return -a;
	return a;
}

int main(){
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	cout.setf(ios::fixed);
	cout.precision(6);
	int n,m;
	cin>>n>>m;
	long long a[113];
	long long f[113];
	f[0]=1;
	f[1]=1;
	long long mm=1000000000;
	for (int i=2;i<113;i++){
		f[i]=(f[i-1]+f[i-2])%mm;
	}
	for (int i=0;i<n;i++){
		cin>>a[i];
	}
	for (int i=0;i<m;i++){
		int t;
		cin>>t;
		if (t==1){
			int x,v;
			cin>>x>>v;
			x--;
			a[x]=v;
		}
		else{
			int l,r;
			cin>>l>>r;
			l--; r--;
			if (t==2){
				long long ans=0;
				for (int i=l;i<=r;i++){
					ans=(ans+a[i]*f[i-l])%mm;
				}
				cout<<ans<<'\n';
			}
			else{
				int d;
				cin>>d;
				for (int j=l;j<=r;j++){
					a[i]=(a[i]+d)%mm;
				}
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
	//freopen( "input.txt", "r", stdin );
	//freopen( "output.txt", "w", stdout );

	int n;
	int pos;
	cin >> n >> pos;
	pos--;
	vector<int> parents;
	for( int i = 0; i < n; i++ ) {
		int pred;
		cin >> pred;
		parents.push_back( i );
		if( pred > 0 ) {
			parents.back() = pred - 1;
		}
	}
	int myPos = 0;
	while( parents[pos] != pos ) {
		pos = parents[pos];
		myPos++;
	}
	int myGroup = pos;
	vector<int> groups( n, 0 );
	for( int i = 0; i < n; i++ ) {
		pos = i;
		while( parents[pos] != pos ) {
			pos = parents[pos];
		}
		if( pos != myGroup ) {
			groups[pos]++;
		}
	}
	vector<int> sum( n + 1, 0 );
	sum[0] = 1;
	for( int i = 0; i < n; i++ ) {
		if( groups[i] == 0 ) {
			continue;
		}
		for( int j = n - 1; j >= 0; j-- ) {
			if( sum[j] != 0 ) {
				sum[j + groups[i]] = 1;
			}
		}
	}
	for( int i = 0; i <n; i++ ) {
		if( sum[i] ) {
			cout << i + myPos + 1 << endl;
		}
	}

	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include<cstdio>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<utility>
#include<memory>

using namespace std;

#define forn(i, n) for(int  i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
typedef long long ll ;

int b[10001], s[10001];
int a[10001];
int c[10001];
vector<int>p;
int main(){
  //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int  x, t, n;
    cin >> n >> x;
    memset(a, -1, sizeof a);
    memset(b, -1, sizeof b);
    forn(i, n){
        cin >> t;
        if(!t)continue;
        t--;
        b[i] = t;
        a[t] = i;
    }
    x--;
    int y = 0, xx = x;
    while(y < n){
        y++;
        if(b[x] == -1)break;
        x = b[x];
    }
    forn(i, n){
        if(b[i] != -1 || i == x)continue;
        int res = 0;
        t = i;
        while(a[t] != -1){
            res++;
            t = a[t];
        }
        p.pb(res + 1);
    }
    s[0] = 1;
    forn(i, (int)p.size()){
        for(int j = n; j >= 0; j--){
            if(s[j])s[j + p[i]] = 1;
        }
    }
    for(int i = 0; i <= n; ++i){
        if(s[i])cout << i + y << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include<cstdio>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<utility>
#include<memory>

using namespace std;

#define forn(i, n) for(int  i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
typedef long long ll ;

int b[1001], s[1001];
int a[1001][1001];
int c[1001], n, res = 0;
vector<int>p;
void dfs(int v){
    c[v] = 1;
    res++;
    forn(i, n){
        if(!c[i] && a[v][i])dfs(i);
    }
}
int main(){
  //  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int  x, t;
    cin >> n >> x;
    forn(i, n){
        cin >> t;
        if(!t)continue;
        t--;
        b[i] = t;
        a[t][i] = 1;
        a[i][t] = 1;
    }
    x--;
    int y = 0, xx = x;
    while(y < n){
        y++;
        if(!b[x])break;
        x = b[x];
    }
    int r = 0;
    forn(i, n){
        if(c[i])continue;
        if(!c[i])dfs(i);
        if(c[xx] && !r){
            r = 1;
            res = 0;
            continue;
        }
        p.pb(res);
        res = 0;
    }
    s[0] = 1;
    forn(i, (int)p.size()){
        for(int j = n; j >= 0; j--){
            if(s[j])s[j + p[i]] = 1;
        }
    }
    for(int i = 0; i <= n; ++i){
        if(s[i])cout << i + y << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <cstdio>

#define sqr(t) ((t)*(t))
#define INF INT_MAX

using namespace std;

map <char, int> aa;

int n, m, k, i, j, arr[10000], opr[10000], ans, c, st, bob, posbob, fordin[10000], din[1001], pp, q;

string s, s2;

int main ()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    cin>>n>>k;
    for (i=1; i<=n; i++)
    {
        cin>>arr[i];
        if (arr[i]==0)
        {
            opr[i]=1;
            if (i==k)
            {
                posbob=1;
                bob=i;
            }
        }
    }
    for (j=1; j<=n; j++)
    for (i=1; i<=n; i++)
    {
        if (arr[i]>0)
            if (opr[arr[i]]>0)
            {
                if (bob==arr[i])
                    bob=i;
                if (i==k)
                {
                    bob=i;
                    posbob=opr[arr[i]]+1;
                }
                opr[i]=opr[arr[i]]+1;
                opr[arr[i]]=0;
                arr[i]=0;
            }
    }
    int maxx=0;
    int sc=n;
    for (i=1; i<=sc; i++)
    {
        if (opr[i]>0)
        if (i!=bob)
        {
            m++;
            fordin[m]=opr[i];
            if (opr[i]>maxx)
                maxx=opr[i];
            if (opr[i]==1)
                pp++;
        }
        else
            n-=opr[i];
    }

    if (pp>=maxx-1)
    {
        for (i=0; i<=n; i++)
            cout<<posbob+i<<' ';
        return 0;
    }

    din[0]=0;

    for (i=0; i<n; i++)
        if ((din[i]>0) || (i==0))
        {
            for (q=din[i]+1; q<=m; q++)
            if ((din[i+fordin[q]]>q) || (din[i+fordin[q]]==0))
                din[i+fordin[q]]=q;
        }
    for (i=0; i<=n; i++)
    if ((din[i]>0) || (i==0))
        cout<<posbob+i<<' ';
}


#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF (1<<30)
#define PI acos(-1.0)
#define ll __int64
using namespace std;

/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/

int num[1100],len;
int beh[1100],n,x,head[1100],save[1100];
bool ans[1100];
int lim;

void dfs(int cur,int sum)
{
   if(cur>lim)
      return ;
   int t=num[cur]+sum;
   ans[t]=true;
   for(int i=cur+1;i<=lim;i++)
      dfs(i,t);
}

int main()
{
   while(scanf("%d%d",&n,&x)!=EOF)
   {

      len=0;
      memset(beh,-1,sizeof(beh));
      memset(num,0,sizeof(num));
      memset(ans,false,sizeof(ans));
      memset(head,0,sizeof(head));

      for(int i=1;i<=n;i++)
      {
         scanf("%d",&save[i]);
         if(save[i])
            beh[save[i]]=i;
         else
            head[++len]=i;
      }
      int order=1,flag=x;
      //printf("save:%d\n",save[x]);
      while(save[x]) //x的前面一个
      {
         x=save[x];
         order++;
         flag=x;
      }
      //printf("order:%d flag:%d\n",order,flag);
      int j=0;
      for(int i=1;i<=len;i++)
      {
         int t=head[i];
         if(t==flag)
            continue;
         ++j;
         while(t!=-1)
         {
            num[j]++;
            t=beh[t];
         }
      }
      ans[order]=true;

      lim=j;
      for(int i=1;i<=j;i++)
         dfs(i,order);
      for(int i=1;i<=n;i++)
      {
         if(ans[i]==false)
            continue;
         printf("%d\n",i);
      }
   }
   return 0;
}


#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF (1<<30)
#define PI acos(-1.0)
#define ll __int64
using namespace std;

/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/

int num[1100],len;
int beh[1100],n,x,head[1100],save[1100];
bool ans[1100];
bool vis[1100];
int lim;

void dfs(int cur,int sum)
{
   if(cur>lim)
      return ;
   int t=num[cur]+sum;
   ans[t]=true;
   for(int i=cur+1;i<=lim;i++)
      dfs(i,t);
}

int main()
{
   while(scanf("%d%d",&n,&x)!=EOF)
   {

      len=0;
      memset(beh,-1,sizeof(beh));
      memset(num,0,sizeof(num));
      memset(ans,false,sizeof(ans));
     // memset(head,0,sizeof(head));
      for(int i=1;i<=n;i++)
      {
         scanf("%d",&save[i]);
         if(save[i])
            beh[save[i]]=i;
         else
            head[++len]=i;
      }
      int order=1,flag=x;
      //printf("save:%d\n",save[x]);
      while(save[x]) //x的前面一个
      {
         x=save[x];
         order++;
         flag=x;
      }
      //printf("order:%d flag:%d\n",order,flag);
      int j=0;
      for(int i=1;i<=len;i++)
      {
         int t=head[i];
         if(t==flag)
            continue;
         ++j;
         while(t!=-1)
         {
            num[j]++;
            t=beh[t];
         }
      }
      ans[order]=true;
     // ans[0]=true;

      lim=j;
      for(int i=1;i<=lim;i++)
      {
         for(int k=n;k>=num[i];k--)
            if(ans[k-num[i]])
               ans[k]=true;
      }
      /*for(int i=1;i<=j;i++)
         dfs(i,order);*/
      for(int i=1;i<=n;i++)
      {
         if(ans[i]==false)
            continue;
         printf("%d\n",i);
      }
   }
   return 0;
}


#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <algorithm>
#include <bitset>
#include <set>

using namespace std;

#define REP(i,n) for(long long int i = 0; i < int(n); ++i)
#define REPV(i, n) for (long long int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(long long int i = (int)(a); i < (int)(b); ++i)

#define ALL(v) (v).begin(), (v).end()
#define PF push_front
#define PB push_back
#define MP make_pair
#define F first
#define S second

#define lli long long int

int a[3000];
int b[3000];
int counts[3000];
int lens[3000];
int used[3000];

int may[3000];

int main()
{
#ifdef FILE_IO
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	int n, x;
	cin >> n >> x;
	int t;
	REP(i, n) {
		cin >> t;
		a[i + 1] = t;
		if (t) b[t] = i + 1;
	}
	int bob = 1;
	int b_pos = x;
	int l_b = b_pos;
	while(1) {
		b_pos = a[b_pos];
		if (b_pos == 0) break;
		bob ++;
		l_b = b_pos;
	}
	used[l_b] = 1 ;

	for(int i = 1; i <= n; ++i) {
		if (!used[i] && a[i] == 0) {
			used[i] = true;
			int l = 1;
			int j = b[i];
			while(j) {
				used[j] = true;
				j = b[j];
				++l;
			}
			counts[i] = l;
		}
	}

	for(int i = 1; i <= n; ++i) {
		if (counts[i]) lens[counts[i]]++;
	}

	vector<pair<int, int>> v;

	for(int i = 1; i <= n; ++i) {
		if(lens[i]) v.push_back(MP(i, lens[i]));
	}

	set<int> pos;

	for(int i = 0; i < v.size(); ++i) {
		set<int> p;
		for(int k = 0; k <= v[i].second; ++k) {
			if (pos.size())
				for(set<int>::iterator it = pos.begin(); it != pos.end(); ++it) {
					p.insert(k * v[i].first + (*it));
				}
			if (k) p.insert(k * v[i].first);
		}
		pos = p;
	}

	cout << bob << endl;
	for(set<int>::iterator it = pos.begin(); it != pos.end(); ++it) {
				cout << (bob + (*it)) << endl;
	}
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <assert.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <math.h>

#define fo(a,b,c) for( a = ( b ); a < ( c ); ++ a )
#define fr(a,b) fo( a, 0, ( b ) )
#define fi(a) fr( i, ( a ) )
#define fj(a) fr( j, ( a ) )
#define fk(a) fr( k, ( a ) )
#define mp make_pair
#define pb push_back
#define all(v) (v).begin( ), (v).end( )
#define _(a,b) memset( a, b, sizeof( a ) )
using namespace std;

void _ad(int &p){ static int i = 0; p = i++; }
#define fi123(x) for_each (all(x),_ad)
#define outv(x) copy(all(x), ostream_iterator<int>(cout," "))

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef map<string,int> msi;

int n, m;
vector<vector<int> > g;
vector<char> mark;
int ansx = -1;
int dfs (int v,int x,int d = 0)
{
	if (v == x - 1)
		ansx = d;
	mark[v] = true;
	for (int i = 0; i < g[v].size(); i++)
	{
		int u = g[v][i];
		if (!mark[u])
		{
			return dfs (u,x,d + 1) + 1;
		}
	}
	return 0;
}

vector<pair<int,int> > dlen;
vector <char> taked;
int D[10000];
vector<int> answer;
int X;
void dfs1 (int p = 0)
{
	vector<bool> h(1001);
	for (int i = 0; i < dlen.size(); i++)
	{
		if (taked[i] == 0)
		{
			if (h[dlen[i].first])
				continue;
			h[dlen[i].first] = true;
			if (dlen[i].second != -1)
			{
				//if (D[i][p + dlen[i].first]  == -1)
				{
					//D[i][p + dlen[i].first] = 0;
					answer.push_back (p + dlen[i].first - dlen[i].second);
				}
			}
			else
			{
				taked[i] = 1;
				//D[i][p + dlen[i].first] = 0;
				dfs1(p + dlen[i].first);
				taked[i] = 0;
			}
		}
	}
}
bool pr (const pair<int,int> &a,const pair<int,int> &b)
{
	if (a.second == b.second)
		return a.first < b.first;
	return a.second > b.second;
}
int dp[1010][1010];
bool cango (int summ,int ex,int id = 0,int mask = 0)
{
	if (summ < 0 || (id == dlen.size() && summ != 0))
		return false;
	if (summ == 0)
		return true;
	if(dp[summ][id] != -1)
		return dp[summ][id];
	bool res = false;
	if (id == ex)
		return cango (summ,ex,id + 1,mask);
	else
	{
		res = cango (summ,ex,id + 1,mask);
		if (res)
			return dp[summ][id] = true;
		res = cango (summ - dlen[id].first,ex,id + 1,mask | 1 << id);
	}
	return dp[summ][id] = res;
}
int main( )
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	//freopen("output.txt", "wt", stdout);
#endif
	int x;
	cin >> n >> x;
	X = x;
	int i,a;
	vector<int> nil;
	g.resize(n);
	fi(n)
	{
		scanf ("%d",&a);
		if (a != 0){
			g[i].push_back (a - 1);
			g[a - 1].push_back (i);
		}
		else
		{
			nil.push_back (i);
		}
	}

	mark.resize (n);
	int id = 0;
	fi(nil.size())
	{
		ansx = -1;
		int len = dfs (nil[i],x) + 1;
		dlen.push_back (mp(len,ansx));
		if (ansx != -1)
			id = i;
	}
	int c;
	_(dp,255);
	for (int i = 0; i < n - dlen[id].first + 1; i++)
	{
		if (i)
			c = i;
		else
			c = n - dlen[id].first;
		if (cango(c,id))
		{
			answer.push_back (i + dlen[id].first - dlen[id].second);
		}
	}
	_(D,255);
	sort (all(answer));
	for (int i = answer.size() - 1; i >= 0; i--)
	{
		printf ("%d\n",n - answer[i] + 1);
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <assert.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <math.h>

#define fo(a,b,c) for( a = ( b ); a < ( c ); ++ a )
#define fr(a,b) fo( a, 0, ( b ) )
#define fi(a) fr( i, ( a ) )
#define fj(a) fr( j, ( a ) )
#define fk(a) fr( k, ( a ) )
#define mp make_pair
#define pb push_back
#define all(v) (v).begin( ), (v).end( )
#define _(a,b) memset( a, b, sizeof( a ) )
using namespace std;

void _ad(int &p){ static int i = 0; p = i++; }
#define fi123(x) for_each (all(x),_ad)
#define outv(x) copy(all(x), ostream_iterator<int>(cout," "))

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef map<string,int> msi;

int n, m;
vector<vector<int> > g;
vector<char> mark;
int ansx = -1;
int dfs (int v,int x,int d = 0)
{
	if (v == x - 1)
		ansx = d;
	mark[v] = true;
	for (int i = 0; i < g[v].size(); i++)
	{
		int u = g[v][i];
		if (!mark[u])
		{
			return dfs (u,x,d + 1) + 1;
		}
	}
	return 0;
}

vector<pair<int,int> > dlen;
vector <char> taked;
int D[10000];
vector<int> answer;
int X;
void dfs1 (int p = 0)
{
	D[p] = 0;
	for (int i = 0; i < dlen.size(); i++)
	{
		if (D[p + dlen[i].first] == -1 && taked[i] == 0)
		{
			if (dlen[i].second != -1)
			{
				answer.push_back (p + dlen[i].first - dlen[i].second);
			}
			else
			{
				taked[i] = 1;
				dfs1(p + dlen[i].first);
				taked[i] = 0;
			}
		}
	}
}
int main( )
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	//freopen("output.txt", "wt", stdout);
#endif
	int x;
	cin >> n >> x;
	X = x;
	int i,a;
	vector<int> nil;
	g.resize(n);
	fi(n)
	{
		scanf ("%d",&a);
		if (a != 0){
			g[i].push_back (a - 1);
			g[a - 1].push_back (i);
		}
		else
		{
			nil.push_back (i);
		}
	}

	mark.resize (n);
	fi(nil.size())
	{
		ansx = -1;
		int len = dfs (nil[i],x) + 1;
		dlen.push_back (mp(len,ansx));
	}
	_(D,255);
	taked.resize(n);
	dfs1();
	sort (all(answer));
	for (int i = answer.size() - 1; i >= 0; i--)
	{
		printf ("%d\n",n - answer[i] + 1);
	}
	return 0;
}


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

#define pb push_back

using namespace std;

bool done[2000];
bool ok[1500][1500];
vector<int> res;

int number[2000];
bool start[2000];
vector< vector<int> > a;
int n, x, cur;
vector<int> tmp;
int mas[2000];
int x_number, x_where;
vector<int> cnt;

void process(int i) {
	if(!done[i]) {
		cur = mas[i];
		if(cur == 0) {
			start[i] = 1;
			number[i] = a.size();
			tmp.clear();
			tmp.pb(i);
			a.pb(tmp);
			done[i] = 1;
			if(i == x-1) {
				x_number = 1;
				x_where = number[i];
			}
		} else {
			if(done[cur-1]) {
				number[i] = number[cur-1];
				a[number[i]].pb(i);
			} else {
				int q = cur-1;
				process(q);
				number[i] = number[q];
				a[number[i]].pb(i);
			}
			done[i] = 1;
			if(i == x-1) {
				x_number = a[number[i]].size();
				x_where = number[i];
			}
		}
	}
}

int main() {
	/*freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);*/

	for(int i = 0; i < 2000; i++)
		number[i] = -1;

	cin >> n >> x;
	a.clear();

	for(int i = 0; i < n; i++)
		cin >> mas[i];

	for(int i = 0; i < n; i++) {
		process(i);
	}

	for(int i = 0; i < a.size(); i++) {
		if(i != x_where) {
			cnt.pb(a[i].size());
		}
	}

	for(int i = 0; i < cnt.size(); i++) {
		for(int j = 1; j <= n; j++) {
			if(ok[i][j]){
				ok[i+1][j] = 1;
				ok[i+1][j+cnt[i]] = 1;
			}
		}
		ok[i+1][cnt[i]] = 1;
	}

	res.clear();
	res.pb(0);
	for(int i = 1; i <= n; i++) {
		if(ok[cnt.size()][i]) {
			res.pb(i);
		}
	}

	sort(res.begin(), res.end());

	for(int i = 0; i < res.size(); i++)
		cout << res[i] + x_number << endl;

	/*for(int i = 0; i < a.size(); i++) {
		for(int j = 0; j < a[i].size(); j++)
			cout << a[i][j] << ' ';
		cout << endl;
	}

	cout << x_number << ' ' << x_where;*/

	return 0;
}


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

int n, x;
int mas[2000];
int color[2000];
bool used[2000];
int cnt[2000];
vector <int> ansv;
vector <int> v;
vector < vector <int> > dp;

int dfs (int v)
{
    if (mas[v] == 0)
    {
        return 1;
    }
    return dfs(mas[v]) + 1;
}

int dfs2 (int v)
{
    if (mas[v] == 0)
    {
        return v;
    }
    return dfs2(mas[v]);
}

int main()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> mas[i];

    for (int i = 1; i <= n; i++)
    {
        cnt[dfs2(i)]++;
    }
    for (int i = 1; i <= n; i++)
    {
        if (cnt[i] != 0 && dfs2(x) != i)
            v.push_back(i);
    }

    int ans = dfs(x) - 1;

    vector <int> tmpv;
    for (int i = 0; i < 2000; i++)
        dp.push_back(tmpv);
    dp[0].push_back(1);

    for (int i = 0; i < v.size(); i++)
    {
        for (int j = 0; j < dp[i].size(); j++)
        {
            dp[i + 1].push_back(dp[i][j] + cnt[v[i]]);
            dp[i + 1].push_back(dp[i][j]);
        }
    }

    for (int i = 0; i <= v.size(); i++)
    {
        for (int j = 0; j < dp[i].size(); j++)
        {
            ansv.push_back(dp[i][j] + ans);
        }
    }

    sort(ansv.begin(), ansv.end());

    for (int i = 0; i < ansv.size(); i++)
    {
        cout << ansv[i] << "\n";
        while(i + 1 < ansv.size() && ansv[i + 1] == ansv[i])
            i++;
    }

    return 0;
}


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

int n, x;
int mas[2000];
int color[2000];
bool used[2000];
int cnt[2000];
vector <int> ansv;
vector <int> v;
bool dp[2000][2000];

int dfs (int v)
{
    if (mas[v] == 0)
    {
        return 1;
    }
    return dfs(mas[v]) + 1;
}

int dfs2 (int v)
{
    if (mas[v] == 0)
    {
        return v;
    }
    return dfs2(mas[v]);
}

int main()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> mas[i];

    for (int i = 1; i <= n; i++)
    {
        cnt[dfs2(i)]++;
    }
    for (int i = 1; i <= n; i++)
    {
        if (cnt[i] != 0 && dfs2(x) != i)
            v.push_back(i);
    }

    int ans = dfs(x) - 1;

    dp[0][1] = true;

    for (int i = 0; i < v.size(); i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (dp[i][j])
            {
                dp[i + 1][j + cnt[v[i]]] = true;
                dp[i + 1][j] = true;
            }
        }
    }

    for (int i = 0; i <= v.size(); i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (dp[i][j])
                ansv.push_back(j + ans);
        }
    }
    sort(ansv.begin(), ansv.end());

    for (int i = 0; i < ansv.size(); i++)
    {
        cout << ansv[i] << "\n";
        while(i + 1 < ansv.size() && ansv[i + 1] == ansv[i])
            i++;
    }

    return 0;
}


#ifdef ONLINE_JUDGE
#include <iostream>
#else
#include <fstream>
#endif
#include<vector>
using namespace std;
 long long n;
 long long x;
 long long b[10100];
 long long c[10100];
 long long count=0;
 long long cbob;
vector< long long> g[10100];
bool used[10100];
vector< long long> comp;
 long long k=0;

void dfs ( long long v) {
	used[v] = true;
	comp.push_back (v);
	if (v==x)
	{
		cbob=count;
	}
	for (size_t i=0; i<g[v].size(); ++i) {
		 long long to = g[v][i];
		if (! used[to])
			dfs (to);
	}
}

void find_comps() {

	for ( long long i=0; i<n; ++i)
		if (! used[i]) {
			comp.clear();

			dfs (i);
			c[count]=( long long)comp.size();
			count++;

		}
}
void gdebobr(long long i)
{
	k++;
	if (b[i]!=0)
	{
		gdebobr(b[i]-1);
	}

}
int main()
{
#ifndef ONLINE_JUDGE
#pragma comment(linker, "/STACK:268435456")
 ifstream cin ("input.txt");
 ofstream cout ("output.txt");
 #endif
 cin>>n>>x;
 x--;
 for( long long i=0; i<n; i++)
 {
	used[i]=false;
	cin>>b[i];
	if (b[i]!=0)
	{
	//b[i]--;
	g[b[i]-1].push_back(i);
	g[i].push_back(b[i]-1);
	}

 }

find_comps();
 long long d[10100];
for( long long i=0; i<10100; i++)
{
	d[i]=0;
}

 long long num=0;
 long long first=0;
for( long long i=0; i<count; i++)
{
if (cbob==i)
{

}
else
{
	d[num]=c[i];
	num++;
}
}

for( long long i=0; i<num; i++)
{
	for( long long j=0; j<num-1; j++)
	{
		if (d[j]>d[j+1]) { long long gg=d[j]; d[j]=d[j+1]; d[j+1]=gg;}
	}
}

gdebobr(x);
first=k;
bool may[1009];
for( long long i=1; i<=n; i++)
{
	may[i]=false;
}
may[first]=true;
for( long long i=0; i<num; i++)
{
	for( long long j=n; j>0; j--)
	{
		if (may[j])
		{
			if (j+d[i]<=n) {may[j+d[i]]=true;}
		}
	}
}
for( long long i=1; i<=n; i++)
{
	if (may[i]) {cout<<i<<endl;}
}

#ifndef ONLINE_JUDGE
cin.close();
cout.close();
#endif
return 0;
}


#ifdef ONLINE_JUDGE
#include <iostream>
#else
#include <fstream>
#endif
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
int n;
int x;
int b[1010];
int c[1010];
int count=0;
int cbob=-1;
int numbob=-1;
vector<int> g[1000];
bool used[1000];
vector<int> comp;
int k=0;

void dfs (int v,int k) {
	used[v] = true;
	comp.push_back (v);
	if (v==x)
	{
		cbob=count;
		numbob=k;
	}
	for (size_t i=0; i<g[v].size(); ++i) {
		int to = g[v][i];
		if (! used[to])
			dfs (to,k+1);
	}
}

void find_comps1() {

	for (int i=0; i<n; ++i)
		if (! used[i]) {
			comp.clear();
			if (g[i].size()!=0)
			{
			dfs (i,0);
			c[count]=(int)comp.size();
			count++;
			}
		}
}
void find_comps2() {

	for (int i=0; i<n; ++i)
		if (! used[i]) {
			comp.clear();

			dfs (i,0);
			c[count]=(int)comp.size();
			count++;

		}
}
void gdebobr(int i)
{
	k++;
	if (b[i]!=0)
	{
		gdebobr(b[i]);
	}

}
int main()
{
#ifndef ONLINE_JUDGE
#pragma comment(linker, "/STACK:268435456")
 ifstream cin ("input.txt");
 ofstream cout ("output.txt");
 #endif
 cin>>n>>x;
 x--;
 for(int i=0; i<n; i++)
 {
	used[i]=false;
	cin>>b[i];
	if (b[i]!=0)
	{
	b[i]--;
	g[b[i]].push_back(i);
	g[i].push_back(b[i]);
	}
	//g1[b[i]].push_back(i);

 }
find_comps1();
find_comps2();
int d[1010];
int num=0;
int first=0;
for(int i=0; i<count; i++)
{
if (cbob==i)
{
	 first=c[cbob]-numbob;
}
else
{
	d[num]=c[i];
	num++;
}
}

for(int i=0; i<num; i++)
{
	for(int j=0; j<num-1; j++)
	{
		if (d[j]>d[j+1]) {int gg=d[j]; d[j]=d[j+1]; d[j+1]=gg;}
	}
}

gdebobr(x);
first=k;
bool may[1009];
for(int i=1; i<=n; i++)
{
	may[i]=false;
}
may[first]=true;
for(int i=0; i<num; i++)
{
	for(int j=n; j>0; j--)
	{
		if (may[j])
		{
			may[j+d[i]]=true;
		}
	}
}
for(int i=1; i<=n; i++)
{
	if (may[i]) {cout<<i<<endl;}
}

#ifndef ONLINE_JUDGE
cin.close();
cout.close();
#endif
return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

typedef long long LL;
typedef vector<int> vi;
typedef unsigned ui32;

#define FOR(i, n) for (int i = 0; i < (int)(n); ++i)

int Next[1024];

int main()
{
    int n, k;
    cin >> n >> k;
    vi v;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (x == 0)
            v.push_back(i);
        else
            Next[x] = i;
    }
    vi g;
    int bpos = -1;
    FOR(i, v.size())
    {
        int pos = -1;
        int t = v[i];
        int cpos = 0;
        while (t) {
            if (t == k)
                pos = cpos;
            cpos++;
            t = Next[t];
        }
        if (pos != -1)
            bpos = pos;
        else
            g.push_back(cpos);
    }
    vi a(2000, 0);
    a[0] = 1;
    FOR(i, g.size()) {
        for (int j = 1100; j >= 0; j--)
            if (a[j])
                a[j + g[i]] = 1;
    }
    FOR(i, 1000)
        if (a[i])
            cout << (i + bpos + 1) << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

typedef long long LL;
typedef vector<int> vi;
typedef unsigned ui32;

#define FOR(i, n) for (int i = 0; i < (int)(n); ++i)

int next[1024];

int main()
{
    int n, k;
    cin >> n >> k;
    vi v;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (x == 0)
            v.push_back(i);
        else
            next[x] = i;
    }
    vi g;
    int bpos = -1;
    FOR(i, v.size())
    {
        int pos = -1;
        int t = v[i];
        int cpos = 0;
        while (t) {
            if (t == k)
                pos = cpos;
            cpos++;
            t = next[t];
        }
        if (pos != -1)
            bpos = pos;
        else
            g.push_back(cpos);
    }
    vi a(2000, 0);
    a[0] = 1;
    FOR(i, g.size()) {
        for (int j = 1100; j >= 0; j--)
            if (a[j])
                a[j + g[i]] = 1;
    }
    FOR(i, 1000)
        if (a[i])
            cout << (i + bpos + 1) << endl;

    return 0;
}


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

int n, x;
int pre[1100];
int net[1100];
bool hah[1100];
vector<int> val;

int main()
{
    while (cin >> n >> x)
    {
        memset (pre, -1, sizeof(pre));
        memset (net, -1, sizeof(net));
        memset (hah, false, sizeof(hah));
        for (int i = 1; i <= n; i++)
        {
            int a;
            scanf("%d", &a);
            if (a != 0)
            {
                pre[i] = a;
                net[a] = i;
            }
        }
        val.clear();
        for (int i = 1; i <= n; i++)
        {
            if (pre[i] == -1)
            {
                int ct = 0;
                int a = i;
                bool f = true;
                while (a != -1)
                {
                    if (a == x)
                    {
                        f = false;
                    }
                    ct++;
                    a = net[a];
                }
                if (f == true)
                {
                    val.push_back(ct);
                }
            }
        }
        sort (val.begin(), val.end());
        int sum = 0;
        hah[0] = true;
        for (int i = 0; i < val.size(); i++)
        {
            for (int j = 0; j <= sum; j++)
            {
                if (hah[j] == true)
                {
                    hah[j + val[i]] = true;
                }
            }
            sum += val[i];
        }
        int n1 = 0;
        int b = x;
        while (b != -1)
        {
            n1++;
            b = pre[b];
        }
        for (int i = 0; i <= sum; i++)
        {
            if (hah[i] == true)
            {
                printf("%d\n", n1 + i);
            }
        }
    }
    return 0;
}


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

int n, x;
int pre[1100];
int net[1100];
bool hah[1100];
vector<int> val;

int main()
{
    while (cin >> n >> x)
    {
        memset (pre, -1, sizeof(pre));
        memset (net, -1, sizeof(net));
        memset (hah, false, sizeof(hah));
        for (int i = 1; i <= n; i++)
        {
            int a;
            scanf("%d", &a);
            if (a != 0)
            {
                pre[i] = a;
                net[a] = i;
            }
        }
        val.clear();
        for (int i = 1; i <= n; i++)
        {
            if (pre[i] == -1)
            {
                int ct = 0;
                int a = i;
                bool f = true;
                while (a != -1)
                {
                    if (a == x)
                    {
                        f = false;
                    }
                    ct++;
                    a = net[a];
                }
                if (f == true)
                {
                    val.push_back(ct);
                }
            }
        }
        sort (val.begin(), val.end());
        hah[0] = true;
        for (int i = 0; i < val.size(); i++)
        {
            for (int j = n; j >= 0; j--)
            {
                if (hah[j] == true)
                {
                    hah[j + val[i]] = true;
                }
            }
        }
        int n1 = 0;
        int b = x;
        while (b != -1)
        {
            n1++;
            b = pre[b];
        }
        for (int i = 0; i <= n; i++)
        {
            if (hah[i] == true)
            {
                printf("%d\n", n1 + i);
            }
        }
    }
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <vector>

#define nextLine() {for(int c=getchar(); c!='\n' && c!=EOF; c=getchar());}
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

#define TASK "file"

using namespace std;

#define pii pair<int,int>
#define mp(x, y) make_pair(x, y)
#define pb(_o_) push_back(_o_)

typedef long long LL;
typedef unsigned long long ULL;
typedef long double ldb;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<int> vb;
typedef vector<vi> vvb;
typedef list<int> li;
typedef multiset<int>::iterator siit;

const int inf = (1 << 30) - 1;
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));

#ifndef ONLINE_JUDGE
#define parr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";puts("");}puts("");
#else
#define parr(a,n,m) for(int i=0;i<2;i++)i++;
#endif

void ML()
{
    int *ass;
    for (;;)
    {
        ass = new int[2500000];
        for (int i = 0; i < 2500000; i++)
            ass[i] = rand();
    }
}

int n, k;
int nxt[1030];
int prv[1030];

void LoAd()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &nxt[i]);
        prv[nxt[i]] = i;
    }
}
bool was[1030];

int dfs(const int v)
{
    if (0 == v)
        return 0;
    if (was[v])
        return -100000000;
    was[v] = true;
    return 1 + dfs(nxt[v]);
}

vi lens;

bool isres[1030];
bool was2[1030][1030];

void rec(const int v, const int sum)
{
    if (was2[v][sum])
        return;

    if (v == (int)lens.size())
    {
        isres[sum] = true;
        return;
    }
    rec(v + 1, sum);
    rec(v + 1, sum + lens[v]);
    was2[v][sum] = true;
}

void SoLvE()
{
    int minlen = dfs(k);

    for (int i = 1; i <= n; i++)
    {
        if (0 == prv[i])
        {
            int tmp = dfs(i);
            if (tmp > 0)
                lens.push_back(tmp);
        }
    }
    rec(0, minlen);
    for (int i = 1; i <= n; i++)
    {
        if (isres[i])
            printf("%d\n", i);
    }
}

int main()
{
    srand( (int) time(NULL));
#ifndef ONLINE_JUDGE
    freopen(TASK".in","r",stdin);   freopen(TASK".out","w",stdout);
#endif
    LoAd();
    SoLvE();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#pragma comment (linker, "/STACK:1000000000")

using namespace std;
int a[1000];
bool was[1000];
int level[1000];
int color[1000];
int c[1000];
int cnt = 0;

void dfs(int u)
{
    was[u] = true;
    if (a[u] != 0)
    {
        if (was[a[u] - 1] == false)
            dfs(a[u] - 1);
        level[u] = level[a[u] - 1] + 1;
        color[u] = color[a[u] - 1];
        c[color[u]] = u;
    }
    else
    {
        level[u] = 1, color[u] = cnt++;
        c[color[u]] = u;
    }
}

int main()
{
    int n, x;
    cin >> n >> x;
    bool dp[1001];
    for (int i = 0; i < n; i++) cin >> a[i], was[i] = false, dp[i] = false;
    dp[n] = false;
    for (int i = 0; i < n; i++)
        if (was[i] == false)
            dfs(i);
    int y = level[x - 1];
    vector <int> knapsack;
    for (int i = 0; i < cnt; i++)
    {
        if (color[x - 1] == i) continue;
        knapsack.push_back(level[c[i]]);
    }
    dp[0] = true;
    for (int i = 0; i < knapsack.size(); i++)
    {
        for (int j = n; j >= 0; j--)
            if (j + knapsack[i] <= n)
                dp[j + knapsack[i]] |= dp[j];
    }
    for (int i = 0; i <= n; i++)
        if (dp[i])
            cout << i + y << endl;
    y = y;
    return 0;
}


#pragma comment(linker,"/stack:67108864")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <sstream>
#include <fstream>
#include <climits>
#include <cfloat>
#include <functional>
#include <ctime>
#include <numeric>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <bitset>
#include <string>
#include <list>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <typeinfo>
#include <cassert>
#include <ctime>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define forn1(i, n) for(int i = 1; i <= int(n); ++i)
#define ford(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define ford1(i, n) for(int i = int(n); i > 0; --i)
#define foran(i, a, n) for(int i = int(a); i <= int(n); ++i)

#define mp make_pair
#define pb push_back
#define sqr(x) (x)*(x)
#define all(v) (v).begin(),(v).end()

typedef long double ld;
typedef long long ll;

const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;
const ll INF = 1000 * 1000 * 1000 + 9;
const int NMAX = 2*1000 + 300;

int n;
int myidx;
int ind[NMAX];
int arr[NMAX];
int ng[NMAX];
int gs[NMAX];
vector <int> g[NMAX];

inline bool read()
{
    memset(ind, 0, sizeof(ind));
    memset(ng, 0, sizeof(ng));
    memset(gs, 0, sizeof(gs));
    if (!(cin >>n >>myidx))
        return false;
    forn(i, n)
    {
        g[i].clear();
        cin >>arr[i];
    }
    return true;
}

inline void solve()
{
    int start = 0, myleader = myidx;
    myidx = arr[myidx - 1];
    while(myidx != 0)
    {
        start++;
        myleader = myidx;
        myidx = arr[myidx - 1];
    }
    bool d[NMAX];
    memset(d, 0, sizeof d);
    forn(i, n)
    {
        if (arr[i] != 0)
            g[arr[i] - 1].push_back(i);
    }
    d[start] = 1;
    forn(i, n)
    {
        if (arr[i] != 0)
            continue;
        if (i == myleader - 1)
            continue;
        int j = i;
        int sz = 1;
        while(g[j].size() == 1)
            j = g[j][0], sz++;
        ford(pos, n)
            d[pos + sz] |= d[pos];
    }
    forn(i, n)
        if (d[i])
            printf("%d ", i + 1);
    puts("");
}

int main()
{
    ifstream ifile("input.txt");
    if (ifile) {
        freopen("input.txt", "rt", stdin);
    }
#ifdef ONLINE_JUDGE
    if (ifile) {
        freopen("output.txt","wt",stdout);
    }
#endif
    while(read())
        solve();
    return 0;
}


/*
ID: kapitan1
PROG:
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
int n,x,y,q,cnt,ind,self,b;
int mark[1000000],mas[1000000],ans[1000000],nextt[1000000],prevv[1000000];
int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>mark[i];
		{
			if(mark[i]!=0)
			{
				nextt[i]=mark[i];
				prevv[mark[i]]=i;
			}
		}
	}
	q=0;
	self=0;
	for(int i=1;i<=n;i++)
	{
		ind=0;
		if(prevv[i]==0)
		{
			cnt=1;
			b=i;
			while(1)
			{
				if(b==x)
				{
					ind=1;
					self=cnt;
				}
				if(nextt[b]!=0)
				{
					b=nextt[b];
					cnt++;
				}
				else
				{
					break;
				}

			}
			if(ind==0)
			{
				q++;
				mas[q]=cnt;
			}
			else
			{
				self = cnt-self+1;
			}
		}
	}

	/////////////

	ans[0]=1;
	for(int i=1;i<=q;i++)
	{
		for(int j=n;j>=mas[i];j--)
		{
			if(ans[j-mas[i]])
			{
				ans[j]=1;
			}
		}
	}

	for(int i=0;i<=n;i++)
	{
		if(ans[i]==1)
		{
			cout<<i+self<<endl;
		}
	}

 	return 0;
}


/*
ID: kapitan1
PROG:
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
int n,x,y,q,cnt,ind,self,b;
int mark[1000000],mas[1000000],ans[1000000],next[1000000],prev[1000000];
int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>mark[i];
		{
			if(mark[i]!=0)
			{
				next[i]=mark[i];
				prev[mark[i]]=i;
			}
		}
	}
	q=0;
	self=0;
	for(int i=1;i<=n;i++)
	{
		ind=0;
		if(prev[i]==0)
		{
			cnt=1;
			b=i;
			while(1)
			{
				if(b==x)
				{
					ind=1;
					self=cnt;
				}
				if(next[b]!=0)
				{
					b=next[b];
					cnt++;
				}
				else
				{
					break;
				}

			}
			if(ind==0)
			{
				q++;
				mas[q]=cnt;
			}
			else
			{

			}
		}
	}

	/////////////

	ans[0]=1;
	for(int i=1;i<=q;i++)
	{
		for(int j=n;j>=mas[i];j--)
		{
			if(ans[j-mas[i]])
			{
				ans[j]=1;
			}
		}
	}

	for(int i=0;i<=n;i++)
	{
		if(ans[i]==1)
		{
			cout<<i+self<<endl;
		}
	}

 	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <cmath>
#include <sstream>
#include <utility>
#include <ctime>
#include <memory.h>
#include <cctype>
#include <cstdlib>
#include <limits>

using namespace std;

#pragma comment (linker, "/STACK:64000000")

#define y0 qwertyuiop
#define y1 asdfghjkl
#define forn(j, n) for(j=1; j<=n; j++)
#define sz size()
#define pb push_back
#define fors(w, s) for(set <int> :: iterator w=s.begin(); w!=s.end(); w++)
#define pii pair<int, int>
#define uns unsigned
#define vi vector <int>
#define vvi vector <vi>
#define inf 2000000000
#define o 1000000007
#define x first
#define y second

typedef long long ll;

int j, n, k, l, p, i, h, m, mn=inf, mx, sum, q, q1, q2, q3, a[100500], d[100500], pr[100500], r[100500], f[100500];
bool b[100500], pos[100500], din[1010][1010];

void tg ()
{
	freopen ("output.txt","r",stdin); freopen ("input.txt","w",stdout);

}

void rec (int i, int sum)
{
	if (i>l)
	{
		pos[sum+r[k]]=1;
		return;
	}
	rec (i+1, sum);
	if (i!=d[k])
	{
		rec (i+1, sum+f[i]);
	}
	return;
}

int main()
{
	//tg (); return 0;
	//freopen ("input.txt","r",stdin); freopen ("output.txt","w",stdout);
    //freopen ("karlsson.in","r",stdin); freopen ("karlsson.out","w",stdout);
	scanf ("%d%d", &n, &k);
	forn (j, n)
	{
		scanf ("%d", &a[j]);
		pr[a[j]]=j;
	}
	forn (j, n)
	{
		if (!d[j] & !a[j])
		{
			q=1;
			r[j]=1;
			d[j]=++l;
			p=pr[j];
			while (p)
			{
				q++;
				r[p]=q;
				d[p]=l;
				p=pr[p];
			}
		}
	}
	forn (j, n)
	{
		if (!pr[j])
		{
			f[d[j]]=r[j];
		}
	}
	din[0][0]=1;
	f[d[k]]=0;
	printf ("%d\n", r[k]);
	forn (j, l)
	{
		for (i=0; i<=n; i++)
		{
			if (din[j-1][i])
			{
				din[j][i]=1;
			}
		}
		for (i=f[j]; i<=n; i++)
		{
			if (din[j-1][i-f[j]])
			{
				din[j][i]=1;
			}
		}
	}
	forn (j, n)
	{
		if (din[l][j])
		{
			printf ("%d\n", j+r[k]);
		}
	}
	return 0;
}


//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>
using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
//#define X first
//#define Y second

int p[1001],rev[1001];
bool us[1001];

int dp[1001][1001];
int main()
{
	//ios_base::sync_with_stdio(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	int x;
	cin>>n>>x;
	FOR(i,1,n+1)
	{
		scanf("%d",&p[i]);
		if(p[i]>0)
			rev[p[i]]=i;

	}
	int me=1;
	int xx=x;
	while(p[xx]!=0)
	{
		us[xx]=1;
		xx=p[xx];
		me++;
	}
	us[xx]=1;
	vector<int> comp;
	FOR(i,1,n+1)
		if(rev[i]==0&&!us[i])
		{
			int xx=i,k=1;
			while(p[xx]!=0)
			{
				if(xx==x||us[p[xx]])goto label;
				k++;
				xx=p[xx];
			}
			comp.push_back(k);
			label:;
		}
	int k=comp.size();
	for(int i=1;i<=k;++i)
		FOR(j,0,n+1)
		if(j>=comp[i-1])
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-comp[i-1]] + comp[i-1]);
		else dp[i][j]=dp[i-1][j];
	printf("%d\n",me);
	FOR(i,1,n+1)
		if(dp[k][i]==i)if(i+me<=n)printf("%d\n",i+me);
	return 0;
}