#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int N2 = N * N;
  vector<int> Para(N2, 0);
  vector<vector<int>> dist(N, vector<int>(N, 0));
  for (int i = 0; i < (N2); ++i) cin >> Para[i];
  int ans = 0;
  for (int i = 0; i < (N); ++i) {
    for (int j = 0; j < (N); ++j) {
      int res = min(i, j);
      res = min(res, N - 1 - i);
      res = min(res, N - 1 - j);
      dist[i][j] = res;
    }
  }
  for (int i = 0; i < (N2); ++i) {
    int res2, w, h = 0;
    res2 = Para[i];
    w = (res2 - 1) % N;
    h = (res2 - 1) / N;
    queue<pair<int, int>> que;
    ans += dist[w][h];
    if ((w - 1) >= 0 && (dist[w - 1][h] > dist[w][h])) {
      dist[w - 1][h] = dist[w][h];
      que.push(pair<int, int>(w - 1, h));
    }
    if ((w + 1) <= N - 1 && (dist[w + 1][h] > dist[w][h])) {
      dist[w + 1][h] = dist[w][h];
      que.push(pair<int, int>(w + 1, h));
    }
    if ((h + 1) <= N - 1 && (dist[w][h + 1] > dist[w][h])) {
      dist[w][h + 1] = dist[w][h];
      que.push(pair<int, int>(w, h + 1));
    }
    if ((h - 1) >= 0 && (dist[w][h - 1] > dist[w][h])) {
      dist[w][h - 1] = dist[w][h];
      que.push(pair<int, int>(w, h - 1));
    }
    while (!que.empty()) {
      int w2 = que.front().first;
      int h2 = que.front().second;
      que.pop();
      if ((w2 - 1) >= 0 && (dist[w2 - 1][h2] - dist[w2][h2]) > 1) {
        dist[w2 - 1][h2] = dist[w2][h2] + 1;
        que.push(pair<int, int>(w2 - 1, h2));
      }
      if ((w2 + 1) <= N - 1 && (dist[w2 + 1][h2] - dist[w2][h2] > 1)) {
        dist[w2 + 1][h2] = dist[w2][h2] + 1;
        que.push(pair<int, int>(w2 + 1, h2));
      }
      if ((h2 + 1) <= N - 1 && (dist[w2][h2 + 1] - dist[w2][h2] > 1)) {
        dist[w2][h2 + 1] = dist[w2][h2] + 1;
        que.push(pair<int, int>(w2, h2 + 1));
      }
      if ((h2 - 1) >= 0 && (dist[w2][h2 - 1] - dist[w2][h2]) > 1) {
        dist[w2][h2 - 1] = dist[w2][h2] + 1;
        que.push(pair<int, int>(w2, h2 - 1));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
