#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
void update_up(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
               int x);
void update_down(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                 int x);
void update_left(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                 int x);
void update_right(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                  int x);
void update_up(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
               int x) {
  if (y > 0) {
    dist[y - 1][x] = min(dist[y][x], dist[y - 1][x]);
  }
  if (x > 0) {
    dist[y][x - 1] = min(dist[y][x], dist[y][x - 1]);
  }
  if (x < n - 1) {
    dist[y][x + 1] = min(dist[y][x], dist[y][x + 1]);
    if (emp[y][x + 1]) update_right(dist, emp, y, x + 1);
  }
}
void update_down(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                 int x) {
  if (y < n - 1) {
    dist[y + 1][x] = min(dist[y][x], dist[y + 1][x]);
    if (emp[y + 1][x]) update_down(dist, emp, y + 1, x);
  }
  if (x > 0) {
    dist[y][x - 1] = min(dist[y][x], dist[y][x - 1]);
  }
  if (x < n - 1) {
    dist[y][x + 1] = min(dist[y][x], dist[y][x + 1]);
    if (emp[y][x + 1]) update_right(dist, emp, y, x + 1);
  }
}
void update_left(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                 int x) {
  if (y > 0) {
    dist[y - 1][x] = min(dist[y][x], dist[y - 1][x]);
  }
  if (y < n - 1) {
    dist[y + 1][x] = min(dist[y][x], dist[y + 1][x]);
    if (emp[y + 1][x]) update_down(dist, emp, y + 1, x);
  }
  if (x > 0) {
    dist[y][x - 1] = min(dist[y][x], dist[y][x - 1]);
  }
}
void update_right(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
                  int x) {
  if (y > 0) {
    dist[y - 1][x] = min(dist[y][x], dist[y - 1][x]);
  }
  if (y < n - 1) {
    dist[y + 1][x] = min(dist[y][x], dist[y + 1][x]);
    if (emp[y + 1][x]) update_down(dist, emp, y + 1, x);
  }
  if (x < n - 1) {
    dist[y][x + 1] = min(dist[y][x], dist[y][x + 1]);
    if (emp[y][x + 1]) update_right(dist, emp, y, x + 1);
  }
}
void update(vector<vector<int>> &dist, vector<vector<bool>> &emp, int y,
            int x) {
  if (y > 0) {
    dist[y - 1][x] = min(dist[y][x], dist[y - 1][x]);
  }
  if (y < n - 1) {
    dist[y + 1][x] = min(dist[y][x], dist[y + 1][x]);
    if (emp[y + 1][x]) update_down(dist, emp, y + 1, x);
  }
  if (x > 0) {
    dist[y][x - 1] = min(dist[y][x], dist[y][x - 1]);
  }
  if (x < n - 1) {
    dist[y][x + 1] = min(dist[y][x], dist[y][x + 1]);
    if (emp[y][x + 1]) update_right(dist, emp, y, x + 1);
  }
}
int main(void) {
  cin >> n;
  vector<int> P(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> P[i];
  }
  vector<vector<bool>> emp(n, vector<bool>(n, false));
  vector<vector<int>> dist(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = min(min(j, n - j - 1), min(i, n - i - 1));
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int x = P[i] % n - 1;
    if (P[i] % n == 0) x = n - 1;
    int y = P[i] / n;
    if (P[i] % n == 0) y--;
    ans += dist[y][x];
    emp[y][x] = true;
    update(dist, emp, y, x);
  }
  cout << ans << endl;
}
