#include <bits/stdc++.h>
using namespace std;
int n, m, k, fr[1005];
bool dp[1005], last[1005];
vector<int> num;
int main() {
  int x, cnt;
  bool flag;
  while (~scanf("%d %d", &n, &m)) {
    k = 1;
    memset(last, true, sizeof(last));
    num.clear();
    memset(dp, false, sizeof(dp));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &fr[i]);
      last[fr[i]] = false;
    }
    for (int i = 1; i <= n; i++)
      if (last[i]) {
        flag = (i == m);
        x = i;
        cnt = 1;
        while (fr[x]) {
          x = fr[x];
          cnt++;
          if (x == m) flag = true;
        }
        if (!flag)
          num.push_back(cnt);
        else
          k = cnt;
      }
    x = m;
    k = 1;
    while (fr[x]) {
      x = fr[x];
      k++;
    }
    memset(dp, false, sizeof(dp));
    dp[k] = true;
    for (__typeof(num.begin()) i = num.begin(); i != num.end(); i++)
      for (int j = n; j >= *i; j--)
        if (dp[j - *i]) dp[j] = true;
    for (int i = 1; i <= n; i++)
      if (dp[i]) printf("%d\n", i);
  }
  return 0;
}
