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

def put(): return map(int, input().split())

n,x = put()
a = list(put())

parent = list(range(n+1))
for i in range(n):
    if a[i]!=0:
        parent[a[i]] = i+1

cnt = []
z = 0
#print(parent)
for i in range(n):
    if a[i]==0:
        j = i+1
        c = 1
        found = False
        if j==x:
            z=c
            found = True
        while j != parent[j]:
            j = parent[j]
            c+=1
            if j==x:
                z=c
                found = True
        if not found:
            cnt.append(c)

#print(cnt,z)
n,m = len(cnt)+1, sum(cnt)+1
dp = [[0]*(m+1) for i in range(n+1)]
dp[0][0]=1
s = set()
s.add(0)
for i in range(1,n):
    for j in range(m):
        if  j==0 or dp[i-1][j]==1 or (j-cnt[i-1]>=0 and dp[i-1][j-cnt[i-1]]==1) :
            dp[i][j] = 1
            s.add(j)
l = []
for i in s:
    l.append(i+z)

l.sort()
print(*l)
