#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
template <typename T>
inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  return (a * b) / gcd(a, b);
}
template <typename T>
string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
template <class T>
void splitStr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
inline int two(int n) { return 1 << n; }
inline int isOnBit(int n, int b) { return (n >> b) & 1; }
inline void onBit(int &n, int b) { n |= two(b); }
inline void offBit(int &n, int b) { n &= ~two(b); }
inline int lastBit(int n) { return n & (-n); }
inline int cntBit(int n) {
  int res = 0;
  while (n && ++res) n -= n & (-n);
  return res;
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
int son[1005], n, x, rx, a[1005], f[1005];
pair<int, int> d[1005];
vector<int> b;
void solve() {
  for (int i = (1), _b = (n); i <= _b; i++) {
    if (a[i]) continue;
    int ok = 1, num = 0;
    int tmp = i;
    do {
      if (tmp == x) ok = 0, d[x].first = num + 1;
      num++;
      tmp = son[tmp];
    } while (tmp);
    if (ok) b.push_back(num);
  }
  sort(b.begin(), b.end());
  memset((f), 0, sizeof((f)));
  f[0] = 1;
  for (int i = 0, _n = (b.size()); i < _n; i++) {
    for (int j = (n), _b = (b[i]); j >= _b; j--) f[j] |= f[j - b[i]];
  }
  for (int i = 0, _n = (n); i < _n; i++)
    if (f[i]) printf("%d\n", i + d[x].first);
}
int main() {
  scanf("%d %d", &n, &x);
  for (int i = (1), _b = (n); i <= _b; i++) son[i] = 0;
  for (int i = (1), _b = (n); i <= _b; i++)
    scanf("%d", &a[i]), son[a[i]] = (a[i]) ? i : 0;
  solve();
  return 0;
}
