#include <bits/stdc++.h>
using namespace std;
int s1, r = 0, s, q, e = 0, b[1001], d[1001], n, x, a[1001], f[1001] = {0}, c,
        h[1001];
void dfs(int i) {
  if (i == x) {
    s1 = c;
    r = 1;
  }
  c++;
  if (a[i] != 0) dfs(a[i]);
}
int main() {
  ifstream inf("input.txt");
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[a[i]] = 1;
  }
  for (int i = 1; i <= n; i++) {
    c = 0;
    if (f[i] == 0) dfs(i);
    if (c == 0)
      continue;
    else if (r == 0) {
      e++;
      b[e] = c;
    } else {
      s = c - s1 - 1;
      r = 0;
    }
  }
  d[0] = 1;
  for (int i = 1; i <= e; i++)
    for (int i1 = n; i1 >= b[i]; i1--)
      if (d[i1 - b[i]] == 1) d[i1] = 1;
  for (int i = 0; i <= n; i++)
    if (d[i] == 1) cout << i + s + 1 << endl;
}
