20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

#include <bits/stdc++.h>
using namespace std;
const long long int INF = (1LL << 58);
const bool DEBUG = 0;
const int MAX_LIMIT = 1003;
int color[MAX_LIMIT], pos[MAX_LIMIT], componentSize[MAX_LIMIT];
std::vector<int> adj[MAX_LIMIT];
bool vis[MAX_LIMIT];
int currColor;
int globalColor = 1;
int nodes, nodeId;
set<int> ans;
bool done[MAX_LIMIT][MAX_LIMIT];
int before[MAX_LIMIT];
int dfs(int node, int currRank) {
  if (vis[node]) return 0;
  vis[node] = 1;
  color[node] = currColor;
  pos[node] = currRank;
  int i, limit = adj[node].size();
  if (DEBUG) {
    cerr << node << " " << currColor << "\n";
  }
  int ret = 0;
  for (i = 0; i < limit; i++) {
    int v = adj[node][i];
    ret += dfs(v, currRank + 1);
  }
  return ret + 1;
}
void rec(int ind, int currSum) {
  if (ind == globalColor) {
    ans.insert(currSum + pos[nodeId]);
    return;
  }
  if (done[ind][currSum]) return;
  done[ind][currSum] = 1;
  rec(ind + 1, currSum);
  if (ind != color[nodeId]) {
    rec(ind + 1, currSum + componentSize[ind]);
  }
  return;
}
int main() {
  int i, j;
  scanf("%d", &nodes);
  scanf("%d", &nodeId);
  for (i = 1; i < nodes + 1; i++) {
    int x;
    scanf("%d", &x);
    adj[x].push_back(i);
    before[i] = x;
  }
  for (i = 1; i < nodes + 1; i++) {
    if (before[i] == 0) {
      currColor = globalColor;
      componentSize[globalColor] = dfs(i, 1);
      globalColor++;
    }
  }
  rec(1, 0);
  set<int>::iterator itr;
  for (itr = ans.begin(); itr != ans.end(); itr++) {
    printf("%d\n", *itr);
  }
  return 0;
}
