#include <bits/stdc++.h>
using namespace std;
vector<int> V;
int B[1003], F[1003], n, me, i, mn;
bool M[1003][1003], H[1003], h;
void g(int i, int s) {
  if (i == V.size()) {
    H[s] = 1;
    return;
  }
  if (M[i][s]) return;
  M[i][s] = 1;
  g(i + 1, s + V[i]);
  g(i + 1, s);
}
void f(int x, int s) {
  if (x == me) mn = s;
  if (!B[x]) {
    V.push_back(s);
    return;
  }
  f(B[x], s + 1);
}
int main() {
  cin >> n >> me;
  for (i = 1; i <= n; i++) {
    cin >> F[i];
    B[F[i]] = i;
  }
  for (i = 1; i <= n; i++)
    if (!F[i]) {
      f(i, 1);
      if (mn && !h) {
        V.pop_back();
        h = 1;
      }
    }
  g(0, 0);
  for (i = 0; i <= n; i++)
    if (H[i]) cout << i + mn << endl;
  return 0;
}
