#include <bits/stdc++.h>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}
using namespace std;
class UnionFind {
 private:
  vector<int> id;
  int getId(int i) {
    if (i == id[i]) return i;
    return id[i] = getId(id[i]);
  }

 public:
  UnionFind(int size = 0) { init(size); }
  ~UnionFind() {}
  void init(int size) {
    id = vector<int>(size);
    for (int i = 0; i < size; i++) id[i] = i;
  }
  void unite(int i, int j) {
    int next = min(getId(i), getId(j));
    id[getId(i)] = id[getId(j)] = next;
  }
  int operator[](int i) { return getId(i); }
  int count() {
    set<int> s;
    for (int i = 0; i < (int)id.size(); i++) s.insert(getId(i));
    return s.size();
  }
};
int main() {
  const int n = getInt();
  const int x = getInt() - 1;
  UnionFind uf(n);
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) {
    a[i] = getInt() - 1;
    if (a[i] != -1) uf.unite(i, a[i]);
  }
  int xc = 1;
  int now = x;
  while (a[now] != -1) {
    now = a[now];
    xc++;
  }
  vector<int> dp(n + 1);
  vector<int> cnts(n);
  for (int i = 0; i < (int)(n); i++) {
    cnts[uf[i]]++;
  }
  dp[xc] = 1;
  for (int i = 0; i < (int)(n); i++) {
    if (cnts[i] != 0 && uf[i] != uf[x]) {
      for (int j = n; j >= 0; j--) {
        if (dp[j] != 0 && j + cnts[i] <= n) {
          dp[j + cnts[i]] = 1;
        }
      }
    }
  }
  for (int i = 0; i < (int)(n + 1); i++)
    if (dp[i]) printf("%d\n", i);
  return 0;
}
