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 = 0.0000001;
const double PI = acos(-1);
const long long INFLL = 0x7FFFFFFFFFFFFFFF;
const int INF = 0x7FFFFFFF;
template <typename T>
inline void next(T &num) {
  char c;
  num = 0;
  do {
    c = getchar_unlocked();
  } while (c != EOF && c == ' ' && c == '\n' && c == '\t');
  int sign = (c == '-' ? -1 : 1);
  if (c != '-') num += (c - '0');
  while ((c = getchar_unlocked()) != EOF && c != '\n' && c != '\t' &&
         c != ' ') {
    num *= 10;
    num += (c - '0');
  }
  num *= sign;
}
int nx[1005], n, x;
bitset<1005> sabi;
vector<int> pin;
pair<int, int> cari(int init, int num) {
  int pos = 0, pjg = 0;
  for (; init; init = nx[init], ++pjg)
    if (init == num) pos = pjg + 1;
  return make_pair(pos, pjg);
}
int main() {
  scanf("%d%d", &n, &x);
  for (int i = int(1), _b = int(n); i <= _b; ++i) {
    int t;
    scanf("%d", &t);
    if (t)
      nx[t] = i;
    else
      pin.push_back(i);
  }
  sabi[0] = 1;
  int pos;
  for (__typeof(pin.begin()) i = pin.begin(); i != pin.end(); i++) {
    int cur = *i;
    pair<int, int> tmp = cari(cur, x);
    if (tmp.first) {
      pos = tmp.first;
      continue;
    }
    for (int i = int(1000 - tmp.second), _b = int(0); i >= _b; --i)
      if (sabi[i]) sabi[i + tmp.second] = 1;
  }
  for (int i = int(0), _b = int(n - pos); i <= _b; ++i)
    if (sabi[i]) printf("%d\n", pos + i);
  return 0;
}
