#include <bits/stdc++.h>
using namespace std;
int mark[1000 + 10];
int num[1000 + 10];
bool dp[10000];
int mark2[1000 + 10];
vector<int> v[1000 + 10];
vector<int> q[1000 + 10];
void dfs(int x, int c) {
  mark[x] = c;
  num[c]++;
  for (int i = 0; i < v[x].size(); i++)
    if (mark[v[x][i]] == 0) dfs(v[x][i], c);
}
void dfs1(int x) {
  for (int i = 0; i < q[x].size(); i++)
    if (mark2[q[x][i]] == 0) {
      mark2[q[x][i]] = mark2[x] + 1;
      dfs1(q[x][i]);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, x, in;
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> in;
    if (in != 0) {
      v[i].push_back(in);
      q[in].push_back(i);
    }
  }
  int col = 1;
  for (int i = 1; i <= n; i++) {
    if (q[i].size() == 0) {
      dfs(i, col);
      col++;
    }
  }
  int p;
  for (int i = 1; i <= n; i++) {
    if (v[i].size() == 0 && mark[i] == mark[x]) {
      mark2[i] = 1;
      dfs1(i);
      p = mark2[x];
      i = n + 1;
    }
  }
  vector<int> vv;
  for (int i = 1; i <= n; i++) {
    if (q[i].size() == 0 && mark[i] != mark[x]) vv.push_back(num[mark[i]]);
  }
  dp[0] = true;
  for (int i = 0; i < vv.size(); i++) {
    for (int j = 1000; j >= 0; j--) {
      if (dp[j] == true) dp[j + vv[i]] = true;
    }
  }
  for (int i = 0; i <= 1000; i++)
    if (dp[i] == true) cout << i + p << endl;
}
