#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> v;
  vector<vector<int>> c;
  for (size_t i = 0; i < n; i++) {
    vector<int> temp;
    for (size_t j = 0; j < n; j++) {
      temp.push_back(i * n + j + 1);
    }
    v.push_back(temp);
  }
  for (size_t i = 0; i < n; i++) {
    vector<int> temp;
    for (size_t j = 0; j < n; j++) {
      temp.push_back(j * n + i + 1);
    }
    c.push_back(temp);
  }
  int ans = 0;
  vector<int> inp;
  for (size_t i = 1; i <= n * n; i++) {
    int a;
    cin >> a;
    inp.push_back(a);
  }
  for (size_t i = 0; i < n * n; i++) {
    int r = (inp[i] - 1) / n;
    int f = (inp[i] - 1) % n;
    int ind = 0;
    int an = 0;
    for (size_t j = 0; j < v[r].size(); j++) {
      if (v[r][j] == inp[i]) {
        ind = j;
        break;
      }
    }
    an = min(ind, (int)v[r].size() - 1 - ind);
    v[r].erase(v[r].begin() + ind);
    for (size_t j = 0; j < c[f].size(); j++) {
      if (c[f][j] == inp[i]) {
        ind = j;
        break;
      }
    }
    an = min(an, min(ind, (int)c[f].size() - 1 - ind));
    ans += an;
    c[f].erase(c[f].begin() + ind);
  }
  cout << ans << endl;
}
