#include <bits/stdc++.h>
using namespace std;
int adj[1000];
int n;
int x;
bool islast[1000];
int xpos;
int rec(int i) {
  if (i == x) {
    if (adj[i] == -1)
      xpos = 1;
    else
      xpos = rec(adj[i]) + 1;
    return -(1 << 29);
  }
  if (adj[i] < 0) return 1;
  return rec(adj[i]) + 1;
}
bool a[1001];
bool b[1001];
int main() {
  memset(islast, true, 1000 * sizeof(bool));
  memset(adj, -1, 1000 * sizeof(int));
  cin >> n >> x;
  x--;
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    if (a >= 0) adj[i] = a;
    if (a >= 0) islast[a] = false;
  }
  for (int i = 0; i < n; i++) {
    if (!islast[i]) continue;
    int t = rec(i);
    if (t >= 0) vec.push_back(t);
  }
  memset(a, false, 1001 * sizeof(bool));
  a[0] = true;
  for (auto it = vec.begin(); it != vec.end(); it++) {
    int num = *it;
    memcpy(b, a, 1001 * sizeof(bool));
    for (int j = 0; j < 1001; j++)
      if (a[j]) b[j + num] = true;
    memcpy(a, b, 1001 * sizeof(bool));
  }
  for (int i = 0; i < 1001; i++)
    if (a[i]) cout << i + xpos << "\n";
}
