#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T Abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
template <typename T>
inline void relaxMin(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
inline void relaxMax(T &a, T b) {
  if (b > a) a = b;
}
const int INF = (int)1E9;
const long double EPS = 1E-6;
const long double PI = 3.1415926535897932384626433832795;
int p[1000];
int nxt[1000];
int used[1000];
int dp[100500];
int get_l(int v) {
  int cnt = 0;
  int s1 = v;
  while (s1) {
    used[s1 - 1] = 1;
    s1 = p[s1 - 1];
    if (s1) cnt++;
  }
  return cnt;
}
int get_r(int v) {
  int cnt = 0;
  int s2 = v;
  while (s2) {
    used[s2 - 1] = 1;
    s2 = nxt[s2 - 1];
    if (s2) cnt++;
  }
  return cnt;
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i(0), _bb(n); i < _bb; ++i) cin >> p[i];
  for (int i(0), _bb(n); i < _bb; ++i)
    if (p[i]) nxt[p[i] - 1] = i + 1;
  n = n;
  int d = get_l(k) + 1;
  get_r(k);
  vector<int> l;
  for (int i(0), _bb(n); i < _bb; ++i)
    if (!used[i]) {
      l.push_back(get_l(i + 1) + get_r(i + 1) + 1);
    }
  dp[0] = 1;
  for (int j(0), _bb(l.size()); j < _bb; ++j)
    for (int i = (n)-1; i >= 0; --i)
      if (dp[i]) dp[i + l[j]] = 1;
  for (int i(0), _bb(n + 1); i < _bb; ++i)
    if (dp[i]) cout << (d + i) << endl;
}
int main() {
  srand(0xA1B2C3D4);
  solve();
  return 0;
}
