#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int f[N], a[N], e[N], n, x, siz[N], tt = 0, k;
void chain(int p) {
  int num = 0, b = 0;
  for (int i = p; i; i = a[i]) {
    ++num;
    if (i == x) b = 1, k = num;
  }
  if (b)
    k = num - k + 1;
  else
    siz[++tt] = num;
}
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), e[a[i]] = 1;
  for (int i = 1; i <= n; ++i)
    if (!e[i]) chain(i);
  f[0] = 1;
  for (int i = 1; i <= tt; ++i)
    for (int j = n; j >= siz[i]; --j) f[j] |= f[j - siz[i]];
  for (int i = 0; i <= n; ++i)
    if (f[i]) printf("%d\n", i + k);
  return 0;
}
