local n = io.read("*n")
local edge = {}
local srccnt = {}
for i = 1, n * n do
  edge[i] = {}
  srccnt[i] = 0
end
local function getindex(a, b)
  if a < b then
    return (a - 1) * n + b
  else
    return (b - 1) * n + a
  end
end
for i = 1, n do
  local prvidx = getindex(i, io.read("*n"))
  for j = 2, n - 1 do
    local nxtidx = getindex(i, io.read("*n"))
    table.insert(edge[prvidx], nxtidx)
    srccnt[nxtidx] = srccnt[nxtidx] + 1
    prvidx = nxtidx
  end
end
local tasks = {}
local asked = {}
for i = 1, n * n do
  asked[i] = false
  if 0 < #edge[i] and srccnt[i] == 0 then
    table.insert(tasks, i)
  end
end
local day = 0
while 0 < #tasks do
  day = day + 1
  local ng = false
  local newtasks = {}
  for i = 1, #tasks do
    local src = tasks[i]
    for j = 1, #edge[src] do
      local dst = edge[src][j]
      if asked[dst] then
        day, ng = -1, true
        break
      end
      srccnt[dst] = srccnt[dst] - 1
      if srccnt[dst] == 0 then
        table.insert(newtasks, dst)
      end
    end
    if ng then break end
  end
  if ng then break end
  tasks = newtasks
end
if day == 0 then day = day - 1 end
print(day)
