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 int max_a = 1002;
int n, x;
int a[max_a];
int b[max_a];
int b_len = 0;
int visited[max_a] = {0};
int root[max_a] = {0};
bool count_offset = false;
int offset = 0;
void visit(int k, int w) {
  visited[k] = true;
  if (count_offset) offset++;
  if (k == x) count_offset = true;
  if (a[k] == -1) {
    if (!count_offset) {
      b[b_len] = w;
      b_len++;
    }
  } else {
    visit(a[k], w + 1);
  }
}
bool answer[max_a][max_a];
int main() {
  cin >> n >> x;
  x--;
  for (int i = 0; i < n; i++) root[i] = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    if (a[i] != -1) {
      root[a[i]] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    count_offset = false;
    if (!visited[i] && root[i]) {
      visit(i, 1);
    }
  }
  if (b_len == 0) {
    printf("%d\n", offset + 1);
    return 0;
  }
  for (int i = 0; i <= b_len; i++) answer[0][i] = 1;
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= b_len; j++) {
      answer[i][j] = answer[i][j - 1];
      if (i >= b[j - 1])
        answer[i][j] = answer[i][j] || answer[i - b[j - 1]][j - 1];
    }
  for (int i = 0; i <= n; i++) {
    if (answer[i][b_len]) printf("%d\n", i + offset + 1);
  }
  return 0;
}
