#include <bits/stdc++.h>
int map[1010], line[1010], a[1010], is[1010];
int main(int argc, char *argv[]) {
  int n, x;
  int i, j;
  int key, cnt, cas, flag;
  scanf("%d%d", &n, &x);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    map[a[i]] = 1;
  }
  cas = 0;
  for (i = 1; i <= n; ++i) {
    if (map[i] == 0) {
      key = i;
      cnt = 0;
      flag = 0;
      while (key) {
        if (key == x) flag = 1;
        key = a[key];
        cnt++;
      }
      if (flag == 0) line[cas++] = cnt;
    }
  }
  key = x;
  cnt = 0;
  while (key) {
    key = a[key];
    cnt++;
  }
  is[cnt] = 1;
  for (i = 0; i < cas; ++i) {
    for (j = n; j >= line[i]; --j) {
      if (is[j - line[i]] == 1) {
        is[j] = 1;
      }
    }
  }
  for (i = 1; i <= n; ++i) {
    if (is[i] == 1) {
      printf("%d\n", i);
    }
  }
  return 0;
}
