#include <bits/stdc++.h>
using namespace std;
using ll = int;
char maze[503][503];
int H;
int W;
int sh, sw;
int gh, gw;
vector<vector<int> > score(503, vector<int>(503, 100));
bool visited[503][503];
const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};
void bfs(int i, int j) {
  deque<pair<int, int> > Q;
  score[i][j] = 0;
  Q.push_back(make_pair(i, j));
  while (!Q.empty()) {
    pair<int, int> q = Q.front();
    Q.pop_front();
    int h = q.first;
    int w = q.second;
    for (int k = 0; k < 4; k++) {
      int nh = h + dh[k];
      int nw = w + dw[k];
      if (nh < 0 || nh >= H || nw < 0 || nw >= W) {
        continue;
      }
      if (score[nh][nw] > (score[h][w] + 1) && maze[nh][nw] == '#') {
        Q.push_back(make_pair(nh, nw));
        score[nh][nw] = score[h][w] + 1;
      }
      if (score[nh][nw] > score[h][w] && maze[nh][nw] != '#') {
        Q.push_front(make_pair(nh, nw));
        score[nh][nw] = score[h][w];
      }
    }
  }
}
int main() {
  ll nn;
  cin >> nn;
  H = nn + 2;
  W = nn + 2;
  ll qq;
  ll ans = 0, tmp;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      if (j == 0 || j == nn + 1 || i == 0 || i == nn + 1) {
        maze[i][j] = '.';
      } else
        maze[i][j] = '#';
    }
  }
  for (ll ii = 0; ii < nn * nn; ii++) {
    cin >> qq;
    sh = (qq - 1) / nn + 1;
    sw = (qq - 1) % nn + 1;
    maze[sh][sw] = '.';
    tmp = 999999999;
    for (ll jj = 0; jj <= nn + 1; jj++) {
      for (ll kk = 0; kk <= nn + 1; kk++) {
        score[jj][kk] = 99999999;
      }
    }
    bfs(sh, sw);
    for (ll jj = 0; jj <= nn + 1; jj++) {
      for (ll kk = 0; kk <= nn + 1; kk++) {
        if (jj == 0 || jj == nn + 1 || kk == 0 || kk == nn + 1) {
          if (tmp > score[jj][kk]) tmp = score[jj][kk];
        }
      }
    }
    ans += tmp;
  }
  cout << ans << endl;
  return 0;
}
