#include <bits/stdc++.h>
using namespace std;
void comb(int* r, int n) {
  for (int i = 999; i >= 0; --i) {
    if (r[i] != 0) {
      r[i + n] = 1;
    }
  }
}
int main() {
  int n, x;
  cin >> n >> x;
  int a[1000] = {};
  int b[1000] = {};
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] != 0) {
      b[a[i] - 1] = i + 1;
    }
  }
  int s[1000] = {};
  int sbn = 0;
  int c = 0;
  for (int i = 0, j = 0; i < n; ++i) {
    int sb = 0;
    if (a[i] == 0) {
      int ss = 1;
      int bb = i;
      if (bb == (x - 1)) {
        sb = ss;
      }
      while (b[bb] != 0) {
        ++ss;
        bb = b[bb] - 1;
        if (bb == (x - 1)) {
          sb = ss;
        }
      }
      if (sb == 0) {
        s[j++] = ss;
      } else {
        sbn = sb;
      }
    }
    c = j;
  }
  sort(s, s + c, less<int>());
  int r[1000] = {};
  r[0] = 1;
  for (int i = 0; i < c; ++i) {
    int ss = s[i];
    comb(&r[0], ss);
  }
  for (int i = 0; i < n; ++i) {
    if (r[i] != 0) {
      cout << i + sbn << endl;
    }
  }
  return 0;
}
