#include <bits/stdc++.h>
using namespace std;
const long long int N = 5e2 + 5;
long long int a[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  long long int i, j, row, col, p[n * n], tot1, tot2, tot3, tot4, cur, ans = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) a[i][j] = 1;
  for (i = 0; i < n * n; i++) cin >> p[i];
  for (i = 0; i < n * n; i++) {
    cur = p[i];
    cur--;
    row = cur / n;
    col = cur - (row * n);
    row++;
    col++;
    a[row][col] = 0;
    tot1 = 0, tot2 = 0, tot3 = 0, tot4 = 0;
    for (j = row; j > 0; j--)
      if (a[j][col]) tot1++;
    for (j = row; j <= n; j++)
      if (a[j][col]) tot2++;
    for (j = col; j > 0; j--)
      if (a[row][j]) tot3++;
    for (j = col; j <= n; j++)
      if (a[row][j]) tot4++;
    ans += min({tot1, tot2, tot3, tot4});
  }
  cout << ans;
  return 0;
}
