#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lint = long long;
const lint inf = 1e9 + 7;
const int MOD = 1000000007;
struct Point {
  int x;
  int y;
  void build(int a, int b) {
    x = a;
    y = b;
  }
  vector<Point> next;
  void get_next(int const& n) {
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx > 0 && nx <= n) {
        if (ny > 0 && ny <= n) {
          Point buf;
          buf.build(nx, ny);
          next.push_back(buf);
        }
      }
    }
  }
};
signed main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; ++i) cin >> p[i];
  vector<vector<int>> cost(n + 5, vector<int>(n + 5, inf));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      int bufy = min(i - 1, n - i);
      int bufx = min(j - 1, n - j);
      cost[i][j] = min(bufy, bufx);
    }
  }
  int res = 0;
  vector<vector<int>> seki(n + 5, vector<int>(n + 5, 1));
  for (int l = 0; l < n * n; ++l) {
    int val = p[l];
    int x = (val - 1) % n + 1;
    int y = (val - 1) / n + 1;
    seki[y][x] = 0;
    res += cost[y][x];
    Point s;
    s.build(x, y);
    queue<Point> que;
    que.push(s);
    while (!que.empty()) {
      Point buf = que.front();
      que.pop();
      buf.get_next(n);
      int num = buf.next.size();
      for (int i = 0; i < num; ++i) {
        if (cost[buf.next[i].y][buf.next[i].x] >
            cost[buf.y][buf.x] + seki[buf.y][buf.x]) {
          cost[buf.next[i].y][buf.next[i].x] =
              cost[buf.y][buf.x] + seki[buf.y][buf.x];
          que.push(buf.next[i]);
        }
      }
    }
  }
  cout << res << "\n";
  return 0;
}
