#include <bits/stdc++.h>
using namespace std;
int a[1005], p[1005], z[1005], d[1005];
inline void dfs(int k, int n) {
  d[k] = 1;
  if (!n) return;
  for (int i = 0; i <= z[n]; ++i) dfs(k + i * n, n - 1);
}
int main() {
  int n, x, k = 0, m = 0;
  scanf("%d%d", &n, &x), memset(d, 0, sizeof d), memset(p, 0, sizeof p),
      memset(z, 0, sizeof z);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), p[a[i]] = i;
  for (int i = x; i; i = a[i], ++k)
    ;
  for (int i = 1, l = 0; i <= n; ++i, l = 0)
    if (i != x && !a[i]) {
      for (int y = i; y; y = p[y], ++l)
        if (y == x) {
          l = 0;
          break;
        }
      z[l]++, m = max(m, l);
    }
  dfs(k, m);
  for (int i = 1; i <= n; ++i)
    if (d[i]) printf("%d\n", i);
}
