#include <bits/stdc++.h>
using namespace std;
bool ff[10000];
int g[10000], f[10000], b[10000];
int find(int k) {
  if (f[k] != 0)
    return f[k] = find(f[k]);
  else
    return k;
}
int main() {
  int n, m, i, j, ll, tt;
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> f[i];
  j = m;
  ll = 1;
  while (f[j] != 0) j = f[j], ll++;
  for (i = 1; i <= n; i++) g[find(i)]++;
  tt = 0;
  for (i = 1; i <= n; i++)
    if (f[i] == 0 && i != j) b[++tt] = g[i];
  ff[ll] = 1;
  for (i = 1; i <= tt; i++)
    for (j = n; j > b[i]; j--) {
      if (ff[j - b[i]]) ff[j] = 1;
    }
  for (i = 1; i <= n; i++)
    if (ff[i]) cout << i << endl;
  return 0;
}
