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 = ((1 << 30) - 1);
const long long linf = ((1ll << 62) - 1);
const int cmax = 1001;
int n, x;
int a[cmax];
bool u[cmax];
vector<vector<int> > groups;
vector<int> ts;
void topSort(int v) {
  if (u[v]) return;
  u[v] = true;
  if (a[v] != -1) topSort(a[v]);
  ts.push_back(v);
}
void go(int v, vector<int> &c) {
  u[v] = true;
  c.push_back(v);
  if (a[v] != -1) go(a[v], c);
}
int dp[cmax][cmax];
int main() {
  int index;
  assert(scanf("%d%d", &n, &x)), --x;
  for (int i = 0; i < n; i++) assert(scanf("%d", &a[i])), --a[i];
  for (int i = 0; i < n; i++) topSort(i);
  memset(u, 0, sizeof(bool) * n);
  reverse((ts).begin(), (ts).end());
  for (int i = 0; i < n; i++)
    if (!u[ts[i]])
      groups.push_back(vector<int>()),
          go(ts[i], groups[((int)(groups).size()) - 1]);
  for (int i = 0; i < ((int)(groups).size()); i++) {
    int pos =
        find((groups[i]).begin(), (groups[i]).end(), x) - groups[i].begin();
    if (pos != ((int)(groups[i]).size())) {
      groups.erase(groups.begin() + i);
      index = pos;
      break;
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < ((int)(groups).size()); i++)
    for (int j = 0; j < n; j++)
      if (dp[i][j])
        dp[i + 1][j] = true, dp[i + 1][j + ((int)(groups[i]).size())] = true;
  for (int i = n - 1; i >= 0; i--)
    if (dp[((int)(groups).size())][i]) cout << n - i - index << '\n';
  return 0;
}
