#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 mt(736);
class dsu;
void update(dsu &white, vector<vector<int>> &ans, int x, int y, int ns);
class dsu {
  vector<int> par, val;
  vector<vector<int>> neigh;

 public:
  dsu(int n = 0) : par(n, -1), val(n, n + 1), neigh(n) {}
  int get_class(int v) { return par[v] == -1 ? v : par[v] = get_class(par[v]); }
  bool unite(int a, int b) {
    a = get_class(a);
    b = get_class(b);
    if (a == b) return false;
    assert(val[a] == val[b]);
    if (neigh[a].size() < neigh[b].size()) swap(a, b);
    neigh[a].insert(neigh[a].end(), neigh[b].begin(), neigh[b].end());
    neigh[b].clear();
    return true;
  }
  void add(int wh, int x) { neigh[get_class(wh)].push_back(x); }
  void update(vector<vector<int>> &ans, int wh, int ns) {
    if (ns < val[wh]) {
      val[wh] = ns;
      for (int i = 0; i < neigh[wh].size(); i++) {
        auto &it = neigh[wh][i];
        if (ans[it / ans.size()][it % ans.size()] == -1) {
          swap(it, neigh[wh].back());
          neigh[wh].pop_back();
          i--;
        } else
          ::update(*this, ans, it % ans.size(), it / ans.size(), ns);
      }
    }
  }
};
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
void update(dsu &white, vector<vector<int>> &ans, int x, int y, int ns) {
  if (ans[y][x] == -1) {
    white.update(ans, y * ans.size() + x, ns);
    return;
  }
  if (ns < ans[y][x]) {
    ans[y][x] = ns;
    for (int i = 0; i < 4; i++) {
      if (0 <= y + dy[i] && y + dy[i] < ans.size() && 0 <= x + dx[i] &&
          x + dx[i] < ans.size())
        update(white, ans, x + dx[i], y + dy[i], ns + 1);
    }
  }
}
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n;
  cin >> n;
  vector<int> perm(n * n);
  for (auto &it : perm) {
    cin >> it;
    it--;
  }
  vector<vector<int>> ans(n, vector<int>(n));
  for (int y = 0; y < ans.size(); y++)
    for (int x = 0; x < ans[y].size(); x++)
      ans[y][x] =
          min(min(y, (int)ans.size() - 1 - y), min(x, (int)ans.size() - 1 - x));
  dsu white(n * n);
  ll sum = 0;
  for (auto it : perm) {
    int y = it / n;
    int x = it % n;
    int ns = ans[it / n][it % n];
    sum += ans[it / n][it % n];
    ans[it / n][it % n] = -1;
    white.update(ans, it, ns);
    for (int i = 0; i < 4; i++) {
      if (0 <= y + dy[i] && y + dy[i] < ans.size() && 0 <= x + dx[i] &&
          x + dx[i] < ans.size()) {
        if (ans[y + dy[i]][x + dx[i]] == -1) {
          white.update(ans, (y + dy[i]) * n + x + dx[i], ns);
        } else {
          white.add(it, (y + dy[i]) * n + x + dx[i]);
          update(white, ans, x + dx[i], y + dy[i], ns);
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      if (0 <= y + dy[i] && y + dy[i] < ans.size() && 0 <= x + dx[i] &&
          x + dx[i] < ans.size()) {
        if (ans[y + dy[i]][x + dx[i]] == -1)
          white.unite(it, (y + dy[i]) * n + x + dx[i]);
      }
    }
  }
  cout << sum << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  solve();
  return 0;
}
