#include <bits/stdc++.h>
using namespace std;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int main() {
  int n;
  cin >> n;
  auto f = [n](int y, int x) { return y * n + x; };
  vector<int> p(n * n), cost(n * n, 0);
  vector<bool> sit(n * n, true);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    --p[i];
  }
  for (int i = 0; i < n * n; ++i) {
    int y = i / n, x = i % n;
    cost[i] = min({y, n - 1 - y, x, n - 1 - x});
  }
  int ans = 0LL;
  for (int i = 0; i < n * n; ++i) {
    ans += cost[p[i]];
    queue<int> que;
    que.push(p[i]);
    sit[p[i]] = false;
    while (!que.empty()) {
      int j = que.front();
      que.pop();
      int y = j / n, x = j % n;
      for (int k = 0; k < 4; ++k) {
        int yy = y + dyx[k][0], xx = x + dyx[k][1];
        if (0 <= yy && yy < n && 0 <= xx && xx < n &&
            cost[f(yy, xx)] > cost[f(y, x)] + int(sit[f(y, x)])) {
          cost[f(yy, xx)] = cost[f(y, x)] + int(sit[f(y, x)]);
          que.push(f(yy, xx));
        }
      }
    }
  }
  cout << ans << endl;
}
