#include <bits/stdc++.h>
using namespace std;
int fa[1010], son[1010];
int findfa(int x) {
  if (fa[x] != x) fa[x] = findfa(fa[x]);
  return fa[x];
}
int s[1010];
int cnts[1010];
int dp[1010];
int main() {
  int n, num, tmp;
  cin >> n >> num;
  for (int i = 1; i <= n; i++) fa[i] = i, son[i] = i;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (!tmp) continue;
    fa[i] = findfa(tmp);
    son[tmp] = i;
  }
  int cnt = 0;
  int tt = findfa(num);
  for (int i = 1; i <= n; i++) {
    if (findfa(i) == i) s[cnt++] = i;
  }
  for (int i = 0; i < cnt; i++) {
    int f = s[i];
    cnts[i] = 1;
    while (son[f] != f) cnts[i]++, f = son[f];
  }
  int nn = 0;
  for (int i = fa[num]; i != num; i = son[i]) nn++;
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  for (int j = 0; j < cnt; j++) {
    if (s[j] == tt) continue;
    for (int i = n - 1; i >= cnts[j]; i--) {
      if (!dp[i - cnts[j]]) continue;
      dp[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (dp[i]) cout << i + 1 + nn << endl;
  return 0;
}
