N = gets.to_i
A = N.times.map{ gets.split.map{|_| _.to_i-1 } }

day = 0
loop {
	day += 1
	remain = A.sum{|_| _.size }
	match_waiting = []
	N.times{|i|
		target = A[i].first
		next unless target
		if i < target
			match_waiting << [i, target] 
		else
			j = match_waiting.find_index([target, i])
			if j
				match_waiting.delete_at j
				A[target].shift
				A[i].shift
			end
		end
	}
	break if A.sum{|_| _.size } == remain
}
if A.sum{|_| _.size } == 0
	p day-1
else
	puts '-1'
end
