#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:500000000")
using namespace std;
bool used[1009];
vector<int> g[1009];
vector<vector<int> > C;
void dfs(int v) {
  for (int i = 0; i < g[v].size(); i++) dfs(g[v][i]);
  C.back().push_back(v);
}
int d[1009];
int main() {
  int n, x, a, i, j;
  scanf("%d %d", &n, &x);
  x--;
  for (i = 0; i < n; i++) {
    scanf("%d", &a);
    a--;
    if (a != -1) {
      used[i] = true;
      g[a].push_back(i);
    }
  }
  int Pos = 0;
  for (i = 0; i < n; i++) {
    if (!used[i]) {
      C.push_back(vector<int>());
      dfs(i);
      int pos = find(C.back().begin(), C.back().end(), x) - C.back().begin();
      if (pos != C.back().size()) {
        Pos = C.back().size() - pos;
        C.pop_back();
      }
    }
  }
  d[0] = 1;
  for (i = 0; i < C.size(); i++) {
    for (j = n; j >= 0; j--) {
      if (d[j]) {
        d[j + C[i].size()] = 1;
      }
    }
  }
  for (i = 0; i <= n; i++)
    if (d[i]) printf("%d\n", i + Pos);
}
