#include <bits/stdc++.h>
using namespace std;
int arr[1000000];
vector<vector<int> > adjlist, adjlist1;
vector<int> visited, visited1;
int coun = 0;
void dfs(int s) {
  visited[s] = 1;
  coun++;
  for (int i = 0; i < adjlist[s].size(); i++) {
    int v = adjlist[s][i];
    if (visited[v] == 0) dfs(v);
  }
}
int f = 0;
void dfs1(int s) {
  visited1[s] = 1;
  f++;
  for (int i = 0; i < adjlist1[s].size(); i++) {
    int v = adjlist1[s][i];
    if (visited1[v] == 0) dfs1(v);
  }
}
vector<int> vi;
int is[1001][1001], memo[1001][1001];
int recursive(int s, int le) {
  if (le < 0) return 0;
  if (s == vi.size() && le == 0) return 1;
  if (s == vi.size()) return 0;
  if (is[s][le]) return memo[s][le];
  is[s][le] = true;
  return memo[s][le] = recursive(s + 1, le) + recursive(s + 1, le - vi[s]);
}
int main() {
  int n, x;
  cin >> n >> x;
  visited.assign(n + 1, 0);
  visited1.assign(n + 1, 0);
  adjlist.assign(n + 1, vector<int>());
  adjlist1.assign(n + 1, vector<int>());
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    if (arr[i] != 0) {
      adjlist[i].push_back(arr[i]);
      adjlist[arr[i]].push_back(i);
      adjlist1[i].push_back(arr[i]);
    }
  }
  dfs1(x);
  dfs(x);
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      coun = 0;
      dfs(i);
      vi.push_back(coun);
    }
  }
  cout << f << '\n';
  for (int i = 1; i <= n; i++) {
    if (recursive(0, i)) cout << i + f << '\n';
  }
  return 0;
}
