class Himo
  attr_accessor :id, :es
  def initialize(id, es)
    @id = id
    @es = es
  end
  def the_other_side(e)
    if e == @es[0]
      @es[1]
    elsif e == @es[1]
      @es[0]
    else
      puts "error"
    end
  end
  def to_s
    "#{@id}, [#{@es[0]},#{@es[1]}]"
  end
end

def rec(m, himos, len)
  if himos[m].nil? || himos[m].empty?
    return len
  else
    max_len = 0
      himos[m].each do |himo|
        l = himo.the_other_side(m)
        himos_copy = himos.dup
        puts "error2" unless himos_copy.delete(m)
        new_len = rec(l, himos_copy, len+1)
        max_len = new_len > max_len ? new_len : max_len
      end
    return max_len
  end

end

while (n = gets.to_i) != 0 do
  himos = {}
  es_nums = []
  0.upto(n-1) do |i|
    es = gets.split(" ").map(&:to_i)
    es.each do |num|
        himos[num] = [] if himos[num].nil?
        himos[num] << Himo.new(i, es)
    end
  end
  lens = []
  himos.each_key {|m| lens << rec(m, himos, 0)}
  puts lens.max
end