#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 7;
const long long MOD = 1000000007;
struct Point {
  long long x;
  long long y;
  void build(long long a, long long b) {
    x = a;
    y = b;
  }
  vector<Point> next;
  void get_next(vector<vector<long long>> const& map) {
    long long py = y + 1;
    long long px = x;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y - 1;
    px = x;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y;
    px = x + 1;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y;
    px = x - 1;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
  }
};
signed main() {
  long long n;
  cin >> n;
  vector<long long> p(n * n);
  for (long long i = 0; i < n * n; ++i) cin >> p[i];
  vector<vector<long long>> cost(n + 5, vector<long long>(n + 5, inf));
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      long long bufy = min(i - 1, n - i);
      long long bufx = min(j - 1, n - j);
      cost[i][j] = min(bufy, bufx);
    }
  }
  long long res = 0;
  vector<vector<long long>> seki(n + 5, vector<long long>(n + 5, 1));
  for (const auto& val : p) {
    long long x = (val - 1) % n + 1;
    long long 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(cost);
      long long num = buf.next.size();
      for (long long 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;
}
