N = gets.chomp.to_i
a = {}
N.times do |i|
  a[i+1] = gets.chomp.split(" ").map(&:to_i)
end

days = 0
cnt = 0
require "set"

loop do
  days += 1
  vs = Set.new
  (1..N).each do |i|
    next if a[i].empty? || vs.include?(i)
    i2 = a[i].first
    next if vs.include?(i2)
    if i == a[i2].first
      vs << a[i].shift
      vs << a[i2].shift
      cnt += 1
    end
  end
  if cnt == N * (N - 1) / 2
    puts days
    exit 0
  end
  if vs.empty?
    puts -1
    exit 0
  end
end
