#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
bool mark[2000], check[2000], flag[2000];
int i, n, k, b[2000], a[2000], c[2000], iraverjic, iranicaraj, type[2000000], r,
    pop, q, j, in;
int main() {
  cin >> n >> k;
  in = k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] != 0) {
      b[a[i]] = i;
    }
  }
  mark[k] = 1;
  while (b[k] != 0) {
    iraverjic++;
    mark[b[k]] = 1;
    k = b[k];
  }
  k = in;
  while (a[k] != 0) {
    iranicaraj++;
    mark[a[k]] = 1;
    k = a[k];
  }
  for (i = 1; i <= n; i++) {
    q = 0;
    if (mark[i] == 0) {
      pop = i;
      mark[pop] = 1;
      while (mark[a[pop]] == 0 && a[pop] != 0) {
        mark[a[pop]] = 1;
        pop = a[pop];
        q++;
      }
      pop = i;
      while (mark[b[pop]] == 0 && b[pop] != 0) {
        mark[b[pop]] = 1;
        pop = b[pop];
        q++;
      }
      q++;
      r++;
      type[r] = q;
    }
  }
  check[iranicaraj + 1] = 1;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= 1000; j++) {
      flag[j] = 0;
    }
    for (j = 1; j <= 1000; j++) {
      if (check[j] == 1 && iraverjic + j + type[i] <= 1000 && flag[j] == 0 &&
          check[j + type[i]] == 0) {
        flag[j + type[i]] = 1;
        check[j + type[i]] = 1;
      }
    }
  }
  for (i = 1 + iranicaraj; i <= n - iraverjic; i++) {
    if (check[i] == 1) {
      cout << i << endl;
    }
  }
  return 0;
}
