#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }
  vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
  vector<vector<int>> vv(n, vector<int>(n, 1));
  int res = 0;
  for (int i = 0; i < n * n; i++) {
    int r = n * n;
    vector<vector<int>> m(n, vector<int>(n, n * n));
    int row = (v[i] - 1) / n, col = (v[i] - 1) % n;
    vector<pair<int, int>> vp;
    vp.push_back(make_pair(v[i] - 1, 0));
    while (vp.size() > 0) {
      int pos = vp.back().first, hate = vp.back().second;
      if (pos / n == 0 || pos / n == n - 1 || pos % n == 0 || pos % n == n - 1)
        r = min(r, hate);
      vp.pop_back();
      row = pos / n, col = pos % n;
      for (auto d : dir) {
        int x = row + d.first, y = col + d.second;
        if (x < 0 || y < 0 || x >= n || y >= n) continue;
        int updated_hate = hate;
        if (vv[x][y] == 1) updated_hate++;
        if (updated_hate >= m[x][y]) continue;
        m[x][y] = updated_hate;
        vp.push_back(make_pair(x * n + y, updated_hate));
      }
    }
    vv[(v[i] - 1) / n][(v[i] - 1) % n] = 0;
    res += r;
  }
  cout << res << endl;
}
