"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(){return +cin[cid++];}
function nextstr(){return cin[cid++];}
function nextbig(){return BigInt(cin[cid++]);}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

var myOut = main();
if(myOut !== undefined)console.log(myOut);

function main(){
  var n = next(), nn = n*n;
  var p = nexts(nn);
  var dist = Array(n).fill().map((a,y)=>Array(n).fill().map((b,x)=>Math.min(y,x,n-x-1,n-y-1)));
  var grid = xArray(1,n,n);
  var ans = 0;
  for(var i = 0; i < nn; i++){
    var r = p[i]-1, y = r / n | 0, x = r % n;
    ans += dist[y][x];
    grid[y][x] = 0;
    var q = [y,x];
    while(q.length){
      x = q.pop(), y = q.pop();
      var cost = dist[y][x];
      if(x && dist[y][x-1] - grid[y][x] > cost)dist[y][x-1] = cost + grid[y][x], q.push(y,x-1);
      if(y && dist[y-1][x] - grid[y][x] > cost)dist[y-1][x] = cost + grid[y][x], q.push(y-1,x);
      if(x < n-1 && dist[y][x+1] - grid[y][x] > cost)dist[y][x+1] = cost + grid[y][x], q.push(y,x+1);
      if(y < n-1 && dist[y+1][x] - grid[y][x] > cost)dist[y+1][x] = cost + grid[y][x], q.push(y+1,x);
    }
  }
  return ans;
}