# coding: utf-8
N = gets().to_i
btls = []
comp = [true] + Array.new(N, false)
c_mem = (1..N).to_a
N.times do |i|
  btls << gets().chomp!.split(" ").map{|e| e.to_i}
end
btls.unshift([])

ans = 0
# 対戦が全部完了するまで繰り返し
while !comp.all?
  ans += 1
  next_mem = []
  matched = Array.new(N+1, false)
  
  c_mem.each do |i|
    # すでに全戦終了済みだったらスキップ
    next if comp[i] or comp[btls[i][0]]
    vs = btls[i][0]
    # 相手の対戦順序と同じかどうか？
#    if !matched[i] && !matched[vs] && i == btls[vs][0] 
    if !next_mem.include?(i) && !next_mem.include?(vs) && i == btls[vs][0] 
      btls[i].shift
      btls[vs].shift
      next_mem << i << vs
      matched[i] = true
      matched[vs] = true
      comp[i] = true if btls[i].empty?
      comp[vs] = true if btls[vs].empty?
    end
  end
  if next_mem.empty?
    puts -1
    exit 0
  end
  c_mem = next_mem
end

puts ans

