#include <bits/stdc++.h>
using namespace std;
const int M = 1e6, N = 1e3 + 20, MOD = 1e9 + 7, OO = 1e16;
int m, n, k, x, y, vis[N], dp[N][N], len, bla, ans, a[N], ok, cnt = 1;
pair<int, int> p;
vector<int> v;
set<long long> ss;
string s, t;
map<int, int> mp;
vector<vector<int> > G;
int dsu(int x) {
  if (x == a[x]) return cnt;
  cnt++;
  dsu(a[x]);
}
int Dp(int i, int ans) {
  ss.insert(ans);
  if (i == v.size()) return 1;
  int &r = dp[i][ans];
  if (r != -1) return r;
  r = Dp(i + 1, ans);
  r += Dp(i + 1, ans + v[i]);
  return r;
}
int DFS(int i) {
  vis[i] = 1;
  if (i == m) ok = 1;
  for (int j = 0; j < G[i].size(); j++) {
    if (!vis[G[i][j]]) len = 1 + DFS(G[i][j]);
  }
  return len;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  cin >> n >> m;
  G.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i])
      G[i].push_back(a[i]), G[a[i]].push_back(i);
    else
      a[i] = i;
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      ok = 0;
      len = 1, x = DFS(i);
      if (!ok) v.push_back(x);
    }
  bla = dsu(m);
  memset(dp, -1, sizeof dp);
  Dp(0, bla);
  for (auto l : ss) cout << l << endl;
  return 0;
}
