until(a=(1..(m=gets.to_i)).map{|i|[i]})==[]
until(i,j=gets.split.map(&:to_i))==[0,0]
next if(t=a.find{|p|p.include?(i)}).include?(j)&&t.index(i)>=t.index(j)
a<<[t.pop]until t.last==i
t.pop
j>0?a.find{|p|p.include?(j)}<<i :a<<[i]end
puts a.map(&:size).sort.select{|i|i>0},:end
end