def gs() STDIN.gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end
 
def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end


n = gi
indexs = Array.new(n){0}
ass = Array.new(n) do
	gets.split().map{|m| m.to_i-1}
end

answer = 0
flag = false
until flag do
	flag = true
	play = []
	n.times do |i|
		next if indexs[i]==(n-1)
		flag = false
		
		target = ass[i][indexs[i]]
		if ass[target][indexs[target]]==i
			play << i
		end
	end
	if play.empty?
		puts -1
		exit
	end
	play.each{|i| indexs[i]+=1 }
	answer += 1
end

puts answer-1