#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x;
  if (scanf("%lld", &x) == -1) exit(0);
  return x;
}
const long long INF = 5e18;
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
const int N = 200;
int is_out[N][N] = {0};
int n;
int memo[N][N] = {0};
int scors[N][N] = {0};
void setscors(int x, int y) {
  scors[x][y] = 1 + min(x % (n - 1),
                        min(y % (n - 1),
                            min((n - 1 - x) % (n - 1), (n - 1 - y) % (n - 1))));
}
int min_(int v1, int v2, int v3, int v4) {
  return min(v1, min(v2, min(v3, v4)));
}
void update_point(int x, int y) {
  int v1 = INF;
  if (x < (n - 1)) {
    if (is_out[x + 1][y])
      v1 = scors[x + 1][y];
    else
      v1 = 1 + scors[x + 1][y];
  }
  int v2 = INF;
  if (x > 0) {
    if (is_out[x - 1][y])
      v2 = scors[x - 1][y];
    else
      v2 = 1 + scors[x - 1][y];
  }
  int v3 = INF;
  if (y < (n - 1)) {
    if (is_out[x][y + 1])
      v3 = scors[x][y + 1];
    else
      v3 = 1 + scors[x][y + 1];
  }
  int v4 = INF;
  if (y > 0) {
    if (is_out[x][y - 1])
      v4 = scors[x][y - 1];
    else
      v4 = 1 + scors[x][y - 1];
  }
  int v = INF;
  if (is_out[x][y])
    v = min_(v1, v2, v3, v4);
  else
    v = 1 + min_(v1, v2, v3, v4);
  scors[x][y] = min(scors[x][y], v);
}
void update_neighbors_of(int x, int y) {
  queue<pair<int, int>> q;
  if (x < n - 1) q.push({x + 1, y});
  if (x > 0) q.push({x - 1, y});
  if (y < n - 1) q.push({x, y + 1});
  if (y > 0) q.push({x, y - 1});
  int visited[N][N] = {0};
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (visited[p.first][p.second]) continue;
    visited[p.first][p.second] = true;
    update_point(p.first, p.second);
    if (p.first < n - 1) q.push({p.first + 1, p.second});
    if (p.first > 0) q.push({p.first - 1, p.second});
    if (p.second < n - 1) q.push({p.first, p.second + 1});
    if (p.second > 0) q.push({p.first, p.second - 1});
  }
}
int main() {
  n = read();
  vector<int> v;
  for (auto i = 0; i < (n * n); i++) {
    v.push_back(read() - 1);
  }
  for (auto i = 0; i < n * n; i++) {
    int x = v[i] / n;
    int y = v[i] % n;
    setscors(x, y);
  }
  int cnt = 0;
  for (auto i = 0; i < n * n; i++) {
    int x = v[i] / n;
    int y = v[i] % n;
    cnt += (scors[x][y]) ? scors[x][y] - 1 : 0;
    is_out[x][y] = 1;
    if (scors[x][y] > 0) scors[x][y]--;
    update_neighbors_of(x, y);
  }
  cout << cnt << endl;
  return 0;
}
