N=gets.to_i
    $as=N.times.map{gets.split.map{|e|e.to_i-1}}
    $queue=Array.new
    def push_queue(i)
      op=$as[i][0]
      return if op.nil?
      opop=$as[op][0]
      return if i!=opop
      $queue << [i,op]
      return
    end
     
    day=0
     
    N.times do |i|
      push_queue(i)
    end
     
    until $queue.empty?
      day+=1
      queue_cur=$queue
      $queue=Array.new
      until queue_cur.empty?
        a,b=queue_cur.shift
        if $as[a][0]==b && $as[b][0]==a
          $as[a].shift
          $as[b].shift
          push_queue(a)
          push_queue(b)
        end
      end
    end
    puts $as.all?{|es|es.empty?} ? day : -1
