#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& o, pair<A, B>& p) {
  return o << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "{";
  for (typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
    o << *it << ", ";
  return o << "}";
}
struct debugger {
  ostream& out;
  bool first;
  void start() { first = true; }
  debugger(ostream& out) : out(out) { start(); }
  void nl() {
    out << endl;
    start();
  }
  template <class T>
  debugger& operator,(T& v) {
    if (first)
      first = false;
    else
      out << ", ";
    out << v;
    return *this;
  }
  template <class T>
  debugger& operator,(pair<T*, int> arr) {
    out << "{";
    for (typeof((arr.second) - 1) i = (0); i <= ((arr.second) - 1); i++) {
      out << arr.first[i] << ", ";
    }
    out << "}";
    return *this;
  }
} dbg(cerr);
int n, k, par[1100], child[1100], vis[1100];
bool pos[1100];
int main() {
  cin >> n >> k;
  for (typeof(n) i = (1); i <= (n); i++) {
    cin >> par[i];
    if (par[i]) child[par[i]] = i;
  }
  vector<int> v;
  int beeverpos = -1;
  for (typeof(n) i = (1); i <= (n); i++) {
    if (!par[i]) {
      int x = i, c = 0;
      int beever = false;
      if (k == x) {
        beever = true;
        beeverpos = 1;
      }
      while (child[x]) {
        x = child[x];
        c++;
        if (k == x) {
          beever = true;
          beeverpos = c + 1;
        }
      }
      if (!beever) {
        v.push_back(c + 1);
      } else {
      }
    }
  }
  pos[0] = true;
  for (typeof(((int)v.size()) - 1) j = (0); j <= (((int)v.size()) - 1); j++) {
    for (int i = n; i >= v[j]; i--) {
      if (pos[i - v[j]] == true) pos[i] = true;
    }
  }
  for (typeof((n)-1) j = (0); j <= ((n)-1); j++) {
    if (pos[j] == true) {
      cout << beeverpos + j << endl;
    }
  }
  return 0;
}
