#include <bits/stdc++.h>
using namespace std;
struct st {
  int pr;
  int next;
  bool in;
  int id;
};
int main() {
  int N;
  cin >> N;
  st q;
  q.next = -1;
  q.in = 0;
  q.pr = -1;
  vector<st> v(N, q);
  int K;
  cin >> K;
  K--;
  for (int i = 0; i < N; i++) {
    v[i].id = i;
    int a;
    cin >> a;
    if (a == 0) {
      v[i].next = -1;
    } else {
      a--;
      v[i].next = a;
      v[a].pr = i;
    }
  }
  int e = 1;
  int k = 1;
  vector<int> cake;
  for (int i = 0; i < N; i++) {
    if (v[i].in) continue;
    st t = v[i];
    while (t.next != -1) {
      t = v[t.next];
    }
    int cnt = 1;
    bool is_e = 0;
    while (t.pr != -1) {
      v[t.id].in = 1;
      if (t.id == K) {
        is_e = 1;
        k = cnt;
      }
      t = v[t.pr];
      cnt++;
    }
    v[t.id].in = 1;
    if (t.id == K) {
      is_e = 1;
      k = cnt;
    }
    if (is_e)
      e = cnt;
    else
      cake.push_back(cnt);
  }
  sort(cake.begin(), cake.end());
  int Sum = 0;
  for (int i = 0; i < cake.size(); i++) {
    Sum += cake[i];
  }
  vector<vector<bool> > dp(cake.size(), vector<bool>(Sum + 1, 0));
  for (int i = 0; i < cake.size(); i++) {
    for (int j = 1; j < Sum + 1; j++) {
      if (j == cake[i]) {
        dp[i][j] = 1;
        continue;
      }
      if (j < cake[i]) {
        if (i != 0)
          dp[i][j] = dp[i - 1][j];
        else
          dp[i][j] = 0;
        continue;
      }
      if (i != 0) {
        dp[i][j] = (dp[i - 1][j] || dp[i - 1][j - cake[i]]);
        continue;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  cout << k << endl;
  for (int j = 1; j < Sum + 1; j++) {
    if (dp[cake.size() - 1][j]) cout << k + j << endl;
  }
  return 0;
}
