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;
vector<int> v;
int n;
int x;
int a[3270];
int used[3270];
int dp[3270];
int bplace = -1;
int plc;
void input();
void solve();
void f(int vertex);
int main() {
  input();
  solve();
  return 0;
}
void input() {
  scanf("%d%d", &n, &x);
  int i;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
}
void solve() {
  int i, j;
  int lstplc;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (a[j] == i) break;
    }
    plc = 0;
    lstplc = bplace;
    if (used[i] == 0 && j == n + 1) {
      f(i);
      if (bplace != -1 && lstplc == -1) {
        bplace = plc - bplace + 1;
        continue;
      }
      v.push_back(plc);
    }
  }
  dp[0] = 1;
  int m = v.size();
  for (i = 0; i < m; i++) {
    for (j = 0; j <= n; j++) {
      if (dp[j] == 1 && dp[j + v[i]] == 0) {
        dp[j + v[i]] = 2;
      }
    }
    for (j = 0; j <= n; j++) {
      if (dp[j] == 2) dp[j] = 1;
    }
  }
  for (i = 0; i <= n; i++) {
    if (dp[i] == 1) printf("%d\n", i + bplace);
  }
}
void f(int vertex) {
  plc++;
  used[vertex] = 1;
  if (vertex == x) {
    bplace = plc;
  }
  if (a[vertex] == 0) return;
  f(a[vertex]);
}
