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].first
  return if op.nil? || i>op
  opop=$as[op].first
  return if i!=opop
  $queue << [i,op]
end

day=0

N.times do |i|
  push_queue(i)
end

until $queue.empty?
  day+=1
  queue_cur=$queue.clone
  $queue=Array.new
  until queue_cur.empty?
    a,b=queue_cur.shift
    $as[a].shift
    $as[b].shift
    push_queue(a)
    push_queue(b)
  end
end
puts $as.any?{|es|es.empty?} ? day : -1
