#include <bits/stdc++.h>
using namespace std;
int l[(100000 + 10)], size = 0;
int fa[(100000 + 10)], son[(100000 + 10)], n, x, spec, specp;
bool b[(100000 + 10)] = {0}, f[(100000 + 10)] = {0};
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) scanf("%d", &fa[i]);
  for (int i = 1; i <= n; i++)
    if (fa[i]) son[fa[i]] = i;
  for (int i = 1; i <= n; i++)
    if (fa[i] == 0) {
      int tot = 1, j = i;
      bool flag = b[x];
      b[i] = 1;
      j = i;
      if (i == x) spec = 1;
      while (son[j]) {
        b[son[j]] = 1, tot++, j = son[j];
        if (j == x) spec = tot;
      }
      if (b[x] == flag)
        l[++size] = tot;
      else
        specp = spec;
    }
  f[0] = 1;
  for (int i = 1; i <= size; i++) {
    for (int j = n; j >= 0; j--) {
      if (f[j]) f[j + l[i]] = 1;
    }
  }
  for (int j = 0; j < n + 1; j++)
    if (f[j]) cout << j + specp << endl;
  return 0;
}
