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

import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Main {

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    String next() throws IOException {
        while( in==null || !in.hasMoreTokens() )
            in = new StringTokenizer( br.readLine() );
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

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





    void run() throws IOException {
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        Reader reader = oj ? new InputStreamReader(System.in) :
                new FileReader("input.txt");
        br = new BufferedReader(reader);
        out = new PrintWriter(
                oj ? new OutputStreamWriter(System.out) :
                        new FileWriter("output.txt"));
        solve();
        //deb();
        out.flush();
    }

    int n, x;
    int[] a, cntIn;
    int posX = -1;
    Vector<Integer> val = new Vector<Integer>();
    boolean[] can;

    void solve() throws IOException {
        n = nextInt();
        x = nextInt()-1;
        a     = new int[n];
        cntIn = new int[n];
        for( int v = 0; v < n; ++v ){
            a[v] = nextInt()-1;
            if( a[v]!=-1 ) ++cntIn[a[v]];
        }
        for( int v = 0; v < n; ++v ){
            if( cntIn[v]==0 ){
                Vector<Integer> path = new Vector<Integer>();
                for( int k = v; k != -1; k = a[k] )
                    path.add(k);
                Collections.reverse(path);
                boolean find = false;
                for( int i = 0; i < path.size(); ++i ){
                    if( path.get(i).equals(x) ){
                        posX = i+1;
                        find = true;
                    }
                }
                if( !find )
                    val.add( path.size() );
            }
        }
        if( posX == -1 ) posX = 1;
//        out.println(posX);

        can = new boolean[n+1];
        can[0] = true;
        for( int add : val ){
            for( int i = n; 0 <= i-add; --i )
                if( can[i-add] )
                    can[i] = true;
        }
        boolean[] ans = new boolean[n+1];
        for( int i = n; 0 <= i-posX; --i )
            if( can[i-posX] )
                ans[i] = true;
        for( int i = 1; i <= n; ++i )
            if( ans[i] )
                out.println(i);
        out.println();
    }


}
