#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int t = 1, n, m, i, j, k, l, u, to;
  while (t--) {
    cin >> n >> k;
    k--;
    vector<int> a(n);
    for (auto &it : a) {
      cin >> it;
      it--;
    }
    vector<int> vis(n), nxt(n, -1);
    for (i = 0; i < n; i++) {
      if (a[i] != -1) {
        nxt[a[i]] = i;
        vis[i] = 1;
      }
    }
    vector<int> sz;
    int pos;
    for (i = 0; i < n; i++) {
      if (!vis[i]) {
        bool f = 0;
        u = i;
        int c = 0;
        while (u != -1) {
          if (u == k) {
            f = 1;
            pos = c;
          }
          u = nxt[u];
          c++;
        }
        if (!f) {
          sz.push_back(c);
        }
      }
    }
    m = sz.size() + 1;
    vector<int> dp(n + 1);
    dp[0] = 1;
    for (auto &it : sz) {
      for (i = n; i >= it; i--) {
        dp[i] |= dp[i - it];
      }
    }
    vector<int> ans;
    for (i = 0; i <= n; i++) {
      if (dp[i]) {
        ans.push_back(pos + i);
      }
    }
    sort(ans.begin(), ans.end());
    for (auto &it : ans) {
      cout << it + 1 << '\n';
    }
  }
}
