#include <bits/stdc++.h>
using namespace std;
int l[1211], r[1211];
bool vis[1211][2211];
vector<int> A;
int main() {
  ios::sync_with_stdio(false);
  int n, pos;
  cin >> n >> pos;
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    l[i] = x;
    r[x] = i;
  }
  int ilen = -1;
  for (int i = 1; i <= n; i++) {
    if (!l[i]) {
      int len = 1;
      bool flag = true;
      if (i == pos) {
        ilen = 1;
        continue;
      }
      for (int j = r[i]; j; j = r[j]) {
        len++;
        if (j == pos) {
          flag = false;
          ilen = len;
        }
      }
      if (flag) A.push_back(len);
    }
  }
  memset(vis, 0, sizeof(vis));
  vis[0][0] = true;
  int sv = A.size();
  for (int i = 0; i < sv; i++)
    for (int j = 0; j < n; j++) {
      vis[i + 1][j] |= vis[i][j];
      vis[i + 1][j + A[i]] |= vis[i][j];
    }
  for (int i = 0; i < n; i++)
    if (vis[sv][i]) cout << i + ilen << '\n';
  return 0;
}
