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;
int nextnum = 1;
int mynum = 0;
int resultnum = 0;
int check[1100] = {0};
int x;
int main() {
  int n, error;
  error = 0;
  cin >> n >> x;
  int input[1100] = {0};
  int count[1100] = {0};
  int result[10000] = {0};
  void numsearch(int *input, int *check, int x);
  for (int i = 0; i < n; i++) {
    cin >> input[i];
  }
  numsearch(input, check, x - 1);
  int num(int *input, int *check, int i);
  for (int i = 0; i < n; i++) {
    if (check[i] == 0) {
      check[i] = num(input, check, i);
    }
  }
  for (int i = 1; i < nextnum; i++) {
    for (int j = 0; j < n; j++) {
      if (check[j] == i) {
        count[i]++;
      } else {
        error++;
      }
    }
  }
  count[x] = mynum;
  bool use[1100];
  use[0] = false;
  void shukei(bool *use, int now, int *count, int *result);
  shukei(use, 1, count, result);
  sort(result, result + 10000);
  for (int i = 0; i < 10000; i++) {
    if (result[i] != 0 && result[i] != result[i + 1]) {
      cout << result[i] << endl;
    }
  }
}
int num(int *input, int *check, int i) {
  if (input[i] != 0) {
    int j = input[i];
    return check[i] = num(input, check, j - 1);
  } else {
    if (check[i] != 0) {
      return check[i];
    } else {
      return check[i] = nextnum++;
    }
  }
}
void numsearch(int *input, int *check, int x) {
  if (input[x] == 0) {
    mynum++;
  } else {
    int j = input[x];
    mynum++;
    numsearch(input, check, j - 1);
  }
}
void shukei(bool *use, int now, int *count, int *result) {
  if (now < nextnum) {
    use[now] = true;
    shukei(use, now + 1, count, result);
    use[now] = false;
    shukei(use, now + 1, count, result);
  } else {
    for (int i = 0; i < nextnum; i++) {
      if (use[i] && use[check[x]]) {
        result[resultnum] += count[i];
      }
    }
    resultnum++;
  }
}
