#include <bits/stdc++.h>
using namespace std;
int N;
int B[1004];
int smart_id;
bool is_smart_in_line;
int has_ref[1004];
int count_line(int x) {
  if (x == smart_id) is_smart_in_line = true;
  if (x == 0)
    return 0;
  else
    return 1 + count_line(B[x]);
}
bool DP[1004];
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> smart_id;
  for (int i = 1; i <= N; ++i) cin >> B[i];
  for (int i = 1; i <= N; ++i) has_ref[B[i]] = 1;
  vector<int> A;
  int smart_cnt = 0;
  for (int i = 1; i <= N; ++i)
    if (!has_ref[i]) {
      is_smart_in_line = false;
      int cnt = count_line(i);
      if (is_smart_in_line) {
        smart_cnt = count_line(smart_id);
      } else {
        A.push_back(cnt);
      }
    }
  DP[0] = 1;
  for (int i = 0; i < A.size(); ++i) {
    for (int n = 1000; n >= A[i]; --n)
      if (DP[n - A[i]]) DP[n] = 1;
  }
  for (int n = 0; n <= 1000; ++n)
    if (DP[n]) cout << n + smart_cnt << endl;
  return 0;
}
