def toid(i, j)
  # 頂点番号を返す
  i, j = j, i if i > j
  return $id[[i, j].inspect]
end

def dfs(v)
  if $visited[v]
    return -1 if !$calc[v]
    return $dp[v]
  end
  $visited[v] = true
  $dp[v] = 1
  $to[v].each do |u|
    res = dfs(u)
    return -1 if res == -1
    $dp[v] = [$dp[v], res + 1].max
  end
  $calc[v] = true
  return $dp[v]
end

n = gets.to_i
$visited = Array.new(n)
$calc = Array.new(n)
$dp = Array.new(n, 0)
$a = n.times.map { gets.split.map { |x| x.to_i - 1 } }
v = 0
$id = Hash.new(0)
$to = Array.new(n * (n - 1)) { [] }
n.times do |i|
  n.times do |j|
    if i < j
      $id[[i, j].inspect] = v + 1
      v += 1
    end
  end
end

n.times do |i|
  (n - 1).times do |j|
    $a[i][j] = toid(i, $a[i][j])
  end
  (n - 2).times do |j|
    $to[$a[i][j]] << $a[i][j + 1]
  end
end
ans = 0
v.times do |i|
  res = dfs(i)
  if res == -1
    p -1
    exit
  end
  ans = [ans, res].max
end
p ans
