#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
const double error = 1e-7;
const double PI = acos(-1);
mt19937 rng(
    (unsigned int)chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  if (x < m) return x;
  return x % m;
}
const int inf = 0x3f3f3f3f;
const long long int infl = 1e18 + 1;
const int nmax = 1e3 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n, x;
  cin >> n >> x;
  vector<int> frnt(n + 1), bck(n + 1);
  vector<int> len(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> frnt[i];
    bck[frnt[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    if (frnt[i] > 0) continue;
    int cur = i;
    while (cur > 0) {
      len[cur] = len[frnt[cur]] + 1;
      cur = bck[cur];
    }
  }
  int cur = x;
  while (bck[cur] != 0) {
    cur = bck[cur];
  }
  if (cur != x) len[cur] = 0;
  vector<int> coins;
  for (int i = 1; i <= n; i++) {
    if (i == x || bck[i] > 0) continue;
    coins.push_back(len[i]);
  }
  bitset<nmax> memo;
  memo[0] = 1;
  for (auto c : coins) memo |= memo << c;
  for (int i = 0; i < nmax; i++) {
    if (memo[i] == 1) cout << i + len[x] << "\n";
  }
}
