#include <bits/stdc++.h>
template <class T>
T _diff(T a, T b) {
  return (a < b ? b - a : a - b);
}
template <class T>
T _abs(T a) {
  return (a < 0 ? -a : a);
}
template <class T>
T _max(T a, T b) {
  return (a > b ? a : b);
}
template <class T>
T _min(T a, T b) {
  return (a < b ? a : b);
}
template <class T>
T max3(T a, T b, T c) {
  return (_max(a, b) > c ? _max(a, b) : c);
}
template <class T>
T min3(T a, T b, T c) {
  return (_min(a, b) < c ? _min(a, b) : c);
}
template <class T>
T GCD(T a, T b) {
  a = _abs(a);
  b = _abs(b);
  T tmp;
  while (a % b) {
    tmp = a % b;
    a = b;
    b = tmp;
  }
  return b;
}
template <class T>
T LCM(T a, T b) {
  a = _abs(a);
  b = _abs(b);
  return (a / GCD(a, b)) * b;
}
using namespace std;
int n, x, sm, m;
vector<int> v;
int a[1005], nw, i, tmp;
bool f[1005];
bool dp[1005][1005];
void func(int p, int sum) {
  if (sum + sm > n) return;
  f[sum + sm] = 1;
  if (p >= m) return;
  if (dp[p][sum]) return;
  dp[p][sum] = 1;
  func(p + 1, sum);
  func(p + 1, sum + v[p]);
}
int main() {
  v.clear();
  memset(f, 0, sizeof(f));
  scanf("%d", &n);
  scanf("%d", &x);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  sm = 1;
  nw = x;
  f[nw] = 1;
  while (a[nw]) {
    sm++;
    nw = a[nw];
    f[nw] = 1;
  }
  for (i = 1; i <= n; i++) {
    if (a[i] == 0 && f[i] == 0) {
      tmp = 1;
      nw = i;
      bool f1 = 1;
      while (f1) {
        f1 = 0;
        for (int j = 1; j <= n; j++) {
          if (a[j] == nw) {
            tmp++;
            f1 = 1;
            f[j] = 1;
            nw = j;
            break;
          }
        }
      }
      v.push_back(tmp);
    }
  }
  memset(f, 0, sizeof(f));
  m = v.size();
  memset(dp, 0, sizeof(dp));
  func(0, 0);
  for (i = 1; i <= n; i++)
    if (f[i]) {
      printf("%d\n", i);
    }
  return 0;
}
