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

local ret = 0
local done = {}
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  for i = 1, n do done[i] = false end
  for i = 1, n do
    if pos[i] < n and not done[i] then
      remain = true
      local nxt = t[i][pos[i]]
      if t[nxt][pos[nxt]] == i and not done[nxt] then
        proceed = true
        pos[i] = pos[i] + 1
        pos[nxt] = pos[nxt] + 1
        done[i] = true
        done[nxt] = true
      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)
