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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef   long long int ll;
typedef unsigned long long int sll;
typedef  double ld;
typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define um unordered_multiset
#define us unordered_set
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define cc continue
#define br break
#define rem 1000000007
#define PI 3.1415926535
 
ll n,m=0,k=0,r=0,cnt=0;
 ll dis[200005]={0};
 ll dis1[200005]={0};
 
 ve <ll> adj[200005];
ll b[200005];
 
ll a[200005];
 bool vis[200005]={0};
 bool vis1[200005]={0};
 ve <ll> v;
 bool prime[15000006];
 queue <ll> q;
 
 
 
ll power(ll x,  ll y, ll p) 
{ 
    ll res = 1;      // Initialize result 
  
    x = x % p;  // Update x if it is more than or  
                // equal to p 
  
    while (y > 0) 
    { 
        // If y is odd, multiply x with result 
        if (y & 1) 
            res = (res*x) % p; 
  
        // y must be even now 
        y = y>>1; // y = y/2 
        x = (x*x) % p;   
    } 
    return res; 
} 
 void bfs (ll x,bool vis[],ll dis[])
 { vis[x]=true;
   
   q.push(x);
   while(!q.empty())
   {
   ll s=q.front();q.pop();
     for(auto i=adj[s].begin();i!=adj[s].end();i++)
     {     if(vis[*i]){continue;}
         vis[*i]=true;
         q.push(*i);
         dis[*i]=dis[s]+1;                   
     }
   
   }
                        
 }
 
void dfs(ll x,ve <ll> adj[],bool vis[])
 {
     vis[x]=true;
     
    cnt++;
    
    if(a[x]==0)return;
    else
    {dfs(a[x],adj,vis);}
  
  return;
 }
 
void SieveOfEratosthenes(ll n) 
{ 
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    //bool prime[n+1]; 
   // memset(prime, true, sizeof(prime)); 
  
    for (ll p=2; p*p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            // Update all multiples of p greater than or  
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked.  
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    
} 
ll gcd(ll a,ll b)
{
 if(a%b==0)return b;
 else {return gcd(b,a%b);}
}
 
 
ll power(ll x,ll y)
{ ll z=1;
   while(y--)  
   {
     z*=x;                     
   }
   return z;
}
bool comp(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
       if(a.first<b.first)return true; 
       else if(a.first==b.first)
       {
                              if(a.second>b.second)return true;
                              else{return false;}
       }
       else{return false;}
}
 
void solve()
{  
cin>>n>>k;
rep(i,1,n+1)
{
                     cin>>a[i];
                       adj[a[i]].pb(i);
                
}
ll z=1;
ll x=k;
while(true)
{ if(a[x]==0)
            {break;}
            else
            { x=a[x];z++;}
}

rep(i,1,n+1)
{
                       if(adj[i].empty()&&vis[i]==0)
                       { cnt=0;
                                              dfs(i,adj,vis);
                                              if(vis[k]==0)
                                              {
                                                   v.pb(cnt);                  
                                              }
                                              else
                                              {
                                                   vis[k]=0;                  
                                              }
                                              
                       }
}
bool dp[1007];
dp[0]=1;
ll s=0;
rep(i,0,v.size())
{//cout<<v[i]<<" ";
                      for(ll j=s;j>=0;j--)
                      {
                                             if(dp[j])
                                             {dp[j+v[i]]=1;}
                      }
                      s+=v[i];
}

rep(i,0,s+1)
{//cout<<dp[i];
                       if(dp[i]){cout<<i+z<<endl;}
                       
}
}
 
 
int main()
{ FAST;
 
    ll t=1;//cin>>t;
   
    while(t--){
                           solve();
    }
    return 0;
}
