#include <bits/stdc++.h>
using namespace std;
const int N = 1024;
int n, x, front[N], nxt[N], len[N];
int dp[N];
vector<int> in;
int main(int argc, char** argv) {
  scanf("%d %d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &front[i]);
    nxt[front[i]] = i;
  }
  int cnt = 0, cur = x;
  while (cur) {
    cnt++;
    cur = front[cur];
  }
  for (int i = 1; i <= n; i++) {
    if (!front[i]) {
      cur = i;
      len[i] = 0;
      while (cur) {
        len[i]++;
        if (cur == x) {
          len[i] = -1;
          break;
        }
        cur = nxt[cur];
      }
      if (len[i] != -1) {
        in.push_back(len[i]);
      }
    }
  }
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  for (int i = 0; i < ((int)in.size()); i++) {
    for (int j = n; j >= in[i]; j--) {
      if (dp[j - in[i]]) {
        dp[j] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (dp[i]) printf("%d\n", i + cnt);
  return 0;
}
