#include <bits/stdc++.h>
using namespace std;
vector<long long> chain;
bool curr[2100], nxt[2100];
long long p[2100], c[2100];
int main() {
  ios_base::sync_with_stdio(0);
  long long n, x;
  cin >> n >> x;
  for (long long i = 1; i <= n; i++) cin >> p[i], c[p[i]] = i;
  long long endLen = 1;
  long long leading = x;
  while (p[leading]) {
    endLen++;
    leading = p[leading];
  }
  p[leading] = -1;
  for (long long i = 1; i <= n; i++)
    if (!p[i]) {
      long long l = 1, v = i;
      while (c[v] != 0) {
        v = c[v];
        l++;
      }
      chain.push_back(l);
    }
  curr[0] = true;
  for (long long l : chain) {
    for (long long i = 0; i <= 2 * n; i++)
      if (curr[i]) {
        nxt[i] = nxt[i + l] = true;
      }
    swap(curr, nxt);
  }
  for (long long i = 0; i < 2 * n; i++)
    if (curr[i]) cout << i + endLen << "\n";
  return 0;
}
