#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<long long>;
using vvl = vector<vl>;
using vp = vector<pair<int, int>>;
string s;
int k;
ll INF = 10000000000000000;
int main() {
  int N;
  cin >> N;
  vector<vvi> ST(N, vvi(N, vi(4)));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) ST[i][j] = {i, N - j - 1, N - i - 1, j};
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int pair<int, int>;
    cin >> pair<int, int>;
    pair<int, int> --;
    ans += *min_element(ST[pair<int, int> / N][pair<int, int> % N].begin(),
                        ST[pair<int, int> / N][pair<int, int> % N].end());
    for (int i = 0; i < pair<int, int> % N; i++) ST[pair<int, int> / N][i][1]--;
    for (int i = N - 1; i > pair<int, int> % N; i--)
      ST[pair<int, int> / N][i][3]--;
    for (int i = 0; i < pair<int, int> / N; i++) ST[i][pair<int, int> % N][2]--;
    for (int i = N - 1; i > pair<int, int> / N; i--)
      ST[i][pair<int, int> % N][0]--;
  }
  cout << ans << endl;
}
