#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  vector<bool> hasPrevious(n, false);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] > 0) {
      hasPrevious[a[i] - 1] = true;
    }
  }
  vector<int> queues(n, 0);
  int next = 0;
  for (int i = 0; i < n; i++) {
    if (!hasPrevious[i]) {
      int j = i;
      while (true) {
        queues[next]++;
        if (j == x - 1) {
          queues[next] = 0;
          next--;
          break;
        }
        if (a[j] == 0) {
          break;
        }
        j = a[j] - 1;
      }
      next++;
    }
  }
  int j = x - 1;
  int smartPos = 1;
  while (true) {
    if (a[j] == 0) {
      break;
    }
    j = a[j] - 1;
    smartPos++;
  }
  vector<bool> possible(n, false);
  vector<bool> selected(next, false);
  int actual = 0;
  int k = 1 << next;
  while (actual < k) {
    int aux = actual;
    int sum = 0;
    for (int i = 0; i < next; i++) {
      if ((aux & 1) == 1) {
        sum += queues[i];
      }
      aux = aux >> 1;
    }
    possible[sum + smartPos - 1] = true;
    actual++;
  }
  for (int i = 0; i < n; i++) {
    if (possible[i]) {
      cout << (i + 1) << endl;
    }
  }
  return 0;
}
