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;
long long visit[1007], dp[1007], dp1[1007], n, d, chk;
vector<long long> v[1007], v1;
long long dfs(long long node) {
  visit[node] = 1;
  if (node == d) {
    chk = 1;
    return 1;
  }
  long long res = 1, i, x;
  for (i = 0; i < v[node].size(); i++) {
    x = v[node][i];
    if (!visit[x]) {
      res += dfs(x);
    }
  }
  return res;
}
int main() {
  long long i, x, val, k, tot = 0, sum;
  cin >> n >> d;
  for (i = 1; i <= n; i++) {
    cin >> x;
    v[x].push_back(i);
  }
  for (i = 1; i <= n; i++) visit[i] = 0;
  v1.clear();
  for (i = 0; i < v[0].size(); i++) {
    chk = 0;
    val = dfs(v[0][i]);
    if (chk == 1)
      k = val;
    else {
      v1.push_back(val);
      tot += val;
    }
  }
  for (i = 0; i <= tot; i++) dp[i] = dp1[i] = 0;
  dp[0] = 1;
  sort(v1.begin(), v1.end());
  for (i = 0; i < v1.size(); i++) {
    for (sum = 1; sum <= tot; sum++) {
      if (v1[i] < sum) {
        dp[sum] = dp[sum - v1[i]];
      }
    }
    dp[v1[i]] = 1;
  }
  for (i = 0; i <= tot; i++) {
    if (dp[i] == 1) {
      dp1[i + k] = 1;
    }
  }
  for (i = 0; i <= tot + k; i++) {
    if (dp1[i] == 1) cout << i << endl;
  }
  return 0;
}
