#include <bits/stdc++.h>
using namespace std;
int n;
unordered_set<int> DifferentSum(vector<int>& arr, int be) {
  if (be >= n) {
    return {};
  }
  unordered_set<int> s = DifferentSum(arr, be + 1);
  int k = s.size();
  unordered_set<int> ans;
  for (auto itr = s.begin(); itr != s.end(); itr++) {
    ans.insert(*itr + arr[be]);
    ans.insert(*itr);
  }
  ans.insert(arr[be]);
  return ans;
}
unordered_map<int, vector<int> > m;
int tp = 0;
int ts = 0;
int pos;
void DFS(int v) {
  tp++;
  if (v == pos) {
    ts = tp;
  }
  for (int i = 0; i < m[v].size(); i++) {
    DFS(m[v][i]);
  }
}
int main() {
  cin >> n;
  cin >> pos;
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(i);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] != 0) {
      s.erase(v[i]);
      m[i].push_back(v[i]);
    }
  }
  bool f = 1;
  vector<int> arr;
  for (auto itr = s.begin(); itr != s.end(); itr++) {
    DFS(*itr);
    if (ts && f) {
      ts = tp - ts + 1;
      tp = 0;
      f = 0;
    }
    if (tp != 0) arr.push_back(tp);
    tp = 0;
  }
  n = arr.size();
  unordered_set<int> ans = DifferentSum(arr, 0);
  set<int> sort;
  sort.insert(ts);
  for (auto itr = ans.begin(); itr != ans.end(); itr++) {
    sort.insert(*itr + ts);
  }
  for (auto itr = sort.begin(); itr != sort.end(); itr++) {
    cout << *itr << endl;
  }
}
