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

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

#include<cstdio>
#include<set>
using namespace std;
const int MAXN=1010;
int pai[MAXN];//FATHER
int rank[MAXN];//RANK
int val[MAXN];//VALUE OF EACH CONJ
int coin[MAXN];//KNAPSACK
int dp[MAXN];
int pai2[MAXN];//
set <int> conj;

//UNION-FIND
int find(int a){
    if(pai[a]==a)return a;
    return pai[a]=find(pai[a]);
    
}
void unit(int a,int b){
     int ax=find(a);
     int bx=find(b);
     if(pai[ax]==pai[bx])return;
     else{
       if(rank[ax]>rank[bx]){
     pai[bx]=ax;
     
     val[ax]+=val[bx];
       }
       else if(rank[ax]<rank[bx]){
     pai[ax]=bx;
     val[bx]+=val[ax];
       }
       else {
     rank[ax]++;
     pai[bx]=ax;
     val[ax]+=val[bx];
       }
     }
}
int find2(int a){
  if(a==0)
  return 0;
  else{
    return find2(pai2[a])+1;
  }
}
int main(){
  int N,M;
  scanf("%d %d",&N,&M);
  for(int i=1;i<=N;i++){
    pai[i]=i;
    rank[i]=1;
    val[i]=1;
  }
  for(int i=1;i<=N;i++){
    int a;
    scanf("%d",&a);
     pai2[i]=a;
    if(a!=0){
      unit(i,a);
    }
  }

  int aux=0;
  conj.insert(pai[M]);
  for(int i=1;i<=N;i++){
    if(conj.find(find(i))==conj.end()){
      coin[aux]=val[find(i)];
      aux++;
      conj.insert(find(i));
    }
  }
  dp[find2(M)]=1;
  /*for(int i=0;i<aux;i++){
    printf("%d\n",coin[i]);
  }*/
  for(int i=0;i<aux;i++){
    for(int j=N;j>=1;j--){
      if(dp[j]==1&&j+coin[i]<=N){
            dp[j+coin[i]]=1;
      }
    }  
  }
  for(int i=1;i<=N;i++){
    if(dp[i]==1)printf("%d\n",i);
  }
}