#include <bits/stdc++.h>
using namespace std;
int father[100010];
int a[100010];
int find(int v) {
  if (father[v] != v) father[v] = find(father[v]);
  return father[v];
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  int n, x;
  while (~scanf("%d%d", &n, &x)) {
    for (int i = 1; i <= n; i++) father[i] = i;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i]) {
        int fx = find(father[i]);
        int fy = find(father[a[i]]);
        father[fx] = father[fy];
      }
    }
    int len = 0;
    for (int i = x; i != 0; i = a[i]) len++;
    map<int, int> mm;
    for (int i = 1; i <= n; i++) {
      int xx = find(father[i]);
      if (((mm).find(xx) != (mm).end()))
        mm[xx]++;
      else
        mm.insert(make_pair(xx, 1));
    }
    int self = find(father[x]);
    vector<int> bp;
    for (map<int, int>::iterator it = mm.begin(); it != mm.end(); it++)
      if (it->first != self) bp.push_back(it->second);
    int flag[2000] = {0};
    flag[len] = true;
    for (int i = 0; i < bp.size(); i++)
      for (int j = n; j - bp[i] >= len; j--) {
        if (flag[j - bp[i]]) flag[j] = true;
      }
    for (int i = 1; i <= n; i++)
      if (flag[i]) printf("%d\n", i);
  }
  return 0;
}
