#include <bits/stdc++.h>
int n, m;
int dp[1010], map[1010], father[1010], size[1010], used[1010];
int find(int now) {
  if (father[now] == now) return now;
  return father[now] = find(father[now]);
}
int main() {
  int i, j, p = 1;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; ++i) scanf("%d", map + i);
  for (i = 1; i <= n; ++i) father[i] = i;
  for (i = 1; i <= n; ++i)
    if (map[i]) father[i] = map[i];
  while (map[m]) {
    ++p;
    m = map[m];
  }
  for (i = 1; i <= n; ++i) find(i);
  for (i = 1; i <= n; ++i) ++size[father[i]];
  dp[0] = 1;
  for (i = 1; i <= n; ++i)
    if (!used[father[i]] && father[i] != m) {
      for (j = 1000 - size[father[i]]; j + 1; --j)
        if (dp[j]) dp[j + size[father[i]]] = 1;
      used[father[i]] = 1;
    }
  for (i = 0; i <= 1000; ++i)
    if (dp[i]) printf("%d\n", i + p);
  return 0;
}
