20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )

#define ALL(A) A.begin(), A.end()
#define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second

typedef long long LL;


typedef vector<int> VI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;



template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T> inline void CLR(T &A){A.clear();}

//}

/** Constant List .. **/ //{

const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};

const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int dxhorse[] = {-2 , -2 , -1 , -1 , 1 , 1 , 2 , 2};
const int dyhorse[] = {1 ,  -1 , 2  , -2 , 2 ,-2 , 1 ,-1};

const int MOD = 1000000007;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const LL INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0); //M_PI;

//}

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
//}
template<class T> inline T low_bit(T x) {return x & -x;}
/*****************************************************************/
int n , x;
const int N = 2e3;
int a[N] , next[N];
bool pre[N][N] , nex[N][N];
VI st[N];
int top;
PII p[N];
bool check(int me){
    if (p[me].se > x) return 0;
    if (p[me].se == x) return 1;
    int last = x - p[me].se;
//    if (p[me].fi == 0) return nex[1][last];
//    if (p[me].fi == top - 1) return pre[top - 2][last];
//    for (int i = 0 ; i <= last ; ++i)
//        if (pre[p[me].fi - 1][i] && nex[p[me].fi + 1][last - i]) return true;
//    return false;
    int line = p[me].fi;
    if (line == 0) return nex[1][last];
    if (line == top - 1) return pre[top - 2][last];
    for(int i = 0 ; i <= last ; ++i)
        if (pre[line - 1][i] && nex[line + 1][last - i]) return true;
    return false;
}
int main(){
    scanf("%d%d" , &n , &x);
    for (int i = 1 ; i <= n ; ++i) {
            scanf("%d" , &a[i]);
            next[a[i]] = i;
    }
    top = 0;
    for (int i = 1 ; i <= n ; ++i) if (a[i] == 0){
        int pos = 1;
        st[top].resize(0);
        int now = i;
        do{
            p[now] = MP(top , pos);
            pos++;
            st[top].PB(now);
            now = next[now];
        }while(now);
        ++top;
    }
//#define debug
#ifdef debug
    printf("%d\n" , top);
    for (int i = 0 ; i < top ; ++i){
        for (int j = 0 ; j < st[i].size() ; ++j)
            printf("%d " , st[i][j]);
        puts("");
    }
#endif // debug
    RST(pre);RST(nex);

    for (int i = 0 ; i < top ; ++i){
        pre[i][0] = 1;
        pre[i][st[i].size()] = 1;
        if (i){
            for (int j = 0 ; j <= n ; ++j) if (pre[i - 1][j]){
                pre[i][j] = pre[i][j + st[i].size()] = 1;
            }
        }
    }
    nex[top][0] = 1;
    for (int i = top - 1 ; i >= 0 ; --i){
        nex[i][0] = 1;
        for (int j = 0 ; j <= n ; ++j) if (nex[i + 1][j])
            nex[i][j] = nex[i][j + st[i].size()] = 1;
    }

    for (int i = 1 ; i <= n ; ++i) if (check(i)) printf("%d\n" , i);
}
