n = gets.chomp.to_i

a = {}
1.upto(n) { |i|
  a[i] = gets.chomp.split(/\s+/).map { |e| e.to_i }
}

day = 0
loop {
  battles = []

  day += 1
  sorted_a = a.to_a.sort_by { |e| e[1].size }.reverse
  sorted_a.each { |e|
    break if e[1].size == 0

    vs = e[1][0]
    if a[vs][0] == e[0] && !battles.include?(vs) && !battles.include?(e[0])
      battles << vs
      battles << e[0]
      a[vs].shift
      e[1].shift
    end
  }

  break if battles.empty?
}

if a.to_a.any? { |e| !e[1].empty? }
  puts -1
else
  puts day - 1
end

