### subroutines

def in_pile?(i, j, d)
  k = i
  while ! $cbs[k][d].nil?
    k = $cbs[k][d]
    return true if k == j
  end
  false
end

def off_overs(k)
  while ! $cbs[k][2].nil?
    k0 = $cbs[k][2]
    $cbs[k][2] = $cbs[k0][1] = nil
    k = k0
  end
end

def put_on(i, j)
  while ! $cbs[j][2].nil?
    j = $cbs[j][2]
  end
  $cbs[j][2] = i
  $cbs[i][1] = j
end

### main

loop do
  m = gets.to_i
  break if m == 0

  $cbs = (0...m).map{|i| [i, nil, nil]}

  loop do
    i, j = gets.split.map{|s| s.to_i - 1}
    break if i == -1 && j == -1

    if j < 0
      if ! $cbs[i][1].nil?
        off_overs(i)
        $cbs[$cbs[i][1]][2] = nil
        $cbs[i][1] = nil
      end
    elsif in_pile?(i, j, 2)
      off_overs(i)
      put_on(i, j)
    elsif in_pile?(i, j, 1)
      # do nothing
    elsif i != j
      off_overs(i)
      put_on(i, j)
    end
  end
  #p $cbs

  plens = []

  for i in (0...m)
    if $cbs[i][1].nil?
      len = 1
      i0 = i
      while ! $cbs[i0][2].nil?
        i0 = $cbs[i0][2]
        len += 1
      end
      plens << len
    end
  end
  #p plens

  plens.sort.each{|len| puts len}
  puts 'end'
end