n = gets.to_i

as = [nil]
index = [nil] + Array.new(n, 0)

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

max = n * (n - 1) / 2

count = 0
deleted_count = 0

while true
	temp = {}
	is_delete = false

	(1...as.size).each{ |i|
		if as[i].size > 0 then
			target = as[i][0]
			if as[target].size > 0 && i == as[target][0] then
				temp[i] = true

				deleted_count += 1
				is_delete = true 
			end
		end
	}

	temp.each{ |k,v|
		as[k].shift
	}
	
	if ! is_delete then
		if deleted_count >= max then
			puts count
			exit
		else
			puts -1
			exit
		end
	end
	
	count += 1
end
