#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  vector<vector<int>> board(n, vector<int>(n, 1));
  vector<int> movex{1, -1, 0, 0}, movey{0, 0, 1, -1};
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int sx = p % n, sy = p / n;
    board[sy][sx] = 0;
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        que;
    que.push(make_pair(0, make_pair(sy, sx)));
    vector<vector<int>> distance(n, vector<int>(n, -1));
    distance[sy][sx] = 0;
    while (!que.empty()) {
      pair<int, pair<int, int>> info = que.top();
      que.pop();
      int posx = info.second.second, posy = info.second.first;
      if (distance[posy][posx] < info.first) continue;
      for (int j = 0; j < 4; j++) {
        int x = movex[j] + posx, y = movey[j] + posy;
        if (x < 0 || x >= n || y < 0 || y >= n) continue;
        int kyori = board[y][x] + distance[posy][posx];
        if (distance[y][x] == -1 || distance[y][x] > kyori) {
          que.push(make_pair(kyori, make_pair(y, x)));
          distance[y][x] = kyori;
        }
      }
    }
    int mn = 250000;
    for (int i = 0; i < n; i++) {
      mn = distance[0][i] < mn ? distance[0][i] : mn;
      mn = distance[n - 1][i] < mn ? distance[n - 1][i] : mn;
      mn = distance[i][0] < mn ? distance[i][0] : mn;
      mn = distance[i][n - 1] < mn ? distance[i][n - 1] : mn;
    }
    ans += mn;
  }
  cout << ans << endl;
}
