#include <bits/stdc++.h>
using namespace std;
int const MAXN = 1010;
int const MAX_W = 10000;
int next_vertex[MAXN], nnew[MAXN], color_count[MAXN], parent_vertex[MAXN];
int dp[MAX_W];
void dfs(int v, int color) {
  if (v < 0) return;
  if (nnew[v]) return;
  nnew[v] = color;
  dfs(next_vertex[v], color);
  dfs(parent_vertex[v], color);
}
int main() {
  int n;
  int init_num;
  scanf("%d%d", &n, &init_num);
  init_num--;
  for (int i = 0; i < n; i++) {
    next_vertex[i] = -1;
    parent_vertex[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    int v;
    scanf("%d", &v);
    v--;
    if (v < 0) continue;
    next_vertex[i] = v;
    parent_vertex[v] = i;
  }
  dfs(init_num, 1);
  int color = 2;
  for (int i = 0; i < n; i++)
    if (!nnew[i]) {
      dfs(i, color);
      color++;
    }
  for (int i = 0; i < n; i++) color_count[nnew[i]]++;
  int our_comp_size = color_count[1];
  int our_pos_in_comp = 0;
  {
    int v = init_num;
    while (next_vertex[v] >= 0) {
      our_pos_in_comp++;
      v = next_vertex[v];
    }
  }
  vector<int> all_w;
  for (int i = 2; i <= color; i++)
    if (color_count[i] > 0) all_w.push_back(color_count[i]);
  sort(all_w.begin(), all_w.end());
  dp[0] = 1;
  for (int i = 0; i < (int)all_w.size(); i++)
    for (int j = MAX_W - 1; j >= 0; j--)
      if (dp[j])
        if (j + all_w[i] < MAX_W) dp[j + all_w[i]] = 1;
  for (int i = 0; i < MAX_W; i++)
    if (dp[i]) printf("%d\n", i + our_pos_in_comp + 1);
  return 0;
}
