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.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {
	
	public void solve() throws Exception {
		int n = sc.nextInt(), x = sc.nextInt() - 1;
		
		int[] prev = new int[n];
		int[] next = new int[n];
		
		Arrays.fill(prev, -1);
		Arrays.fill(next, -1);
		
		for (int i = 0; i < n; i++) {
			prev[i] = sc.nextInt() - 1;
			if (prev[i] != -1) {
				next[prev[i]] = i;
			}
		}
		
		ArrayList<Integer> groups = new ArrayList<Integer>();
		int cbgroup = -1, cbpos = -1;
		
		for (int i = 0; i < n; i++) {
			if (prev[i] == -1) {
				int cur = i;
				int curpos = 0;
				while (cur != -1) {
					if (cur == x) {
						cbgroup = groups.size();
						cbpos = curpos;
					}
					cur = next[cur];
					curpos++;
				}
				groups.add(curpos);
			}
		}
		
		int[] w = new int[groups.size() - 1];
		
		for (int i = 0, pos = 0; i < groups.size(); i++) {
			if (i != cbgroup) {
				w[pos++] = groups.get(i);
			}
		}
		
		boolean[] knapsack = new boolean[n + 1];
		knapsack[0] = true;
		for (int i = 0; i < w.length; i++) {
			for (int j = knapsack.length - 1; j >= 0; j--) {
				if (knapsack[j] && j + w[i] < knapsack.length) {
					knapsack[j + w[i]] = true;
				}
			}
		}
		
		for (int i = 0; i < knapsack.length; i++) {
			if (knapsack[i]) {
				out.println(i + cbpos + 1);
			}
		}
	}

	static Throwable uncaught;

	BufferedReader in;
	FastScanner sc;
	PrintWriter out;

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable uncaught) {
			Solution.uncaught = uncaught;
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) throws Throwable {
		Thread thread = new Thread(null, new Solution(), "", (1 << 26));
		thread.start();
		thread.join();
		if (Solution.uncaught != null) {
			throw Solution.uncaught;
		}
	}

}

class FastScanner {

	BufferedReader in;
	StringTokenizer st;

	public FastScanner(BufferedReader in) {
		this.in = in;
	}

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

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

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

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

}