#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  x--;
  vector<int> q;
  q.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &q[i]);
    q[i]--;
  }
  vector<int> res_v;
  int nBPos = 0;
  for (int i = 0; i < n; i++) {
    if (q[i] == -1) {
      bool ifB = false;
      int pos = i;
      bool bLast = false;
      int d = 1;
      while (!bLast) {
        if (pos == x) {
          ifB = true;
          nBPos = d;
          break;
        }
        bLast = true;
        for (int i = 0; i < n; i++) {
          if (q[i] == pos) {
            bLast = false;
            pos = i;
            d++;
            break;
          }
        }
      }
      if (!ifB) res_v.push_back(d);
    }
  }
  sort(res_v.begin(), res_v.end());
  bool din[2000] = {0};
  bool din_prev[2000] = {0};
  din_prev[0] = true;
  din[0] = true;
  for (int i = 0; i < res_v.size(); i++) {
    for (int j = 0; j <= n; j++) {
      if (din_prev[j]) din[j + res_v[i]] = true;
    }
    memcpy(din_prev, din, sizeof(din));
  }
  for (int i = 0; i <= n; i++)
    if (din[i]) cout << i + nBPos << endl;
  return 0;
}
