#include <bits/stdc++.h>
using namespace std;
static const int INF = 500000000;
template <class T>
void debug(T a, T b) {
  for (; a != b; ++a) cerr << *a << ' ';
  cerr << endl;
}
int n;
int nxt[1005], lat[1005];
vector<int> group;
int vis[1005];
int able[1005];
int main() {
  int x;
  cin >> n >> x;
  --x;
  memset(nxt, -1, sizeof(nxt));
  memset(lat, -1, sizeof(lat));
  for (int i = 0; i < n; ++i) {
    cin >> nxt[i];
    --nxt[i];
    if (nxt[i] != -1) lat[nxt[i]] = i;
  }
  for (int i = 0; i < n; ++i)
    if (!vis[i] && lat[i] == -1) {
      int num = 1;
      int cur = i;
      int isX = 0;
      if (i == x) isX = 1;
      vis[i] = 1;
      while (nxt[cur] != -1) {
        cur = nxt[cur];
        vis[cur] = 1;
        if (cur == x) isX = 1;
        ++num;
      }
      if (!isX) group.push_back(num);
    }
  int add = 0;
  int cur = x;
  while (nxt[cur] != -1) {
    cur = nxt[cur];
    ++add;
  }
  able[0] = 1;
  for (int i = 0; i < group.size(); ++i)
    for (int j = n; j >= 0; --j)
      if (able[j]) {
        able[j + group[i]] = 1;
      }
  for (int i = 0; i < n + 1; ++i)
    if (able[i]) {
      printf("%d\n", i + add + 1);
    }
}
