#include <bits/stdc++.h>
using namespace std;
vector<int> g[1005];
vector<int> bef[1005];
int viz[1005], v[1005], d[1005], act;
void dfs(int nod) {
  viz[nod] = 1;
  act++;
  for (vector<int>::iterator it = g[nod].begin(); it != g[nod].end(); it++)
    if (viz[*it] == 0) dfs(*it);
}
void dfsb(int nod) {
  act++;
  for (vector<int>::iterator it = bef[nod].begin(); it != bef[nod].end(); it++)
    dfsb(*it);
}
int main() {
  int n, i, j, s, nr, before, y;
  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    cin >> y;
    if (y != 0) {
      g[i].push_back(y);
      g[y].push_back(i);
      bef[i].push_back(y);
    }
  }
  dfs(s);
  act = 0;
  dfsb(s);
  before = act - 1;
  nr = 0;
  for (i = 1; i <= n; i++) {
    if (viz[i]) continue;
    act = 0;
    dfs(i);
    v[++nr] = act;
  }
  d[0] = 1;
  for (i = 1; i <= nr; i++)
    for (j = n; j - v[i] >= 0; j--)
      if (d[j - v[i]]) d[j] = 1;
  for (i = before + 1; i <= n; i++)
    if (d[i - before - 1] == 1) cout << i << '\n';
  return 0;
}
