#include <bits/stdc++.h>
using namespace std;
int n, x;
int father[1001];
int son[1001];
bool dp[1001];
bool dp2[1001];
int MAIN() {
  while (cin >> n >> x) {
    memset(father, 0, sizeof(father));
    memset(son, 0, sizeof(son));
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for (int i = 1; i <= n; i++) {
      int t;
      cin >> t;
      if (t == 0) continue;
      father[i] = t;
      son[t] = i;
    }
    int pX = 0;
    for (int i = 1; i <= n; i++)
      if (father[i] == 0) {
        int cntNode = 1;
        int posX = 0;
        int t = i;
        if (t == x) posX = cntNode;
        while (son[t]) {
          t = son[t];
          cntNode++;
          if (t == x) posX = cntNode;
        }
        if (posX > 0)
          pX = posX;
        else {
          for (int i = 0; i <= n; i++) {
            dp2[i] = false;
            if (dp[i]) dp2[i] = true;
            if (i >= cntNode && dp[i - cntNode]) dp2[i] = true;
          }
          for (int i = 0; i <= n; i++) dp[i] = dp2[i];
        }
      }
    for (int i = 0; i <= n; i++)
      if (dp[i]) cout << i + pX << endl;
  }
  return 0;
}
int main() {
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  return MAIN();
}
