#include <bits/stdc++.h>
using namespace std;
int N;
int p[1010];
bool last[1010];
vector<int> v;
bool dp[1010][1010];
int main(void) {
  int x, i, j;
  cin >> N >> x;
  x--;
  for ((i) = 0; (i) < (int)(N); (i)++) {
    cin >> p[i];
    p[i]--;
  }
  for ((i) = 0; (i) < (int)(N); (i)++) last[i] = true;
  for ((i) = 0; (i) < (int)(N); (i)++)
    if (p[i] != -1) last[p[i]] = false;
  for ((i) = 0; (i) < (int)(N); (i)++)
    if (last[i]) {
      int y = i;
      int cnt = 0;
      bool found = false;
      while (1) {
        cnt++;
        if (y == x) found = true;
        y = p[y];
        if (y == -1) break;
      }
      if (!found) v.push_back(cnt);
    }
  int d = 0;
  int tmp = x;
  while (1) {
    d++;
    tmp = p[tmp];
    if (tmp == -1) break;
  }
  dp[0][0] = true;
  int sz = v.size();
  for ((i) = 0; (i) < (int)(sz); (i)++)
    for ((j) = 0; (j) < (int)(N + 1); (j)++)
      if (dp[i][j]) dp[i + 1][j] = dp[i + 1][j + v[i]] = true;
  for ((i) = 0; (i) < (int)(N + 1); (i)++)
    if (dp[sz][i]) cout << d + i << endl;
  return 0;
}
