def gs() STDIN.gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end
 
def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end


class Node
	attr_accessor :value, :next
	def initialize(value, next_node)
		@value = value
		@next = next_node
	end
	def self.create(value)
		self.new(value,nil)
	end
end

class LinkedList
	attr_accessor :head, :tail
	def initialize(arr)
		arr = [arr] if arr.class!=Array
		now = Node.create(arr.shift)
		@head = @tail = now
		arr.each do |a|
			self.add(Node.create(a))
		end
	end
	
	def add(node)
		@tail.next = node
		@tail = node
	end
	
	def print_all
		n = @head
		while n!=nil do
			print("#{n.value} #{'-> ' if n!=@tail}")
			n = n.next
		end
	end
end

n = gi
ass = Array.new(n) do
	LinkedList.new(gets.split().map{|m| m.to_i-1}).head
end

answer = 0
flag = false
while true do
	flag = true
	play = []
	n.times do |i|
		next if ass[i]==nil
		flag = false
		
		#p ass.map{|m|m.nil? ? nil : m.value}
		target = ass[i].value
		if ass[target].value==i
			play << i
		end
	end
	#p play
	break if play.empty?
	play.each{|i| ass[i]= ass[i].next }
	answer += 1
end

puts flag ? answer : -1