require "set"

n = gets.to_s.to_i
a = Array.new(n){ gets.to_s.split.map{|t| t.to_i - 1 } }

next_battles = Set[]
n.times do |i|
  j = a[i][0]
  next_battles << [i, j] if i < j && i == a[j][0]
end

day = 0
battle = 0
while next_battles.any?
  day += 1
  current_battles, next_battles = next_battles, Set[]
  current_battles.each do |(x, y)|
    
    battle += 1
    
    a[x].shift
    a[y].shift
    
    i = x
    if a[i].any?
      j = a[i][0]
      if i == a[j][0]
        i, j = j, i if i > j
        next_battles << [i, j]
      end
    end
    
    i = y
    if a[i].any?
      j = a[i][0]
      if i == a[j][0]
        i, j = j, i if i > j
        next_battles << [i, j]
      end
    end
  end
end

puts battle == n * (n-1) / 2 ? day : -1