#include <bits/stdc++.h>
using namespace std;
const int MAX = 2005;
const int oo = 1 << 25;
int N, x;
int a[MAX], h[MAX], dad[MAX];
int sum[MAX], cnt;
int dp[MAX];
int go(int v, int p) {
  if (!v) return 0;
  dad[v] = p;
  if (h[v]) return h[v];
  return h[v] = 1 + go(a[v], v);
}
int main() {
  scanf("%d%d", &N, &x);
  for (int i = 1; i <= N; i++) scanf("%d", a + i);
  for (int i = 1; i <= N; i++)
    if (!dad[i]) go(i, -1);
  int hx = h[x];
  printf("%d\n", hx);
  while (dad[x] != -1) x = dad[x];
  dad[x] = 0;
  for (int i = 1; i <= N; i++)
    if (dad[i] == -1) sum[++cnt] = h[i];
  for (int i = 1; i <= N; i++) dp[i] = oo;
  for (int j = 1; j <= cnt; j++) dp[sum[j]] = min(dp[sum[j]], j);
  for (int i = 1; i <= N; i++)
    if (dp[i] < oo) {
      printf("%d\n", hx + i);
      for (int j = dp[i] + 1; j <= cnt; j++)
        dp[i + sum[j]] = min(dp[i + sum[j]], j);
    }
  return 0;
}
