#include <bits/stdc++.h>
using namespace std;
vector<int> num, sum;
int NUM[1005], Ind[1005];
int Set[1005];
int Top_sort(int n, int m) {
  queue<int> q;
  int i, j, ret;
  for (i = 1; i <= n; i++)
    if (Ind[i] == 0) q.push(i);
  while (!q.empty()) {
    i = q.front(), q.pop();
    int now = 0, tab = 0;
    for (j = i; true; j = NUM[j]) {
      if (j == m) tab = 1, now = 0;
      if (now++, NUM[j] == 0) break;
    }
    if (tab == 0)
      num.push_back(now);
    else
      ret = now;
  }
  return ret;
}
int main() {
  int n, m, i, j;
  for (i = 1, scanf("%d%d", &n, &m); i <= n; i++)
    if (scanf("%d", &NUM[i]), NUM[i]) Ind[NUM[i]]++;
  int ans = Top_sort(n, m), end = 0;
  Set[0] = 1;
  for (i = 0; i < num.size(); i++)
    for (j = end; j >= 0; j--)
      if (Set[j]) Set[j + num[i]] = 1, end = max(end, j + num[i]);
  for (j = 0; j <= end; j++)
    if (Set[j]) printf("%d\n", ans + j);
  return 0;
}
