N,*A = `dd`.split.map &:to_i
M = (1..N).map{ |i|
   (1..N-1).map{ |k|
     j = A[(i-1)*(N-1) + k-1]
     x,y = [i,j].sort
     x*N+y
   }
}
es = {}
tsort_arg = []
M.each{ |ms|
  (0...ms.size-1).each{ |j|
    a, b = ms[j], ms[j+1]
    es[a] ||= {}
    es[a][b] = 1
    tsort_arg += [a,b]
  }
}

tsorted = IO.popen("echo #{tsort_arg.join(" ")} | tsort", 2 => [:child, 1]){ |pipe| pipe.readlines.map{ |l| l.chomp } }
p tsorted
if tsorted.size != N*(N-1)/2 || tsorted.find{ |l| l.match(/tsort/) }
  puts -1; exit
end

matches = tsorted.map{ |s| s.to_i }
dp = {}
matches.each{ |m|
  dp[m] ||= 1
  if es[m]
    es[m].keys.each{ |n|
      dp[n] ||= 1
      dp[n] = [dp[n], dp[m]+1].max
    }
  end
}

p dp.values.max
