#include <bits/stdc++.h>
#include <queue>
using namespace std;

int main() {
  int n;
  int c =0;
  cin >> n;
  vector<int> m(n*n);
  vector<int> b(n*n);
  vector<int> d = {-1,1,-n,n};
  for (int i = 0; i < n*n; i++) {
   m.at(i) = min(min(n-i%n-1,n-i/n-1),min(i%n,i/n));
  }
  for (int i = 0; i < n*n; i++) {
    int pi;
    cin >> pi;
    pi -= 1;
    b.at(pi) = 1;
    c += m[pi];
    queue<int> q;
    q.push(pi);
    while (q.empty() == 0){
      int ni;
      ni = q.front();
      q.pop();
      for (int j=0;j<4;j++){
        int dj = d.at(j);
        if ((0<=ni+dj)&&(ni+dj<=n*n-1)){
          if (b.at(ni) == 0){
            if (m.at(ni+dj) > m.at(ni)+1){
              m.at(ni+dj) = m.at(ni)+1;
              q.push(ni+dj);
            }
          }else{
              if (m.at(ni+dj)>m.at(ni)){
                m.at(ni+dj) = m.at(ni);
                q.push(ni+dj);
          }
        }  
        }
      }  
    } 
  }
  cout << c << endl;
}