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

stack = es.keys.select{ |m| rs[m].keys.size == 0 }
sorted = []
while m = stack.pop do
  sorted << m
  (es[m].keys).each{ |n|
    rs[n].delete(m)
#    es[m].delete(n)
    if rs[n].keys.size == 0
      stack << n
    end
  }
end

if sorted.size != N*(N-1)/2
  p -1; exit
end

dp = {}
sorted.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
