n = gets.to_i

as = [nil]

n.times{
	as << gets.split.map(&:to_i)
}

count = 0

while true
	temp = {}
	(1...as.size).each{ |i|
		if as[i].size != 0 then
			temp[i] = as[i][0]
		end
	}
	
	keys = temp.keys
	if keys.size == 0 then
		puts count
		exit
	end

	is_delete = false

	keys.each{ |k|
		if temp[k] != nil && temp[temp[k]] == k then
			as[k].shift
			as[temp[k]].shift
			temp[k] = nil
			temp[temp[k]] = nil
			is_delete = true 
		end
	}
	
	if ! is_delete then
		puts -1
		exit
	end
	
	count += 1
end
