#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x7FFFFFFF;
template <class T>
inline bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
void show(T a, int n) {
  for (int i = 0; i < n; ++i) cout << a[i] << ' ';
  cout << endl;
}
template <class T>
void show(T a, int r, int l) {
  for (int i = 0; i < r; ++i) show(a[i], l);
  cout << endl;
}
int nxt[10243];
int last[10232];
int pl;
vector<int> vl;
int n, m;
int que(int u, bool &find) {
  if (u == m) {
    find = true;
    return 1;
  }
  if (!last[u]) return 1;
  return que(last[u], find) + 1;
}
bool dp[1002];
int main() {
  while (cin >> n >> m) {
    memset(last, 0, sizeof(last));
    for (int i = 0; i < n; i++) {
      cin >> nxt[i + 1];
      last[nxt[i + 1]] = i + 1;
    }
    pl = 0;
    vl.clear();
    for (int i = 1; i <= n; i++)
      if (nxt[i] == 0) {
        bool f = 0;
        int l = que(i, f);
        if (f)
          pl = l;
        else
          vl.push_back(l);
      }
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for (int i = 0; i < vl.size(); i++) {
      for (int j = n; j >= vl[i]; j--)
        if (dp[j - vl[i]]) dp[j] = true;
    }
    for (int i = 0; i <= n; i++)
      if (dp[i]) cout << i + pl << endl;
  }
  return 0;
}
