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 <bits/stdc++.h>
using namespace std;

int n, x, a[1002], i, j, f[1002];
list<list<int>> l;
list<int> l0(0);
int main() {
    cin>>n>>x;
    for(i = 0; i < n; i++) cin>>a[i];
    for(i = 0; i < n; i++)
        if (a[i]) {
            auto it_l = find_if(l.begin(), l.end(), [&](auto v){return v.back() == a[i];});
            auto it_r = find_if(l.begin(), l.end(), [&](auto v){return v.front() == (i+1);});
            if (it_l != l.end())
                if (it_r != l.end()) {
                    it_l->splice(it_l->end(), *it_r);
                    l.erase(it_r);
                } else it_l->push_back(i+1);
            else if (it_r != l.end()) it_r->push_front(a[i]);
                else l.push_back({a[i], i+1});
        }
    int xpos = 0;
    for (auto it = l.begin(); it != l.end(); it++) {
        for (auto u: *it) {
            if (u == x) {l0 = *it, it = l.erase(it); break;}
            xpos++;
        }
        if (l0.size()) break;
        xpos = 0;
    }
    if (!l0.size()) l0.push_back(x);
// cout<<"l0  ";
// for(auto u: l0) cout<<u<<' ';
// cout<<"  x "<<xpos<<endl;
    int r = 1;
    f[1] = 1;
    for (auto v: l) {
        for (i = r+1; i > 0; --i) {
            if (f[i]) f[i+v.size()] = 1;
        }
        // f[r+ l.size()+1] = 1; // bad
        r += v.size();
// cout<<v.size()<<" "<<r<<"  ";
// for (int i = 1; i <= r+1; i++) cout<<f[i];
// cout<<endl;
    }
    r = n - r - l0.size();
    for (i = 1; i <= n; i++) {
        if (f[i]) for (j = 0; j <= r; j++) f[i+j] = 1;
    }
    for (i = 1; i <= n; i++) {
        if (f[i]) cout<<i+xpos<<endl;
    }
    return 0;
}