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

//package abbyycup2013;
import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class ProblemB {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    ProblemB() throws IOException {
//        reader = new BufferedReader(new FileReader("input.txt"));
//        writer = new PrintWriter(new FileWriter("output.txt"));
    }

    int[] readInts() throws IOException {
        String[] strings = reader.readLine().split(" ");
        int[] ints = new int[strings.length];
        for(int i = 0; i < ints.length; i++) {
            ints[i] = Integer.parseInt(strings[i]);
        }
        return ints;
    }

    int[] tt = null;
    int tx = 0;
    int readInt() throws IOException {
        if(tt == null || tx >= tt.length) {
            tt = readInts();
            tx = 0;
        }
        return tt[tx++];
    }

    void solve() throws IOException {
        int n = readInt();
        int x = readInt();
        int[] prev = readInts();
        int[] next = new int[n];
        for(int i = 1; i <= n; i++) {
            int pr = prev[i - 1];
            if(pr != 0) next[pr - 1] = i;
        }
        List<Integer> groups = new ArrayList<Integer>();
        for(int i = 1; i <= n; i++) {
            if(next[i - 1] == 0) {
                int q = 0;
                boolean beaverGroup = false;
                for(int u = i; u > 0; u = prev[u - 1]) {
                    if(u == x) beaverGroup = true;
                    q++;
                }
                if(!beaverGroup) groups.add(q);
            }
        }
        int bix = 0;
        for(int u = x; u > 0; u = prev[u - 1]) {
            bix++;
        }
        boolean[] used = new boolean[n];
        used[0] = true;
        for(int g: groups) {
            for(int i = n - 1; i >= 0; i--) {
                if(used[i]) used[i + g] = true;
            }
        }
        for(int i = 0; i < n; i++) {
            if(used[i]) {
                writer.println(i + bix);
            }
        }
        writer.flush();
    }

    void multiSolve() throws IOException {
        int n = readInts()[0];
        for(int i = 0; i < n; i++) {
            solve();
        }
        writer.flush();
    }

    public static void main(String[] args) throws IOException{
        new ProblemB().solve();
    }
}
