#include <bits/stdc++.h>
using namespace std;
int f[1001], r[1001], s[1001], p[1001], q, n;
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) scanf("%d", f + i);
  for (int i = 1; i <= n; i++) r[f[i]] = i;
  {
    int k = 0;
    for (int i = q; i; i = f[i]) {
      k++;
      s[i] = 1;
    }
    for (int i = r[q]; i; i = r[i]) s[i] = 1;
    p[k] = 1;
  }
  for (int i = 1; i <= n; i++)
    if ((!r[i]) && (!s[i])) {
      int k = 0;
      for (int j = i; j; j = f[j]) {
        k++;
        s[j] = 1;
      }
      for (int j = n; j >= k + 1; j--)
        if (p[j - k]) p[j] = 1;
    }
  for (int i = 1; i <= n; i++)
    if (p[i]) printf("%d\n", i);
  return 0;
}
