#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0, 0, 0};
const long long inf = 100000007;
const double eps = 0.000000009;
long long mod = 1000000007;
long long n, m;
map<int, vector<int> > a, b;
set<int> roots;
void root(int node) {
  vector<int> x = a[node];
  if (x.size() == 0) {
    roots.insert(node);
    return;
  } else
    root(a[node][0]);
}
vector<vector<int> > sq;
vector<int> temp;
void dfs(int node) {
  vector<int> x = b[node];
  if (x.size() == 0) {
    vector<int> xx = temp;
    sq.push_back(xx);
    return;
  } else {
    temp.push_back(x[0]);
    dfs(x[0]);
    temp.pop_back();
  }
}
vector<int> dps;
int addi = 0;
set<int> sol;
void recur(int c, int sum) {
  if (c == dps.size()) {
    sol.insert(sum);
    return;
  }
  recur(c + 1, sum);
  recur(c + 1, sum + dps[c]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    x--;
    if (x < 0)
      continue;
    else {
      a[i].push_back(x);
      b[x].push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    root(i);
  }
  set<int>::iterator it = roots.begin();
  for (it; it != roots.end(); it++) {
    temp.push_back(*it);
    dfs(*it);
    temp.pop_back();
  }
  for (int i = 0; i < sq.size(); i++) {
    int found = 0;
    for (int j = 0; j < sq[i].size(); j++) {
      if (sq[i][j] == m - 1) {
        found = 1;
        addi = j + 1;
      }
    }
    if (!found) {
      dps.push_back(sq[i].size());
    }
  }
  recur(0, 0);
  set<int>::iterator it1 = sol.begin();
  for (it1; it1 != sol.end(); it1++) {
    cout << *it1 + addi << endl;
  }
  return 0;
}
