n = gets.chomp.to_i
as=[]
n.times do
  as << gets.chomp.split(" ").map(&:to_i);
end

count = 0 
(n * (n -1) / 2).times do
  p = []
  do_break = true
  as.each do |a|
    unless a.empty? 
      p << a[0]
      do_break = false
    else
      p << -1
    end
  end
  break if do_break

  shift_index = []
  p.each_with_index do |value, i| 
    shift_index << i if value != -1 && p[value-1] == (i+1) 
  end

  if shift_index.empty?
    puts "-1"
    exit 0
  end

  shift_index.each do |i|
    as[i].shift
  end

  count += 1
end

puts count 