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 double EPS = 1E-9;
const int INF = 1000000000;
const long long INF64 = (long long)1E18;
const double PI = 3.1415926535897932384626433832795;
typedef struct {
  int a;
  double p;
} tpo;
int main() {
  long long i, j, n, x;
  int a[2000];
  int o[2000];
  memset(&o, 0, sizeof(int) * 2000);
  cin >> n >> x;
  for (i = 0; i < n; ++i) cin >> a[i + 1];
  for (i = 0; i < n; ++i)
    if (a[i + 1] != 0) {
      int k = a[i + 1];
      while (a[k] != 0) k = a[k];
      o[k] += 1;
    }
  int free = 0;
  for (i = 1; i <= n; ++i)
    if (a[i] == 0 && o[i] == 0) free++;
  int my = 0, myI = -1;
  if (a[x] != 0) {
    int u = a[x];
    my = 1;
    while (a[u] != 0) {
      u = a[u];
      my++;
    }
    myI = u;
  } else
    free--;
  int list[4000], ilist = 1;
  list[0] = 0;
  int blist[4000], iblist;
  int cl[4000];
  memset(&cl, 0, sizeof(int) * 4000);
  cl[0] = 1;
  for (i = 1; i <= n; ++i)
    if (o[i] > 0 && a[i] == 0 && myI != i) {
      iblist = 0;
      for (j = 0; j < ilist; ++j) {
        if (cl[list[j] + o[i] + 1] == 0) blist[iblist++] = list[j] + o[i] + 1;
        cl[list[j] + o[i] + 1] = 1;
      }
      for (j = 0; j < iblist; ++j) list[ilist++] = blist[j];
    }
  int dl[2000];
  memset(&dl, 0, sizeof(int) * 2000);
  for (i = 0; i <= n; ++i)
    if (cl[i] == 1) {
      for (j = 1; j <= free; ++j) dl[i + j] = 1;
    }
  for (i = 0; i <= n; ++i)
    if (dl[i] == 1) cl[i] = 1;
  for (i = 0; i <= n; ++i)
    if (cl[i] == 1 && i + 1 + my <= n) cout << i + 1 + my << endl;
  return 0;
}
