#include <bits/stdc++.h>
using namespace std;
int n, x, pos, totallen, flag, a[1010], g[1010][1010], vis[1010],
    dp[2010][2010], parent[1010], c[1010], in[1010];
vector<int> v, res;
int getparent(int i) {
  if (parent[i] == i)
    return i;
  else
    return getparent(parent[i]);
}
int solve(int idx, int sm, int sz) {
  int i, j, ans = 0;
  if (sm < 0) return 0;
  if (idx >= sz && sm != 0) return 0;
  if (sm == 0) return dp[idx][sm] = 1;
  if (dp[idx][sm] == -1) {
    if (sm - v[idx] >= 0) {
      ans = solve(idx + 1, sm, sz);
      ans = ans || solve(idx + 1, sm - v[idx], sz);
    }
    dp[idx][sm] = ans;
  }
  return dp[idx][sm];
}
int main() {
  int i, j, k, sz, ans = 0;
  scanf("%d%d", &n, &x);
  for (i = 0; i <= n + 1; i++) {
    vis[i] = 0;
    parent[i] = i;
    c[i] = 1;
    in[i] = 0;
    for (j = 0; j <= n + 1; j++) {
      g[i][j] = 0;
      dp[i][j] = -1;
    }
  }
  v.clear();
  res.clear();
  in[x] = 1;
  pos = 1;
  totallen = 1;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > 0) {
      j = getparent(a[i]);
      k = getparent(i);
      if (j != k) {
        if (in[k]) {
          pos += c[j];
        }
        parent[k] = j;
        c[j] += c[k];
        in[j] = in[j] || in[k];
        if (in[j] || in[k]) totallen = c[j];
      }
    }
  }
  for (i = 1; i <= n; i++) {
    int p = getparent(i);
    if (p == i && in[i] != 1) v.push_back(c[p]);
  }
  sort(v.begin(), v.end());
  sz = v.size();
  res.push_back(pos);
  for (int sm = 1; sm + totallen <= n; sm++) {
    if (solve(0, sm, sz)) res.push_back(sm + pos);
  }
  sort(res.begin(), res.end());
  sz = res.size();
  for (i = 0; i < sz; i++) printf("%d\n", res[i]);
  return 0;
}
