#include <bits/stdc++.h>
using namespace std;
int maxElement(int arr[], int n) {
  int maxi = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] > maxi) maxi = arr[i];
  }
  return maxi;
}
int minElement(int arr[], int n) {
  int mini = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] < mini) mini = arr[i];
  }
  return mini;
}
string trim(const string& str) {
  int first = str.find_first_not_of(' ');
  if (string::npos == first) {
    return str;
  }
  int last = str.find_last_not_of(' ');
  return str.substr(first, (last - first + 1));
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int arr[n * n + 1];
  for (int i = 1; i < n * n + 1; i++) cin >> arr[i];
  bool seat[n + 1][n + 1];
  for (int i = 1; i < n + 1; i++)
    for (int k = 1; k <= n; k++) seat[i][k] = true;
  long long int ans = 0;
  for (int i = 1; i < n * n + 1; i++) {
    int c = arr[i] % n;
    int r = arr[i] / (n) + 1;
    if (c == 0) c = n, r = r - 1;
    int maxi = INT_MAX, count = 0;
    for (int k = c + 1; k <= n; k++)
      if (seat[r][k]) count++;
    maxi = min(count, maxi);
    count = 0;
    for (int k = 1; k <= c - 1; k++)
      if (seat[r][k]) count++;
    maxi = min(count, maxi);
    count = 0;
    for (int k = 1; k <= r - 1; k++)
      if (seat[k][c]) count++;
    maxi = min(count, maxi);
    count = 0;
    for (int k = r + 1; k <= n; k++)
      if (seat[k][c]) count++;
    maxi = min(count, maxi);
    seat[r][c] = false;
    ans = ans + (long long int)maxi;
  }
  cout << ans << '\n';
  return 0;
}
