#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<vector<int> > a(505, vector<int>(505, 0));
long long minHaters = INT_MAX, haters = 0, n = 0;
vector<vector<bool> > visited(505, vector<bool>(505, 0));
void dfs(int i, int j) {
  visited[i][j] = 1;
  if (i == 1 || j == 1 || i == n || j == n) {
    minHaters = min(minHaters, haters);
    return;
  }
  if (!visited[i + 1][j]) {
    if (a[i + 1][j] != -1) haters++;
    dfs(i + 1, j);
    if (a[i + 1][j] != -1) haters--;
  }
  if (!visited[i][j + 1]) {
    if (a[i][j + 1] != -1) haters++;
    dfs(i, j + 1);
    if (a[i][j + 1] != -1) haters--;
  }
  if (!visited[i][j - 1]) {
    if (a[i][j - 1] != -1) haters++;
    dfs(i, j - 1);
    if (a[i][j - 1] != -1) haters--;
  }
  if (!visited[i - 1][j]) {
    if (a[i - 1][j] != -1) haters++;
    dfs(i - 1, j);
    if (a[i - 1][j] != -1) haters--;
  }
}
void solve() {
  int p[n * n];
  for (auto& x : p) cin >> x;
  int val = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = val++;
    }
  }
  long long totalHaters = 0;
  for (auto& x : p) {
    int i = x / n + 1, j = 0;
    if (x % n == 0) i--;
    while (a[i][j] != x) j++;
    dfs(i, j);
    totalHaters += minHaters;
    minHaters = INT_MAX;
    haters = 0;
    a[i][j] = -1;
    for (int k = 0; k < 505; k++) fill(visited[k].begin(), visited[k].end(), 0);
  }
  cout << totalHaters << endl;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  cin >> n;
  solve();
  return 0;
}
