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;
const int INF = 0x3fffffff;
const int PRIME = 999983;
const int MOD = 1000000007;
const int MULTI = 1000000007;
const double EPS = 1e-8;
inline bool isodd(int x) { return x & 1; }
inline bool isodd(long long x) { return x & 1; }
bool vis[100010];
class DisjointSet {
 public:
  int parent[100010], sum[100010];
  void init(int n) {
    for (int i = 0; i <= n; i++) parent[i] = i, sum[i] = 1;
  }
  int find(int x) { return x == parent[x] ? x : (parent[x] = find(parent[x])); }
  void merger(int x, int y) {
    int rtx = find(x);
    int rty = find(y);
    parent[rtx] = rty;
    sum[rty] += sum[rtx];
    vis[rty] = vis[rtx] || vis[rty];
  }
} ds;
set<int> ans;
vector<int> d;
int cnt[100010];
void dfs(int pos, int sum) {
  if (pos == d.size()) {
    ans.insert(sum);
    return;
  }
  for (int i = 0; i <= cnt[d[pos]]; i++) dfs(pos + 1, sum + i * d[pos]);
}
int main() {
  int n, x, a;
  scanf("%d%d", &n, &x);
  ds.init(n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    if (a && a != x) {
      ds.merger(a, i);
    }
    if (a == x) vis[i] = true;
  }
  int tmp;
  tmp = ds.sum[ds.find(x)];
  for (int i = 1; i <= n; i++) {
    if (ds.parent[i] != i || i == ds.find(x) || vis[i]) continue;
    cnt[ds.sum[i]]++;
    d.push_back(ds.sum[i]);
  }
  sort(d.begin(), d.end());
  d.erase(unique(d.begin(), d.end()), d.end());
  dfs(0, 0);
  for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) {
    printf("%d\n", (*it) + tmp);
  }
  return 0;
}
