#include <bits/stdc++.h>
using namespace std;
int to[1000];
int from[1000];
int id[1000];
int head[1000];
int sz[1000];
vector<int> comp;
int dp[1001];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  memset(from, -1, sizeof(from));
  for (int i = 0; i < (n); i++) {
    int v;
    scanf("%d", &v);
    v--;
    to[i] = v;
    from[v] = i;
    if (v == -1) head[i] = 1;
  }
  for (int i = 0; i < (n); i++) {
    if (head[i]) {
      bool iAmHere = false;
      int cnt = 0;
      int cur = i;
      while (cur != -1) {
        cnt++;
        id[cur] = cnt;
        if (cur == k - 1) iAmHere = true;
        cur = from[cur];
      }
      if (!iAmHere) comp.push_back(cnt);
    }
  }
  dp[0] = 1;
  for (int i = 0; i < comp.size(); i++)
    for (int j = n - comp[i]; j >= 0; j--) dp[j + comp[i]] |= dp[j];
  bool first = true;
  for (int i = 0; i <= n; i++) {
    if (dp[i]) {
      printf("%d\n", i + id[k - 1]);
    }
  }
  return 0;
}
