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.util.*;
import java.io.*;

public class Main implements Runnable {
		
	public void solve() throws IOException {
		int N = nextInt();
                int me = nextInt() - 1;
                
                int[] a = new int[N];
                for(int i = 0; i < N; i++) a[i] = nextInt() - 1;
                
                boolean[] tail = new boolean[N];
                Arrays.fill(tail, true);
                for(int i = 0; i < N; i++) if(a[i] >= 0)  tail[a[i]] = false;
                
                boolean[] possible = new boolean[N + 1];
                possible[0] = true;
                
                for(int i = 0; i < N; i++)
                        if(tail[i]){
                                int at = i;
                                int len = 0;
                                boolean myq = false;
                                while(at >= 0){
                                        if(at == me) myq = true;
                                        len++;
                                        at = a[at];
                                }
                                
                                if(!myq){
                                        for(int k = N - len; k >= 0; k--){
                                                if(possible[k]){
                                                        possible[k + len] = true;
                                                }
                                        }
                                }
                                
                        }
                
                
                int myOffsetinq = 0;
                while(me >= 0){
                        myOffsetinq ++;
                        me = a[me];
                }
                
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < N; i++){
                        if(possible[i]){
                                sb.append(i +  myOffsetinq);
                                sb.append("\n");
                        }
                }
                
                System.out.print(sb.toString());
	}

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

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

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

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

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}