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;
long long a[1002];
long long n, k;
long long queues[1002];
vector<long long> lens;
vector<long long> nils;
set<long long> results;
int main() {
  freopen("input.txt", "rt", stdin);
  scanf("%d %d", &n, &k);
  long long t;
  for (long long i = 1; i <= n; ++i) {
    scanf("%d", &t);
    a[i] = t;
    queues[t] = i;
    if (t == 0) nils.push_back(i);
  }
  long long prev = 0;
  {
    long long cur = k;
    while (a[cur]) {
      prev += 1;
      cur = a[cur];
    }
  }
  for (vector<long long>::iterator it = nils.begin(); it != nils.end(); it++) {
    long long len = 1;
    long long cur = *it;
    bool ok = cur != k;
    while (queues[cur]) {
      if (cur == k) {
        ok = false;
        break;
      }
      len += 1;
      cur = queues[cur];
    }
    if (ok && cur != k) lens.push_back(len);
  }
  results.insert(prev);
  long long nn = lens.size();
  long long maxmask = 1;
  for (long long i = 0; i <= nn; ++i) {
    maxmask <<= 1;
    maxmask |= 1;
  }
  for (long long mask = 1; mask <= maxmask; ++mask) {
    long long i = 0;
    long long len = 0;
    for (vector<long long>::iterator it = lens.begin(); it != lens.end();
         it++, i += 1) {
      if ((mask >> i) & 1) len += *it;
    }
    results.insert(len + prev);
  }
  for (set<long long>::iterator it = results.begin(); it != results.end();
       it++) {
    cout << *it + 1;
  }
  return 0;
}
