#include <bits/stdc++.h>
using namespace std;
bool vis[1005], flag;
int f[1005], v;
int pre[1005], Next[1005], n, m;
map<int, int> imap;
map<int, int>::iterator it;
int getpre(int x) {
  int res = 0;
  while (pre[x] != -1) {
    res++;
    vis[x] = 1;
    if (x == m) flag = 1;
    x = pre[x];
  }
  vis[x] = 1;
  if (x == m) flag = 1;
  return res;
}
int getnext(int x) {
  int res = 0;
  while (Next[x] != -1) {
    res++;
    vis[x] = 1;
    if (x == m) flag = 1;
    x = Next[x];
  }
  vis[x] = 1;
  if (x == m) flag = 1;
  return res;
}
void pack01(int w) {
  int i;
  for (i = v; i >= w; i--) f[i] += f[i - w];
}
void allpack(int w) {
  int i;
  for (i = w; i <= v; i++) f[i] += f[i - w];
}
void mulpack(int w, int c) {
  int i;
  if (w * c >= v) allpack(w);
  for (i = 1; i < c; i *= 2) {
    pack01(i * w);
    c -= i;
  }
  pack01(c * w);
}
int main() {
  int i, x, sure, nosure;
  while (scanf("%d%d", &n, &m) != EOF) {
    v = n;
    memset(f, 0, sizeof(f));
    f[0] = 1;
    imap.clear();
    sure = nosure = 0;
    memset(vis, 0, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    memset(Next, -1, sizeof(Next));
    for (i = 1; i <= n; i++) {
      scanf("%d", &x);
      if (x) {
        Next[x] = i;
        pre[i] = x;
      }
    }
    for (i = 1; i <= n; i++)
      if (!vis[i]) {
        flag = 0;
        int c = getpre(i) + getnext(i) + 1;
        if (c > 1 && !flag) imap[c]++, sure += c;
      }
    int tmp = getpre(m) + getnext(m) + 1;
    nosure = n - sure - tmp;
    imap[1] = nosure;
    int minpos = getpre(m) + 1;
    for (it = imap.begin(); it != imap.end(); it++)
      mulpack(it->first, it->second);
    for (i = 0; i <= n; i++)
      if (f[i]) printf("%d\n", i + minpos);
  }
  return 0;
}
