local mmi, mma = math.min, math.max
local n = io.read("*n")

local used = {}
local len = {}
local cost = {}
for i = 1, n do
  for j = 1, n do
    local idx = (i - 1) * n + j
    local z = mmi(i - 1, j - 1, n - i, n - j)
    len[idx] = z
    cost[idx] = 1
  end
end

local ret = 0
-- local t = {}
local tasks = {}

for i = 1, n * n do
  -- t[i] = io.read("*n")
  local pos = io.read("*n")
  ret = ret + len[pos]
  cost[pos] = 0

  local done = 0
  local taskpos = 1
  tasks[1] = pos

  local function walk(src, dst)
    if len[src] + cost[src] < len[dst] then
      len[dst] = len[src] + cost[src]
      taskpos = taskpos + 1
      tasks[taskpos] = dst
    end
  end

  while done < taskpos do
    done = done + 1
    local idx = tasks[done]
    if n < idx then walk(idx, idx - n) end
    if idx <= (n - 1) * n then walk(idx, idx + n) end
    if idx % n ~= 0 then walk(idx, idx + 1) end
    if idx % n ~= 1 then walk(idx, idx - 1) end
  end
end
print(ret)
