local n = io.read("*n")
local t = {}
local pos = {}
local waitlist = {}
for i = 1, n do
  pos[i] = 1
  t[i] = {}
  for j = 1, n - 1 do
    t[i][j] = io.read("*n")
  end
end

for i = 1, n do
  if not waitlist[t[i][1]] then
    waitlist[t[i][1]] = {}
  end
  waitlist[t[i][1]][i] = true
end

local ret = 0
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  local done = {}
  for i = 1, n do done[i] = false end
  for i, dstlist in pairs(waitlist) do
    if pos[i] < n and not done[i] then
      for nxt, _unused in pairs(dstlist) do
        remain = true
        local nxt = t[i][pos[i]]
        if waitlist[nxt] and waitlist[nxt][i] and waitlist[i][nxt] and not done[nxt] then
          for x, y in pairs(waitlist[nxt]) do print(x) end
          proceed = true
          waitlist[nxt][i] = nil
          waitlist[i][nxt] = nil
          pos[i] = pos[i] + 1
          pos[nxt] = pos[nxt] + 1
          done[i] = true
          done[nxt] = true
          if pos[i] < n then
            if not waitlist[t[i][pos[i]]] then waitlist[t[i][pos[i]]] = {} end
            waitlist[t[i][pos[i]]][i] = true
          end
          if pos[nxt] < n then
            if not waitlist[t[nxt][pos[nxt]]] then waitlist[t[nxt][pos[nxt]]] = {} end
            waitlist[t[nxt][pos[nxt]]][nxt] = true
          end
          break
        end
      end
    end
  end
  if not remain then
    if not proceed then
      ret = ret - 1
    end
    break
  end
  if not proceed then
    ret = -1
    break
  end
end
print(ret)
