#include <bits/stdc++.h>
using namespace std;
bool vis[1010], used[1010];
int n, me, nq, Queue, Index, a[1010], inde[1010];
vector<int> e[1010];
vector<int> q[1010];
vector<int> ans;
void dfs(int u) {
  if (u == 0) return;
  q[nq].push_back(u);
  vis[u] = true;
  for (int i = 0; i < e[u].size(); i++)
    if (!vis[e[u][i]]) dfs(e[u][i]);
}
void build() {
  nq = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++)
    if (!inde[i]) {
      dfs(i);
      nq++;
    }
}
int main() {
  memset(inde, 0, sizeof(inde));
  cin >> n >> me;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    e[i].push_back(a[i]);
    inde[a[i]]++;
  }
  build();
  for (int i = 0; i < nq; i++) {
    bool ok = false;
    for (int j = 0; j < q[i].size(); j++)
      if (q[i][j] == me) {
        Queue = i;
        Index = j;
      }
    if (ok) break;
  }
  memset(used, false, sizeof(used));
  int tot = (1 << nq);
  for (int s = 0; s < tot; s++) {
    bool ok = true;
    int sum = 0;
    for (int i = 0; i < nq; i++)
      if (s & (1 << i)) {
        sum += q[i].size();
        if (i == Queue) {
          ok = false;
          break;
        }
      }
    if (!ok) continue;
    int res = sum + q[Queue].size() - Index;
    if (used[res]) continue;
    used[res] = true;
    ans.push_back(res);
  }
  sort(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
  return 0;
}
