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;
bool vis[1011], root[1011];
vector<int> g[1011];
int main() {
  int n, x;
  cin >> n >> x;
  int p[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> p[i + 1];
    root[i + 1] = 1;
    if (p[i + 1]) {
      root[i + 1] = 0;
      g[p[i + 1]].emplace_back(i + 1);
    }
  }
  vector<int> v;
  int cur, sz, sp;
  bool found = 0;
  for (int i = 0; i < n; i++) {
    cur = i + 1;
    sz = 0;
    found = 0;
    if (vis[i + 1] or !root[i + 1]) continue;
    while (1) {
      if (cur == 0) break;
      vis[cur] = 1;
      if (cur == x) {
        sp = sz;
        found = 1;
      }
      if (g[cur].size())
        cur = g[cur][0];
      else
        cur = 0;
      sz++;
    }
    if (!found) v.emplace_back(sz);
  }
  set<int> s1, s2;
  for (auto x : v) {
    for (auto y : s1) {
      if (y + x <= n) s2.insert(y + x);
    }
    s2.insert(x);
    for (auto y : s2) {
      s1.insert(y);
    }
    s2.clear();
  }
  sp++;
  cout << sp << " ";
  for (auto x : s1) {
    cout << x + sp << " ";
  }
}
