#include <bits/stdc++.h>
using namespace std;
int a[1024];
int b[1024];
int c[1024];
int d[1024];
int main() {
  int n, x, y;
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i]) b[a[i]] = i;
  }
  int k = 0;
  for (int i = 1; i <= n; i++)
    if (!a[i]) {
      for (int j = i; j; j = b[j], c[k]++)
        if (j == x) {
          y = c[k] + 1;
          c[k] = 0;
          break;
        }
      if (c[k]) k++;
    }
  d[0] = 1;
  for (int i = 0; i < k; i++)
    for (int j = n - c[i]; j >= 0; j--) d[j + c[i]] |= d[j];
  for (int i = 0; i < n; i++)
    if (d[i]) cout << i + y << endl;
  return 0;
}
