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;
int indeg[1024];
int adj[1024];
bool mark[1024];
int depth[1024];
int n, id, comp;
void dfs(int x, int d) {
  comp++;
  mark[x] = true;
  depth[x] = d;
  if (adj[x] != 0) {
    indeg[adj[x]]--;
    dfs(adj[x], d + 1);
  }
}
bool dp[1024][1024];
int main() {
  memset(indeg, 0, sizeof indeg);
  int front, back;
  cin >> n >> id;
  for (int i = 1; i <= n; i++) {
    cin >> front;
    indeg[front]++;
    adj[i] = front;
  }
  int myGroup = -1;
  memset(mark, 0, sizeof mark);
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      if (indeg[j] == 0 && mark[j] == false) {
        comp = 0;
        dfs(j, 1);
        if (mark[id] == true && myGroup == -1)
          myGroup = comp;
        else
          v.push_back(comp);
      }
  }
  if (v.size()) {
    memset(dp, 0, sizeof dp);
    dp[0][0] = true;
    dp[0][v[0]] = true;
    for (int i = 1; i < v.size(); i++) {
      for (int j = 0; j < 1024; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j - v[i] >= 0) dp[i][j] |= dp[i - 1][j - v[i]];
      }
    }
  }
  int Left, Right;
  Left = depth[id] - 1;
  Right = myGroup - depth[id];
  for (int i = 1; i <= n; i++) {
    front = i - 1;
    back = n - i;
    front -= Right;
    back -= Left;
    if (front >= 0 && back >= 0 && dp[v.size() - 1][front]) cout << i << endl;
  }
  return 0;
}
