#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int N = 1005;
int cnt[N];
bitset<N> dp;
int n, m;
int idx[N];
int par[N];
int xx[N];
bool vis[N];
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
int main() {
  for (int i = 0; i <= 1000; i += 1) cnt[i] = 0;
  cin >> n >> m;
  int ix = 1;
  for (int i = 0; i <= n; i += 1) par[i] = i;
  int id = -1, tmp, x;
  bool flag = false;
  for (int i = 1; i <= n; i += 1) {
    cin >> xx[i];
    if (xx[i] != 0) par[i] = find(par[xx[i]]);
    if (xx[i] == 0) {
      idx[i] = ix++;
    }
  }
  for (int i = 1; i <= n; i += 1) {
    if (find(par[i]) == find(par[m])) {
      continue;
    }
    cnt[idx[find(par[i])]]++;
  }
  int now = m;
  while (now != 0) {
    cnt[idx[par[m]]]++;
    now = xx[now];
  }
  dp[cnt[idx[par[m]]]] = 1;
  for (int i = 1; i <= ix - 1; i += 1) {
    if (i != idx[par[m]]) {
      dp = (dp << cnt[i]) | dp;
    }
  }
  for (int i = 0; i <= n; i += 1) {
    if (dp[i] == 1) {
      cout << i << endl;
    }
  }
  return 0;
}
