#include <bits/stdc++.h>
using namespace std;
int a[100002], v[100002];
int m;
bool magic = false;
int visit(int x) {
  if (x == m) magic = true;
  if (x == 0) return 0;
  v[x] = 1;
  return 1 + visit(a[x]);
}
void pvo(vector<int> v) {
  for (int i = 0; (i) < (int)(v.size()); (i)++) cout << v[i] << " ";
  cout << endl;
  ;
}
int main() {
  int n;
  cin >> n >> m;
  for (int i = 0; (i) < (int)(n); (i)++) {
    cin >> a[i + 1];
    v[a[i + 1]] = 1;
  }
  vector<int> ps;
  for (int i = 1; i <= n; i++) {
    if (v[i] == 0) {
      magic = false;
      int size = visit(i);
      if (magic == false) ps.push_back(size);
    }
  }
  int size = ps.size();
  vector<int> all;
  for (int i = 0; i < size; i++) {
    int asize = all.size();
    for (int j = 0; j < asize; j++) {
      all.push_back(ps[i] + all[j]);
    }
    all.push_back(ps[i]);
  }
  int pos = 1;
  int start = m;
  while (a[start] != 0) {
    pos++;
    start = a[start];
  }
  all.push_back(0);
  set<int> s(all.begin(), all.end());
  vector<int> final(s.begin(), s.end());
  sort(final.begin(), final.end());
  for (int i = 0; i < final.size(); i++) cout << final[i] + pos << endl;
  return 0;
}
