#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 a[_max][_max];
int b[_max * _max];
int n;
long long total = 0;
bool biao[_max][_max];
void BFS(int shu) {
  int fang[_max][_max];
  memset(fang, -1, sizeof(fang));
  long long bushu = -1;
  int y = 0;
  int x = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[i][j] == shu) {
        x = i;
        y = j;
      }
    }
  }
  biao[x][y] = true;
  fang[x][y] = 0;
  node no;
  no.x = x;
  no.y = y;
  queue<node> q;
  q.push(no);
  while (!q.empty()) {
    node dang = q.front();
    q.pop();
    int dan_x = dang.x, dan_y = dang.y;
    if (dan_x == 1 || dan_x == n || dan_y == 1 || dan_y == n) {
      if (bushu == -1 || fang[dan_x][dan_y] < bushu) {
        bushu = fang[dan_x][dan_y];
      }
      continue;
    }
    if (bushu != -1 && fang[dan_x][dan_y] >= bushu) {
      continue;
    }
    node next;
    for (int i = 0; i < 4; i++) {
      next.x = dang.x + dir[i][0];
      next.y = dang.y + dir[i][1];
      if (next.x <= 0 || next.x > n || next.y <= 0 || next.y > n) {
        continue;
      }
      if (fang[next.x][next.y] == -1) {
        if (!biao[next.x][next.y]) {
          fang[next.x][next.y] = fang[dang.x][dang.y] + 1;
        } else {
          fang[next.x][next.y] = fang[dang.x][dang.y];
        }
        q.push(next);
      }
    }
  }
  total += bushu;
}
int main() {
  cin >> n;
  int he = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = he++;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    cin >> b[i];
    BFS(b[i]);
  }
  cout << total << endl;
}
