#include <bits/stdc++.h>
using namespace std;
const int inf = 1010001000;
const long long INF = 1001000100010001000;
const int mod = (int)1e9 + 7;
int n;
int c, u, d, r, l;
vector<vector<int>> v;
void tansaku(int i, int j) {
  if (i == 0 or i == n - 1 or j == 0 or j == n - 1) {
    return;
  }
  u = 0;
  d = 0;
  r = 0;
  l = 0;
  for (int k = (0); k < (i); ++k) {
    if (v.at(k).at(j) == 1) u++;
  }
  for (int k = (i + 1); k < (n); ++k) {
    if (v.at(k).at(j) == 1) d++;
  }
  for (int k = (0); k < (j); ++k) {
    if (v.at(i).at(k) == 1) r++;
  }
  for (int k = (j + 1); k < (n); ++k) {
    if (v.at(i).at(k) == 1) l++;
  }
  c += min(min(r, l), min(u, d));
}
int main() {
  cin >> n;
  c = 0;
  v.resize(n, (vector<int>(n, 1)));
  vector<int> p(n * n);
  for (int i = (0); i < (n * n); ++i) {
    int a;
    cin >> a;
    a--;
    p.at(i) = a;
    int y = p.at(i) / n;
    int x = p.at(i) % n;
    tansaku(y, x);
    v.at(y).at(x) = 0;
  }
  cout << c << endl;
  return 0;
}
