n=gets.to_i
a=[]
n.times{a<<gets.chomp.split(" ").map(&:to_i)}
w=(a.transpose)[0]
a=a.transpose
a.shift
a=a.transpose
w.unshift(0)
count=0
while w!=[0]+Array.new(n)
  stop=true
  fin=[0]
  n.times do |i|
    k = Marshal.load(Marshal.dump(w[i]))
    k,w[i]=0 if w[i]==nil
    if fin.include?(i) || fin.include?(k)
      next
    end
    if w[k]==i
      fin<<(i)
	  fin<<(k)
      w[i],w[k]=a[i-1][0],a[k-1][0]
      a[i-1].shift
      a[k-1].shift
      stop=false
    end
  end
  if stop
    count=-1
    break
  end
  count+=1
end
puts count