N = gets.to_i
A = (0...N).map{ gets.split.map &:to_i }
es = (0..N**2).map{ [] }
rs = (0..N**2).map{ 0 }
M = []
mns = {}
A.each_with_index{ |ai,i|
  M << ai.map{ |j|
    x,y = i,j-1
    x,y = y,x if x > y
    m = x*N+y
    mns[m] = 1
    m
  }
}
M.each{ |ms|
  a,b = nil,ms[0]
  ms[1..-1].each{ |m|
    a, b = b,m
    es[a] << b
    rs[b] += 1
  }
}

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

#p M, sorted

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

dp = Array.new((N+1)**2, 1)
sorted.each{ |m|
  d = dp[m] + 1
  es[m].each{ |n|
    dp[n] = d if d > dp[n]
  }
}

p dp.max
