#include <bits/stdc++.h>
using namespace std;
int a[111111];
int p[111111];
long long n, m, x, q, l, r, lindex;
int main() {
  vector<int> lines[1111];
  cin >> n >> x;
  for (int i = (1); i < (n + 1); i++) p[i] = -1;
  for (int i = (1); i < (n + 1); i++) {
    cin >> a[i];
    p[a[i]] = i;
  }
  int lindex;
  for (int i = (1); i < (n + 1); i++) {
    if (a[i] == 0) {
      if (x == i) lindex = i;
      lines[i].push_back(i);
      int j = p[i];
      if (x == j) lindex = i;
      while (j != -1) {
        lines[i].push_back(j);
        j = p[j];
        if (x == j) lindex = i;
      }
    }
  }
  int posx = 0;
  for (int i = (0); i < (lines[lindex].size()); i++) {
    if (x == lines[lindex][i]) {
      posx = i + 1;
      break;
    }
  }
  vector<int> sizes;
  for (int i = (0); i < (n + 1); i++) {
    if (lines[i].size()) {
      if (i == lindex)
        ;
      else
        sizes.push_back(lines[i].size());
    }
  }
  int m[n + 10], i, j;
  for (i = 0; i < n + 10; i++) m[i] = 0;
  m[0] = 1;
  for (i = 0; i < sizes.size(); i++)
    for (j = n; j >= sizes[i]; j--) m[j] |= m[j - sizes[i]];
  for (int i = (0); i < (n); i++)
    if (m[i]) cout << i + posx << endl;
  return 0;
}
