until (m = gets.to_i).zero?
  pile = (1..m).map{|i| [i]}
  until (i, j = gets.split.map(&:to_i)).all?(&:zero?)
    next if i == j
    target = pile.find{|p| p.include?(i)}
    next if target.include?(j) && target.index(i) > target.index(j)
    pile << [target.pop] until target.last == i
    target.pop
    pile.delete(target) if target.empty?
    j.zero? ? pile << [i] : pile.find{|p| p.include?(j)} << i
  end
  puts pile.map(&:size).sort, "end"
end