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

day = 0
loop {
	match_waiting = {}
	matched = false

	N.times{|i|
		target = A[i].last
		next unless target
		if i < target
			match_waiting[i*1000 + target] = true
		elsif match_waiting[target*1000 + i]
			A[target].pop
			A[i].pop
			matched = true
		end
	}

	break unless matched
	day += 1
}
p (A.any?{|_| !_.empty? } ? -1 : day)
