#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);
template <class T>
int chmin(T &t, T f) {
  return (t > f) ? (t = f, 1) : 0;
}
template <class T>
int chmax(T &t, T f) {
  return (t < f) ? (t = f, 1) : 0;
}
inline int getint() {
  int a;
  return scanf("%d", &a) ? a : (fprintf(stderr, "trying to read\n"), -1);
}
inline double getdouble() {
  double a;
  return scanf("%lf", &a) ? a : (fprintf(stderr, "trying to read\n"), -1);
}
int contains(int p, vector<int> &a, int i) {
  if (i == p) return 1;
  if (a[i] == -1) return 0;
  return contains(p, a, a[i]);
}
int length(vector<int> &a, int i) {
  return a[i] == -1 ? 1 : 1 + length(a, a[i]);
}
const int N = 1 << 10;
int dp[N];
void myCode() {
  int n = getint(), p = getint() - 1;
  vector<int> a, last(n, 1);
  for (int i = (int)0; i < (int)n; i++) {
    a.push_back(getint() - 1);
    last[a.back()] = 0;
  }
  vector<int> subset;
  for (int i = (int)0; i < (int)n; i++)
    if (last[i] && !contains(p, a, i)) subset.push_back(length(a, i));
  int myPos = length(a, p);
  dp[0] = 1;
  for (int i = (int)0; i < (int)((int)(subset).size()); i++)
    for (int j = (int)(N - subset[i]) - 1; j >= 0; --j)
      if (dp[j]) dp[j + subset[i]] = 1;
  for (int i = (int)0; i < (int)N; i++)
    if (dp[i]) printf("%d\n", myPos + i);
}
int main() {
  myCode();
  return 0;
}
