#include <bits/stdc++.h>
using namespace std;
int Next[1024];
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x == 0)
      v.push_back(i);
    else
      Next[x] = i;
  }
  vector<int> g;
  int bpos = -1;
  for (int i = 0; i < (int)(v.size()); ++i) {
    int pos = -1;
    int t = v[i];
    int cpos = 0;
    while (t) {
      if (t == k) pos = cpos;
      cpos++;
      t = Next[t];
    }
    if (pos != -1)
      bpos = pos;
    else
      g.push_back(cpos);
  }
  vector<int> a(2000, 0);
  a[0] = 1;
  for (int i = 0; i < (int)(g.size()); ++i) {
    for (int j = 1100; j >= 0; j--)
      if (a[j]) a[j + g[i]] = 1;
  }
  for (int i = 0; i < (int)(1000); ++i)
    if (a[i]) cout << (i + bpos + 1) << endl;
  return 0;
}
