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;
namespace {
template <class T>
void read(T& container) {
  for (typename T::value_type& x : container) cin >> x;
}
template <class T>
void print(T& container, const char separator = ' ') {
  for (typename T::value_type& x : container) cout << x << separator;
}
void openFiles(const char* in, const char* out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
int n, x;
vector<int> v;
const int maxn = 1100;
bool used[maxn];
bool in[maxn];
bool can[2 * maxn];
vector<int> sz;
void run() {
  cin >> n >> x;
  --x;
  v.resize(n);
  read(v);
  for (int x : v) {
    in[x - 1] = 1;
  }
  int add;
  for (int i = 0; i < n; ++i)
    if (!used[i] && !in[i]) {
      bool p = 0;
      int cc = 0;
      int tmp = 0, j;
      for (j = i; v[j] != 0; j = v[j] - 1) {
        if (j == x) {
          p = 1;
          cc = tmp;
        }
        ++tmp;
      }
      if (j == x) {
        p = 1;
        cc = tmp;
      }
      if (!p)
        sz.push_back(tmp + 1);
      else
        add = tmp - cc + 1;
    }
  can[0] = 1;
  for (int x : sz)
    for (int i = n - 1; i >= 0; --i)
      if (can[i]) can[i + x] = 1;
  for (int i = 0; i < n; ++i)
    if (can[i]) cout << i + add << endl;
}
}  // namespace
int main() {
  run();
  return 0;
}
