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>
#pragma comment(linker, "/STACK:94777216")
using namespace std;
int v[1001], vv = 0, n, k;
int ms[1001];
int m[1001];
int din[1001];
string st[1001];
int g, b = -1;
int dfs(int t, int h) {
  if (t == k - 1) {
    g = h;
    b = vv;
  }
  if (m[t + 1] == 0) {
    if (b == vv) {
      b = -1;
      return 0;
    }
    v[vv] = h;
    vv++;
    return 0;
  }
  dfs(m[t + 1] - 1, h + 1);
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> ms[i];
    if (ms[i] != 0) m[ms[i]] = i + 1;
  }
  for (int i = 0; i < n; i++)
    if (ms[i] == 0) dfs(i, 1);
  if (g == 0) g = 1;
  din[g] = 1;
  st[g].assign(vv, 0);
  for (int i = 0; i < vv; i++) st[g][i] = '1';
  for (int i = g + 1; i <= n; i++)
    for (int j = 0; j < vv; j++)
      if ((i - v[j] > 0) && (din[i - v[j]] == 1) && (st[i - v[j]][j] == '1')) {
        din[i] = 1;
        st[i] = st[i - v[j]];
        st[i][j] = '0';
        break;
      }
  for (int i = 0; i <= n; i++)
    if (din[i] == 1) cout << i << endl;
  return 0;
}
