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>
int n, m, up, spe, sp;
int a[1010];
int s[1010];
int f[1010];
int v[1010];
int dp[3010];
int findset(int x) {
  if (x == f[x]) return x;
  f[x] = findset(f[x]);
  return f[x];
}
inline void init() {
  int i, fx, fy;
  scanf("%d%d", &n, &m);
  spe = 0;
  sp = m;
  for (i = 1; i <= n; i++) {
    f[i] = i;
    s[i] = 1;
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= n; i++) {
    if (a[i] == 0) continue;
    fx = findset(a[i]);
    fy = findset(i);
    if (fy == sp) spe += s[fx];
    if (a[i] == sp) sp = fy;
    s[fy] += s[fx];
    f[fx] = fy;
  }
  up = 0;
  for (i = 1; i <= n; i++) {
    fx = findset(i);
    if (s[fx] && sp != fx) {
      up++;
      v[up] = s[fx];
      s[fx] = 0;
    }
  }
}
inline void work() {
  int i, j;
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  for (i = 1; i <= up; i++)
    for (j = n; j >= v[i]; j--)
      if (dp[j - v[i]]) dp[j] = 1;
  spe++;
  for (i = 0; i <= n - spe; i++)
    if (dp[i]) printf("%d\n", i + spe);
}
int main() {
  init();
  work();
  return 0;
}
