#include <bits/stdc++.h>
using namespace std;
int64_t min(int64_t a, int64_t b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}
int main() {
  int n;
  cin >> n;
  vector<vector<int> > s(n, vector<int>(n, 1));
  int64_t ans = 0;
  vector<int> p(n * n);
  int v[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (int i = 0; i < n * n; i++) {
    cin >> p.at(i);
  }
  for (size_t i = 0; i < p.size(); i++) {
    int x, y;
    x = (p.at(i) - 1) % n;
    y = (p.at(i) - 1) / n;
    s.at(x).at(y) = 0;
    vector<vector<int> > grid(n, vector<int>(n, n / 2));
    int temp = n / 2;
    grid.at(x).at(y) = 0;
    queue<pair<int, int> > wait;
    wait.push(make_pair(x, y));
    while (wait.empty() == 0) {
      pair<int, int> now = wait.front();
      wait.pop();
      if (grid.at(now.first).at(now.second) >= temp) {
        continue;
      }
      for (int j = 0; j < 4; j++) {
        int nx = now.first + v[j][0];
        int ny = now.second + v[j][1];
        if (nx == -1 || nx == n || ny == -1 || ny == n) {
          temp = min(temp, grid.at(now.first).at(now.second));
        } else {
          if (grid.at(nx).at(ny) > grid.at(now.first).at(now.second)) {
            grid.at(nx).at(ny) =
                grid.at(now.first).at(now.second) + s.at(nx).at(ny);
            wait.push(make_pair(nx, ny));
          }
        }
      }
    }
    ans += temp;
  }
  cout << ans << endl;
}
