#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> a(n), b(n, -1), d(n, 10001), m(n);
  m[x] = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    if (a[i] != -1) b[a[i]] = i;
  }
  queue<int> q;
  for (int i = 0; i < n; i++)
    if (a[i] == -1) {
      q.push(i);
      d[i] = 1;
    }
  vector<int> gr;
  int our = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (b[v] != -1) {
      q.push(b[v]);
      d[b[v]] = d[v] + 1;
      if (m[v] == 1) m[b[v]] = 1;
    } else {
      if (m[v] != 1) gr.push_back(d[v]);
    }
  }
  vector<int> p(n + 1);
  p[0] = 1;
  our = d[x];
  for (int i = 0; i < gr.size(); i++)
    for (int j = n; j > 0; j--)
      if (p[j] == 0 && j - gr[i] >= 0 && p[j - gr[i]] != 0) p[j] = 1;
  for (int i = 0; i <= n; i++)
    if (p[i]) cout << our + i << endl;
}
