#include <bits/stdc++.h>
using namespace std;
bool g[2][2000], f[2000];
int n, id, a[2000];
void init() {
  int now, i, next, p, j, temp, num;
  bool check;
  memset(g, false, sizeof(g));
  g[0][0] = true;
  now = 0;
  next = 1;
  for (i = 1; i <= n; i++)
    if (f[i]) {
      p = i;
      check = true;
      num = 0;
      while (p != 0) {
        if (p == id) {
          check = false;
          break;
        }
        p = a[p];
        num++;
      }
      if (!check) continue;
      memset(g[next], false, sizeof(g[next]));
      for (j = 0; j <= n; j++)
        if (g[now][j]) g[next][j + num] = true;
      for (j = 0; j <= n; j++)
        if (g[now][j]) g[next][j] = true;
      temp = now;
      now = next;
      next = temp;
    }
  p = id;
  num = 0;
  while (p != 0) {
    p = a[p];
    num++;
  }
  for (j = 0; j <= n; j++)
    if (g[now][j]) cout << num + j << endl;
}
int main() {
  int i;
  cin >> n >> id;
  for (i = 1; i <= n; i++) cin >> a[i];
  memset(f, true, sizeof(f));
  for (i = 1; i <= n; i++)
    if (a[i] != 0) f[a[i]] = false;
  init();
  return 0;
}
