#include <bits/stdc++.h>
using namespace std;
int flag[1100], f[1100], tot, x, y, n, v1, v2;
bool dp[1100][1100];
struct Node {
  int v, pt;
} node[1100];
void dfs(int i) {
  if (node[i].pt == 0)
    flag[i]++;
  else
    dfs(node[i].pt);
}
void check(int i) {
  v2++;
  if (node[i].pt == 0) {
    v1 = flag[i];
    flag[i] = 0;
  } else
    check(node[i].pt);
}
int main() {
  while (~scanf("%d%d", &n, &x)) {
    memset(node, 0, sizeof(node));
    memset(flag, 0, sizeof(flag));
    memset(dp, 0, sizeof(dp));
    tot = 0;
    v2 = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &node[i].pt);
    }
    for (int i = 1; i <= n; i++) {
      dfs(i);
    }
    check(x);
    for (int i = 1; i <= n; i++) {
      if (flag[i]) {
        f[++tot] = flag[i];
      }
    }
    dp[0][0] = true;
    for (int i = 1; i <= tot; i++) {
      for (int j = 0; j <= n; j++) {
        if (dp[i - 1][j]) {
          dp[i][j] = true;
          dp[i][j + f[i]] = true;
        }
      }
    }
    for (int i = 0; i <= n; i++) {
      if (dp[tot][i]) {
        printf("%d\n", i + v2);
      }
    }
  }
  return 0;
}
