#include <bits/stdc++.h>
using namespace std;
const int _max = 501;
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
struct node {
  int x;
  int y;
};
int hao[_max][_max];
int a[_max][_max];
int b[_max][_max];
int n;
long long total = 0;
bool biao[_max][_max];
void BFS(int shu) {
  vector<node> v;
  memset(b, 0, sizeof(b));
  int x = (shu - 1) / n;
  int y = (shu - 1) % n;
  total += a[x][y];
  queue<node> q;
  node no;
  no.x = x;
  no.y = y;
  b[x][y] = 1;
  q.push(no);
  while (!q.empty()) {
    node begin = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      node next;
      next.x = begin.x + dir[i][0];
      next.y = begin.y + dir[i][1];
      if (next.x >= 0 && next.y >= 0 && next.x < n && next.y < n &&
          b[next.x][next.y] == 0 &&
          a[begin.x][begin.y] + 1 == a[next.x][next.y]) {
        b[next.x][next.y] = 1;
        v.push_back(next);
        q.push(next);
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    a[v[i].x][v[i].y]--;
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < (n + 1) / 2; i++) {
    for (int j = i; j < n - i; j++) {
      a[i][j] = i;
      a[n - 1 - i][j] = i;
    }
    for (int j = i; j < n - i; j++) {
      a[j][i] = i;
      a[j][n - 1 - i] = i;
    }
    for (int j = i; j < n - i; j++) {
      hao[i][j] = i;
      hao[n - 1 - i][j] = i;
    }
    for (int j = i; j < n - i; j++) {
      hao[j][i] = i;
      hao[j][n - 1 - i] = i;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int shu;
    cin >> shu;
    BFS(shu);
  }
  cout << total << endl;
}
