#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int count = 0;
  int arr[1031] = {}, pos[1031] = {}, num[1031] = {};
  for (int i = 0; i < n; i++) {
    cin >> pos[i];
    arr[pos[i]] = i + 1;
  }
  int dif = 0, po = 1, d = 0;
  for (int i = 0; i < n; i++) {
    int f = 0;
    if (pos[i] == 0) {
      int t = 0, cur = i + 1, j = 1;
      if (k == cur) po = j, f = 1;
      while (arr[cur] != 0) {
        t++, j++;
        cur = arr[cur];
        if (f == 0 && cur == k) po = j, f = 1;
      }
      t++;
      if (f == 0) num[count++] = t;
    }
  }
  bool subset[n + 1][count + 1];
  for (int i = 0; i <= count; i++) subset[0][i] = true;
  for (int i = 1; i <= n; i++) subset[i][0] = false;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= count; j++) {
      subset[i][j] = subset[i][j - 1];
      if (i >= num[j - 1])
        subset[i][j] = subset[i][j] || subset[i - num[j - 1]][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i - po >= 0 && subset[i - po][count]) cout << i << endl;
  }
}
