#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];
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;
  for (int j = 0; j < vv; j++)
    for (int i = n; i >= g; i--)
      if (din[i] == 1) {
        din[i + v[j]] = 1;
      }
  for (int i = 0; i <= n; i++)
    if (din[i] == 1) cout << i << endl;
  return 0;
}
