20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

#include <bits/stdc++.h>
using namespace std;
const int maxN = int(8e5);
const int smaxN = int(5e3);
const int lmaxN = int(3e6);
const int oo = int(1e18);
const int MOD = int(1e9) + 7LL;
const long double EPS = 1e-9;
const long double PI = acos(0.0) * 2;
const int LOGMAX = 22;
template <typename T>
T Sqr(T x) {
  return x * x;
}
long double Sqrt(long double x) {
  long double lf = 0.0, rg = x;
  while (rg - lf > EPS) {
    long double mid = (lf + rg) / 2.0;
    if (mid * mid <= x) {
      lf = mid;
    } else {
      rg = mid;
    }
  }
  return lf;
}
template <typename T>
T Sqrt(T x) {
  int lf = 0, rg = x;
  while (rg - lf > 1) {
    int mid = (lf + rg) / 2;
    if (mid * mid <= x) {
      lf = mid;
    } else {
      rg = mid;
    }
  }
  for (int i = rg; i >= lf; --i) {
    if (i * i <= x) {
      return i;
    }
  }
}
int n, x, z, sz;
int r[maxN], p[maxN], all[maxN], u[maxN], dp[maxN];
int Find(int xx) { return (p[xx] == xx ? xx : p[xx] = Find(p[xx])); }
void Union(int xx, int y) {
  xx = Find(xx), y = Find(y);
  if (xx == y) {
    return;
  }
  if (r[xx] < r[y]) {
    swap(xx, y);
  }
  r[xx] += r[y], p[y] = xx;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> x;
  for (int i = 1; i <= n; ++i) {
    p[i] = i, r[i] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> z;
    if (i == x) {
      if (!z) {
        dp[1] = 1;
      } else {
        dp[r[Find(z)] + 1] = 1;
      }
    }
    if (z) {
      Union(i, z);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (Find(i) != Find(x) && !u[Find(i)]) {
      u[Find(i)] = 1;
      all[++sz] = r[Find(i)];
    }
  }
  for (int i = 1; i <= sz; ++i) {
    for (int j = n; j >= all[i]; --j) {
      dp[j] = max(dp[j], dp[j - all[i]]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (dp[i]) {
      cout << i << "\n";
    }
  }
  return 0;
}
