#include <bits/stdc++.h>
using namespace std;
int before[1000];
int after[1000];
bool dp[1010];
int main() {
  dp[0] = 0;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    after[i] = -1;
  }
  k--;
  for (int i = 0; i < n; i++) {
    cin >> before[i];
    before[i]--;
    if (before[i] != -1) {
      after[before[i]] = i;
    }
  }
  dp[0] = true;
  int inc = 0;
  for (int i = 0; i < n; i++) {
    if (before[i] == -1) {
      int t = i;
      bool f = false;
      int c = 0;
      while (t != -1) {
        if (t == k) {
          f = true;
          break;
        }
        t = after[t];
        c++;
      }
      if (!f) {
        for (int i = 1000; i >= 0; i--) {
          if (dp[i]) {
            dp[i + c] = true;
          }
        }
      } else {
        inc = c;
      }
    }
  }
  for (int i = 0; i < 1000; i++) {
    if (dp[i]) {
      cout << i + inc + 1 << '\n';
    }
  }
}
