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

Remain = lambda{ A.map(&:size).inject(&:+) }
before = Remain.call
day = 0
loop {
	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
	}
	after = Remain.call
	break if before == after

	before = after
	day += 1
}
p (Remain.call == 0 ? day : -1)
