#include <bits/stdc++.h>
using namespace std;
long long t;
long long n;
long long p[505];
long long dist[505][505];
long long occ[505][505];
struct cmp {
  bool operator()(const pair<long long, pair<long long, long long>> &a,
                  const pair<long long, pair<long long, long long>> &b) {
    return a.first > b.first;
  }
};
priority_queue<pair<long long, pair<long long, long long>>,
               vector<pair<long long, pair<long long, long long>>>, cmp>
    di;
bitset<400000> visited;
long long ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  t = 1;
  for (int _ = 1; _ <= t; _++) {
    cin >> n;
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= n + 1; j++) {
        dist[i][j] = (1ll << 60);
      }
    }
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        occ[i][j] = 1;
        dist[i][j] = min(min(i, j) - 1, n - max(i, j));
      }
    }
    for (int i = 0; i < n * n; i++) {
      cin >> p[i];
      p[i]--;
      ans += dist[p[i] / n + 1][p[i] % n + 1];
      occ[p[i] / n + 1][p[i] % n + 1] = 0;
      di.push(make_pair((0), (make_pair((p[i] / n + 1), (p[i] % n + 1)))));
      visited.reset();
      while (di.size()) {
        auto now = di.top().second;
        di.pop();
        if (visited[now.first * (n + 1) + now.second]) continue;
        visited[now.first * (n + 1) + now.second] = 1;
        if (now.first > 1) {
          if (dist[now.first - 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first - 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(make_pair((dist[now.first - 1][now.second]),
                              (make_pair((now.first - 1), (now.second)))));
          }
        }
        if (now.second > 1) {
          if (dist[now.first][now.second - 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second - 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(make_pair((dist[now.first][now.second - 1]),
                              (make_pair((now.first), (now.second - 1)))));
          }
        }
        if (now.first < n) {
          if (dist[now.first + 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first + 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(make_pair((dist[now.first + 1][now.second]),
                              (make_pair((now.first + 1), (now.second)))));
          }
        }
        if (now.second < n) {
          if (dist[now.first][now.second + 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second + 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(make_pair((dist[now.first][now.second + 1]),
                              (make_pair((now.first), (now.second + 1)))));
          }
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
