#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  vector<int> sq(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p.at(i);
    sq.at(i) = 1;
  }
  int x = 0;
  for (int i = 0; i < n * n; ++i) {
    int current = p.at(i) - 1;
    if (current < n || current % n == 0 || current % n == n - 1 ||
        current > n * n - n) {
      sq.at(current) = 0;
    } else {
      vector<int> count(4, 0);
      for (int j = current - n; j >= 0; j = j - n) {
        if (sq.at(j) != 0) {
          sq.at(p.at(i) - 1) = 0;
          count.at(0) += 1;
        }
      }
      for (int j = current + 1; j < (current / n + 1) * n; ++j) {
        if (sq.at(j) != 0) {
          sq.at(p.at(i) - 1) = 0;
          count.at(1) += 1;
        }
      }
      for (int j = current + n; j < n * n; j = j + n) {
        if (sq.at(j) != 0) {
          sq.at(p.at(i) - 1) = 0;
          count.at(2) += 1;
        }
      }
      for (int j = current - 1; j > current / n * n - 1; --j) {
        if (sq.at(j) != 0) {
          sq.at(p.at(i) - 1) = 0;
          count.at(3) += 1;
        }
      }
      if (*std::min_element(count.begin(), count.end()) > 0) {
        x += *std::min_element(count.begin(), count.end());
      }
    }
  }
  cout << x << endl;
  return 0;
}
