#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -par[root(x)]; }
} uft(250000);
int main() {
  int n;
  cin >> n;
  vi p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    p[i]--;
  }
  vvi fun(n, vi(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int mi = i;
      mi = min(mi, j);
      mi = min(mi, n - 1 - i);
      mi = min(mi, n - 1 - j);
      fun[i][j] = mi;
    }
  }
  int ans = 0;
  int cou = 4 * n - 4;
  vvi vvv(n, vi(n));
  for (int k = 0; k < n * n; ++k) {
    int i = p[k] / n;
    int j = p[k] % n;
    ans += fun[i][j];
    if (!cou) {
      continue;
    }
    queue<P> bfs;
    queue<P> q;
    q.push(P(i, j));
    vector<vector<bool> > check(n, vector<bool>(n, false));
    while (!q.empty()) {
      P now = q.front();
      int nowi = now.first;
      int nowj = now.second;
      q.pop();
      if (now.first && fun[nowi][nowj] < fun[nowi - 1][nowj]) {
        if (check[nowi - 1][nowj]) continue;
        bfs.push(P(nowi - 1, nowj));
        q.push(P(nowi - 1, nowj));
        check[nowi - 1][nowj] = true;
      }
      if (nowj && fun[nowi][nowj] < fun[nowi][nowj - 1]) {
        if (check[nowi][nowj - 1]) continue;
        bfs.push(P(nowi, nowj - 1));
        q.push(P(nowi, nowj - 1));
        check[nowi][nowj - 1] = true;
      }
      if (now.first < n - 1 && fun[nowi][nowj] < fun[nowi + 1][nowj]) {
        if (check[nowi + 1][nowj]) continue;
        bfs.push(P(nowi + 1, nowj));
        q.push(P(nowi + 1, nowj));
        check[nowi + 1][nowj] = true;
      }
      if (nowj < n - 1 && fun[nowi][nowj] < fun[nowi][nowj + 1]) {
        if (check[nowi][nowj + 1]) continue;
        bfs.push(P(nowi, nowj + 1));
        q.push(P(nowi, nowj + 1));
        check[nowi][nowj + 1] = true;
      }
      if (now.first && vvv[nowi - 1][nowj]) {
        if (check[nowi - 1][nowj]) continue;
        q.push(P(nowi - 1, nowj));
        check[nowi - 1][nowj] = true;
      }
      if (nowj && vvv[nowi][nowj - 1]) {
        if (check[nowi][nowj - 1]) continue;
        q.push(P(nowi, nowj - 1));
        check[nowi][nowj - 1] = true;
      }
      if (now.first < n - 1 && vvv[nowi + 1][nowj]) {
        if (check[nowi + 1][nowj]) continue;
        q.push(P(nowi + 1, nowj));
        check[nowi + 1][nowj] = true;
      }
      if (nowj < n - 1 && vvv[nowi][nowj + 1]) {
        if (check[nowi][nowj + 1]) continue;
        q.push(P(nowi, nowj + 1));
        check[nowi][nowj + 1] = true;
      }
    }
    while (!bfs.empty()) {
      P now = bfs.front();
      bfs.pop();
      fun[now.first][now.second]--;
      if (!fun[now.first][now.second]) {
        cou--;
      }
    }
    if (fun[i][j]) {
      cou--;
      fun[i][j];
    }
    vvv[i][j] = 1;
  }
  cout << ans << endl;
}
