#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000;
int A[MAXN];
vector<int> adj[MAXN];
int hasIn[MAXN];
int dp[MAXN];
void calc(int n, int x, vector<int> length) {
  dp[0] = 1;
  for (int i = 0; i < length.size(); i++) {
    for (int w = n; w >= 0; w--) {
      if (dp[w] == 0) continue;
      if (w + length[i] <= n) dp[w + length[i]] = 1;
    }
  }
}
void buildGraph(int n) {
  for (int i = 1; i <= n; i++) {
    int v = A[i];
    if (v == 0) continue;
    adj[i].push_back(v);
    hasIn[v] = 1;
  }
}
vector<int> length;
int mark[MAXN];
int taken[MAXN];
int main() {
  int n, x;
  cin >> n >> x;
  for (int i = 1; i <= n; i++) cin >> A[i];
  buildGraph(n);
  vector<int> xV;
  for (int i = 1; i <= n; i++) {
    if (hasIn[i]) continue;
    vector<int> v;
    int found = (i == x);
    int node = i;
    while (adj[node].size() != 0) {
      v.push_back(node);
      node = adj[node][0];
      found = found || (node == x);
    }
    v.push_back(node);
    if (found)
      xV = v;
    else
      length.push_back(v.size());
  }
  calc(n, x, length);
  sort(length.begin(), length.end());
  reverse(xV.begin(), xV.end());
  int totalLength = 0;
  vector<int> ret;
  for (int i = 1; i <= n; i++) {
    if (n - i + 1 < xV.size()) continue;
    if (dp[i - 1] && dp[n - (i + xV.size()) + 1]) ret.push_back(i);
  }
  int pos = 0;
  for (; pos < xV.size(); pos++)
    if (xV[pos] == x) break;
  sort(ret.begin(), ret.end());
  for (int i = 0; i < ret.size(); i++) {
    cout << ret[i] + pos << endl;
  }
  return 0;
}
