#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e7;
const long long MM = 1e16;
const long long mod = 30;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
long long expo(long long x, long long n, long long M = mod) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = (ans * x) % M;
    }
    x = (x * x) % M;
    n >>= 1;
  }
  return ans;
}
long long grid[1001][1001];
vector<long long> row[1001], col[1001];
map<long long, pair<long long, long long> > mp;
void solve() {
  long long n, i, j;
  cin >> n;
  vector<long long> p(n * n);
  for (i = 0; i < n * n; i++) cin >> p[i];
  long long ans = 0, t = 1;
  for (i = 1; i < n + 1; i++) {
    for (j = 1; j < n + 1; j++) {
      grid[i][j] = t;
      mp[t] = {i, j};
      t++;
    }
  }
  for (i = 0; i < n * n; i++) {
    pair<long long, long long> temp = mp[p[i]];
    long long r = temp.first;
    long long c = temp.second;
    long long count_less = 0, count_big = 0;
    row[r].push_back(c);
    col[c].push_back(r);
    for (j = 0; j < row[r].size(); j++) {
      if (c > row[r][j])
        count_less++;
      else if (c < row[r][j])
        count_big++;
    }
    long long re = MM;
    if (count_less == (c - 1) || count_big == (n - c)) continue;
    re = min(re, (c - 1) - count_less);
    re = min(re, (n - c) - count_big);
    count_less = 0;
    count_big = 0;
    for (j = 0; j < col[c].size(); j++) {
      if (r > col[c][j])
        count_less++;
      else if (r < col[c][j])
        count_big++;
    }
    if (count_less == (r - 1) || count_big == (n - r)) continue;
    re = min(re, (r - 1) - count_less);
    re = min(re, (n - r) - count_big);
    ans += re;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
}
