#include <bits/stdc++.h>
using namespace std;
int a[10000];
int b[10000];
bool mark[1000];
int grp[10000];
int gc;
int chk[10000];
bool find(int num, int n) {
  if (num == 0) return 1;
  if (n < 0) return 0;
  int i;
  for (i = n; i >= 0 && grp[i] > num; i--)
    ;
  if (i < 0) return 0;
  for (; i >= 0; i--)
    if (find(num - grp[i], i - 1)) {
      return true;
    }
  return false;
}
int main() {
  int n, x, i, j, c;
  scanf("%d%d", &n, &x);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    mark[i] = 0;
    b[i] = 0;
    chk[i] = -1;
  }
  chk[0] = 1;
  for (i = 1; i <= n; i++)
    if (a[i] != 0) b[a[i]] = i;
  int posx = 1, z, oc;
  gc = 0;
  i = x;
  mark[i] = 1;
  c = 1;
  while (a[i] != 0) {
    mark[i] = 1;
    posx++;
    i = a[i];
    c++;
  }
  mark[i] = 1;
  i = x;
  while (b[i] != 0) {
    mark[i] = 1;
    i = b[i];
    c++;
  }
  mark[i] = 1;
  oc = c;
  for (i = 1; i <= n; i++)
    if (!mark[i]) {
      j = i;
      c = 1;
      mark[j] = 1;
      while (a[j] != 0) {
        mark[j] = 1;
        j = a[j];
        c++;
      }
      mark[j] = 1;
      j = i;
      while (b[j] != 0) {
        mark[j] = 1;
        j = b[j];
        c++;
      }
      mark[j] = 1;
      grp[gc++] = c;
    }
  sort(grp, grp + gc);
  cout << posx << endl;
  for (i = 1; i <= n - oc; i++)
    if (find(i, gc - 1)) cout << i + posx << endl;
}
