#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int sz = 0;
int pos[1111];
int p[1111];
int head[1111];
int len[1111];
int n, k;
bool tr;
int knum;
int can[2222];
int doit(int x) {
  if (x == k) tr = 1;
  if (p[x] == 0) return pos[x];
  pos[p[x]] = pos[x] + 1;
  return doit(p[x]);
}
int main() {
  int i, j;
  cin >> n >> k;
  for (i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (x == 0) {
      head[++sz] = i;
    } else
      p[x] = i;
  }
  for (i = 1; i <= sz; ++i) {
    tr = 0;
    pos[head[i]] = 1;
    len[i] = doit(head[i]);
    if (tr) knum = i;
  }
  can[0] = 1;
  for (i = 1; i <= sz; ++i) {
    if (i == knum) continue;
    for (j = 1000; j >= 0; --j) {
      if (can[j]) {
        can[j + len[i]] = 1;
      }
    }
  }
  for (i = 0; i <= 1000; ++i) {
    if (can[i]) {
      cout << i + pos[k] << endl;
    }
  }
  return 0;
}
