#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int MAXN = 1000 + 7;
vector<int> g;
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  x--;
  g.resize(n, -1);
  vector<int> a(n);
  vector<int> starts;
  for (int i = (0); i < (n); ++i) {
    scanf("%d", &a[i]);
    if (a[i] == 0) {
      starts.push_back(i);
      continue;
    }
    a[i]--;
    g[a[i]] = i;
  }
  vector<int> v;
  int steps = -1;
  for (int i = (0); i < (int((starts).size())); ++i) {
    int next = starts[i];
    int k = 1;
    bool our = false;
    while (g[next] != -1) {
      if (next == x) {
        steps = k;
        our = true;
      }
      next = g[next];
      k++;
    }
    if (next == x) {
      steps = k;
      our = true;
    }
    if (!our) v.push_back(k);
  }
  vector<bool> sums(n * 3, false);
  sums[0] = true;
  for (int i = (0); i < (int((v).size())); ++i) {
    for (int j = (int((sums).size())) - 1; j >= (0); --j) {
      if (sums[j]) sums[v[i] + j] = true;
    }
  }
  for (int i = (0); i < (int((sums).size())); ++i)
    if (sums[i]) printf("%d\n", i + steps);
  return 0;
}
