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> adj[1001];
vector<int> firsts;
int calculateDistance(int currPos, int prev) {
  if (adj[currPos].size() == 0) return 1;
  if (adj[currPos].size() == 1 && prev != 0) return 1;
  for (auto next : adj[currPos]) {
    if (next != prev) return 1 + calculateDistance(next, currPos);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, x;
  scanf("%d %d", &n, &x);
  int a, prevSmartBeaver = 0, firstElQSmartB = x;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    if (a != 0) {
      if (i == firstElQSmartB) {
        firstElQSmartB = a;
      }
      adj[i].push_back(a);
      adj[a].push_back(i);
    } else {
      firsts.push_back(i);
    }
    if (a == x) prevSmartBeaver = i;
  }
  int distSmartBeaver = calculateDistance(x, prevSmartBeaver);
  vector<int> lEachQ;
  lEachQ.push_back(0);
  for (auto v : firsts) {
    if (v != firstElQSmartB) lEachQ.push_back(calculateDistance(v, 0));
  }
  sort(lEachQ.begin(), lEachQ.begin() + lEachQ.size());
  for (auto l : lEachQ) {
  }
  bool possible[1001][1001] = {false};
  bool possibility[1001] = {false};
  possible[0][0] = true;
  possibility[0] = true;
  for (int k = 1; k <= lEachQ.size(); k++) {
    for (int x = 0; x <= n; x++) {
      if (x - lEachQ[k] >= 0) possible[x][k] |= possible[x - lEachQ[k]][k - 1];
      possible[x][k] |= possible[x][k - 1];
      if (possible[x][k]) possibility[x] = true;
    }
  }
  for (int i = 0; i <= n; i++) {
    if (possibility[i]) cout << i + distSmartBeaver << "\n";
  }
  return 0;
}
