### subroutines

def max_chain(r0, visited)
  max_cl = 1

  for r1 in $nbrs[r0]
    if ! visited[r1]
      visited[r1] = true
      cl = 1 + max_chain(r1, visited)
      visited[r1] = false

      max_cl = cl if max_cl < cl
    end
  end

  max_cl
end

### main

loop do
  n = gets.strip.to_i
  break if n == 0

  $rn = 0
  rids = {}
  redges = []

  n.times do
    a, b = gets.strip.split(' ').map{|s| s.to_i}
    if rids[a].nil?
      rids[a] = $rn
      $rn += 1
    end
    if rids[b].nil?
      rids[b] = $rn
      $rn += 1
    end
    redges << [rids[a], rids[b]]
  end
  #p rn
  #p redges

  $edges = $rn.times.map{[]}
  $nbrs = $rn.times.map{[]}

  for re in redges
    a, b = re
    $edges[a][b] = $edges[b][a] = 1
    $nbrs[a] << b
    $nbrs[b] << a
  end
  #p $edges
  #p $nbrs

  max_cl = 0

  for st in (0...$rn)
    visited = $rn.times.map{false}
    visited[st] = true

    cl = max_chain(st, visited)
    max_cl = cl if max_cl < cl
  end

  puts max_cl
end