### subroutines

def cut_link(i, j)
  if $cbs[i][2] == j && $cbs[j][1] == i
    $cbs[i][2] = nil
    $cbs[j][1] = nil
  end
end

def make_link(i, j)
  if $cbs[i][2].nil? && $cbs[j][1].nil?
    $cbs[i][2] = j
    $cbs[j][1] = i
  end
end

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 above?(i, j)
  in_pile?(i, j, 2)
end

def under?(i, j)
  in_pile?(i, j, 1)
end

def take_off(k)
  k0 = k
  while ! $cbs[k0][2].nil?
    k1 = $cbs[k0][2]
    cut_link(k0, k1)
    k0 = k1
  end

  if ! $cbs[k][1].nil?
    k0 = $cbs[k][1]
    cut_link(k0, k)
  end
end

def put_on(i, j)
  while ! $cbs[j][2].nil?
    j = $cbs[j][2]
  end
  make_link(j, i)
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

    next if i == j

    if j < 0
      if ! $cbs[i][1].nil?
        take_off(i)
      end
    elsif above?(i, j)
      take_off(i)
      put_on(i, j)
    elsif under?(i, j)
      next
    else
      take_off(i)
      put_on(i, j)
    end
  end
  #p $cbs

  plens = []

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

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