#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535898;
int main() {
  int n, x, a[1000], next[1000], nl = 0, r[1000], sz[1000], nsz = 0, mn = 1;
  cin >> n >> x;
  x--;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  memset(next, -1, sizeof(next));
  for (int i = 0; i < n; i++) {
    if (a[i] == -1) continue;
    next[a[i]] = i;
  }
  list<int> l[1000];
  for (int i = 0; i < n; i++) {
    int j = i;
    if (a[j] == -1) {
      l[nl].push_back(j);
      r[j] = nl;
      while (next[j] != -1) {
        l[nl].push_back(next[j]);
        j = next[j];
        r[j] = nl;
      }
      nl++;
    }
  }
  for (int i = 0; i < nl; i++)
    if (r[x] != i) sz[nsz++] = l[i].size();
  for (list<int>::iterator it = l[r[x]].begin(); it != l[r[x]].end();
       it++, mn++)
    if (x == *it) break;
  set<int> myset;
  myset.insert(mn);
  for (int i = 0; i < nsz; i++) {
    set<int> tmpmyset = myset;
    for (set<int>::iterator it = tmpmyset.begin(); it != tmpmyset.end(); it++)
      myset.insert(sz[i] + *it);
  }
  for (set<int>::iterator it = myset.begin(); it != myset.end(); it++)
    cout << *it << endl;
  return 0;
}
