#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const double eps = 1e-9;
const double INF = inf;
const double EPS = eps;
bool D[1100];
int n, A[1100], Len[1100], Next[1100];
vector<int> V;
int main() {
  int i, x, len, j, t;
  bool fl;
  scanf("%d%d", &n, &x), x--;
  memset(Next, -1, sizeof(Next));
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]), A[i]--;
    if (A[i] >= 0) Next[A[i]] = i;
  }
  for (i = 0; i < n; i++)
    if (A[i] == -1) {
      fl = 0, t = i, len = 0;
      while (t != -1) {
        if (t == x) fl = 1;
        len++, Len[t] = len, t = Next[t];
      }
      if (!fl) V.push_back(len);
    }
  D[0] = 1;
  for (i = 0; i < (int)V.size(); i++)
    for (j = 1100; j >= 0; j--)
      if (D[j]) D[j + V[i]] = 1;
  for (i = 0; i <= n; i++)
    if (D[i]) printf("%d\n", i + Len[x]);
  ;
  return 0;
}
