#include <bits/stdc++.h>
using namespace std;
const int dy[] = {0, 1, 0, -1};
const int dx[] = {1, 0, -1, 0};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> grid(n + 2, vector<int>(n + 2, 0));
  for (int i = 0; i < (n + 2); ++i) {
    if (i == 0 || i == n + 1) continue;
    for (int j = 0; j < (n + 2); ++j) {
      if (j == 0 || j == n + 1) continue;
      grid[i][j] = 1;
    }
  }
  int res = 0;
  for (int _ = 0; _ < (n * n); ++_) {
    int p;
    cin >> p;
    int sy = (p - 1) / n + 1;
    int sx = p % n;
    if (p % n == 0) sx = n;
    grid[sy][sx] = 0;
    vector<vector<int>> dist(n + 2, vector<int>(n + 2, -1));
    dist[sy][sx] = 0;
    queue<pair<int, int>> q;
    q.push(make_pair(sy, sx));
    bool flg = false;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int y = p.first;
      int x = p.second;
      for (int dir = 0; dir < (4); ++dir) {
        int ny = y + dy[dir];
        int nx = x + dx[dir];
        if (ny < 0 || ny >= n + 2 || nx < 0 || nx >= n + 2) continue;
        if (dist[ny][nx] != -1) continue;
        if (grid[ny][nx] == 1) continue;
        dist[ny][nx] = dist[y][x] + 1;
        q.push(make_pair(ny, nx));
        if (ny == 0 || ny == n + 1 || nx == 0 || nx == n + 1) {
          flg = true;
        }
      }
    }
    if (!flg) {
      const int INF = 1001001001;
      vector<vector<int>> dist(n + 2, vector<int>(n + 2, INF));
      dist[sy][sx] = 0;
      vector<vector<bool>> seen(n + 2, vector<bool>(n + 2, false));
      seen[sy][sx] = true;
      priority_queue<pair<int, pair<int, int>>,
                     vector<pair<int, pair<int, int>>>,
                     greater<pair<int, pair<int, int>>>>
          pq;
      pq.push(make_pair(0, make_pair(sy, sx)));
      int cost = INF;
      while (!pq.empty()) {
        pair<int, pair<int, int>> pp = pq.top();
        pq.pop();
        int y = pp.second.first;
        int x = pp.second.second;
        for (int dir = 0; dir < (4); ++dir) {
          int ny = y + dy[dir];
          int nx = x + dx[dir];
          if (ny < 0 || ny >= n + 2 || nx < 0 || nx >= n + 2) continue;
          if (seen[ny][nx]) continue;
          seen[ny][nx] = true;
          if (dist[y][x] + grid[ny][nx] < dist[ny][nx]) {
            dist[ny][nx] = dist[y][x] + grid[ny][nx];
            pq.push(make_pair(dist[ny][nx], make_pair(ny, nx)));
            if (ny == 0 || ny == n + 1 || nx == 0 || nx == n + 1) {
              flg = true;
              cost = min(cost, dist[ny][nx]);
            }
          }
        }
        if (flg) break;
      }
      res += cost;
    }
  }
  cout << res << endl;
  return 0;
}
