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>
 
using namespace std;
 
typedef pair<int, int> pairIntInt;

vector<int> adj[1001];
vector<int> firsts;

int calculateDistance(int currPos, int prev)
{
    if(adj[currPos].size()==0) return 1;
    if(adj[currPos].size()==1 && prev!=0) return 1;
    for(auto next: adj[currPos])
    {
        if(next!=prev) return 1 + calculateDistance(next, currPos);
    }
}

int findFirstQ(int curr, int prev)
{
    if(adj[curr].size()==1) return curr;
    
    for(auto next: adj[curr])
    {
        if(next!=prev) return findFirstQ(next,curr);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    int n, x;
    scanf("%d %d", &n, &x);

    int a, prevSmartBeaver=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d", &a);
        if(a!=0)
        {
            adj[i].push_back(a);
            adj[a].push_back(i);
        }
        else
        {
            firsts.push_back(i);
        }
        if (a==x) prevSmartBeaver=i;
    }

    int distSmartBeaver = calculateDistance(x, prevSmartBeaver);
    int firstElQSmartB = findFirstQ(x, prevSmartBeaver);

    vector<int> lEachQ;
    lEachQ.push_back(0);
    for(auto v: firsts)
    {
        if(v!=firstElQSmartB) lEachQ.push_back(calculateDistance(v, 0));
    }


    sort(lEachQ.begin(), lEachQ.begin()+lEachQ.size());


    for(auto l: lEachQ)
    {
        //cout << l << "\n";
    }


    // Dynamic Programming
    bool possible[1001]= {false};
    possible[0]=true;
    for(int k=1; k<=lEachQ.size()-1;k++)
    {
        for(int w=n; w>=0; w--)
        {
            if(possible[w]) possible[w+lEachQ[k]]=true;
        }
    }

    for(int i=0; i<=n;i++)
    {
        if(possible[i]) cout << i+distSmartBeaver << "\n";
    }


    return 0;
}