#include <bits/stdc++.h>
using namespace std;
const int INF = 1LL << 61;
const int MOD = 1000000007;
const int MAX_N = 500010;
int a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vector<int> A, B;
vector<int> g[MAX_N];
vector<int> visited(MAX_N, 0), used(MAX_N, 0), SIZE(MAX_N, 1);
vector<int> D(MAX_N);
vector<set<int>> VS(MAX_N);
struct union_find {
  int par[MAX_N];
  int Size[MAX_N];
  union_find(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      Size[i] = 1;
    }
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    if (same(x, y)) return;
    Size[root(x)] += Size[root(y)];
    par[root(y)] = root(x);
  }
};
void solve() {
  union_find uni(n * n);
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(n); j++) {
      VS[i * j + n].insert(i * j + n);
    }
  }
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(n - 1); j++) {
      g[i * n + j].push_back(i * n + j + 1);
      g[i * n + j + 1].push_back(i * n + j);
      g[(j + 1) * n + i].push_back(j * n + i);
      g[j * n + i].push_back((j + 1) * n + i);
    }
  }
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(n); j++) {
      D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j});
    }
  }
  for (int i = 0; i < int(n * n); i++) {
    x = A[i];
    used[x] = 1;
    a = x / n;
    b = x % n;
    if (a > 0 && used[x - n] && uni.root(x) != uni.root(x - n)) {
      if (uni.Size[x] > uni.Size[x - n]) {
        VS[uni.root(x)].erase(x - n);
        VS[uni.root(x - n)].erase(x);
        uni.unite(x, x - n);
        for (auto ite = VS[uni.root(x - n)].begin();
             ite != VS[uni.root(x - n)].end(); ite++) {
          VS[uni.root(x)].insert(*ite);
        }
      } else {
        VS[uni.root(x)].erase(x - n);
        VS[uni.root(x - n)].erase(x);
        uni.unite(x - n, x);
        for (auto ite = VS[uni.root(x)].begin(); ite != VS[uni.root(x)].end();
             ite++) {
          VS[uni.root(x - n)].insert(*ite);
        }
      }
      D[uni.root(x)] =
          min({D[x], D[x - n], D[uni.root(x)], D[uni.root(x - n)]});
    }
    if (a < n - 1 && used[x + n] && uni.root(x) != uni.root(x + n)) {
      if (uni.Size[x] > uni.Size[x + n]) {
        VS[uni.root(x)].erase(x + n);
        VS[uni.root(x + n)].erase(x);
        uni.unite(x, x + n);
        for (auto ite = VS[uni.root(x + n)].begin();
             ite != VS[uni.root(x + n)].end(); ite++) {
          VS[uni.root(x)].insert(*ite);
        }
      } else {
        VS[uni.root(x)].erase(x + n);
        VS[uni.root(x + n)].erase(x);
        uni.unite(x + n, x);
        for (auto ite = VS[uni.root(x)].begin(); ite != VS[uni.root(x)].end();
             ite++) {
          VS[uni.root(x + n)].insert(*ite);
        }
      }
      D[uni.root(x)] =
          min({D[x], D[x + n], D[uni.root(x)], D[uni.root(x + n)]});
    }
    if (b > 0 && used[x - 1] && uni.root(x) != uni.root(x - 1)) {
      if (uni.Size[x] > uni.Size[x - 1]) {
        VS[uni.root(x)].erase(x - 1);
        VS[uni.root(x - 1)].erase(x);
        uni.unite(x, x - 1);
        for (auto ite = VS[uni.root(x - 1)].begin();
             ite != VS[uni.root(x - 1)].end(); ite++) {
          VS[uni.root(x)].insert(*ite);
        }
      } else {
        VS[uni.root(x)].erase(x - 1);
        VS[uni.root(x - 1)].erase(x);
        uni.unite(x - 1, x);
        for (auto ite = VS[uni.root(x)].begin(); ite != VS[uni.root(x)].end();
             ite++) {
          VS[uni.root(x - 1)].insert(*ite);
        }
      }
      D[uni.root(x)] =
          min({D[x], D[x - 1], D[uni.root(x)], D[uni.root(x - 1)]});
    }
    if (b < n - 1 && used[x + 1] && uni.root(x) != uni.root(x + 1)) {
      if (uni.Size[x] > uni.Size[x + 1]) {
        VS[uni.root(x)].erase(x + 1);
        VS[uni.root(x + 1)].erase(x);
        uni.unite(x, x + 1);
        for (auto ite = VS[uni.root(x + 1)].begin();
             ite != VS[uni.root(x + 1)].end(); ite++) {
          VS[uni.root(x)].insert(*ite);
        }
      } else {
        VS[uni.root(x)].erase(x + 1);
        VS[uni.root(x + 1)].erase(x);
        uni.unite(x + 1, x);
        for (auto ite = VS[uni.root(x)].begin(); ite != VS[uni.root(x)].end();
             ite++) {
          VS[uni.root(x + 1)].insert(*ite);
        }
      }
      D[uni.root(x)] =
          min({D[x], D[x + 1], D[uni.root(x)], D[uni.root(x + 1)]});
    }
    p = 0;
    queue<int> Q;
    x = uni.root(x);
    Q.push(x);
    visited = vector<int>(n * n, 0);
    visited[x] = 1;
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (int x : g[v]) {
        x = uni.root(x);
        if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
          visited[x] = 1;
          Q.push(x);
          D[x] = D[v] + 1;
        }
        if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
          visited[x] = 1;
          Q.push(x);
          D[x] = D[v];
        }
      }
    }
    ans += D[x];
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < int(n * n); i++) {
    cin >> a;
    a--;
    A.push_back(a);
  }
  solve();
}
