#include <bits/stdc++.h>
using namespace std;
int seat[500 * 500 + 1];
int main() {
  int N;
  cin >> N;
  int total_seat = 0;
  for (int k = 0; k < N * N; k++) {
    int p;
    cin >> p;
    int temp_up = 0;
    for (int i = p - N; i > 0; i -= N) {
      if (seat[i] == 0) {
        temp_up++;
      }
    }
    int temp_down = 0;
    for (int i = p + N; i <= N * N; i += N) {
      if (seat[i] == 0) {
        temp_down++;
      }
    }
    int temp_right = 0;
    int right_bound = ((p - 1) / N + 1) * N;
    for (int i = p + 1; i <= right_bound; i++) {
      if (seat[i] == 0) {
        temp_right++;
      }
    }
    int temp_left = 0;
    int left_bound = ((p - 1) / N) * N + 1;
    for (int i = p - 1; i >= left_bound; i--) {
      if (seat[i] == 0) {
        temp_left++;
      }
    }
    int minimum = min(temp_up, min(temp_down, min(temp_right, temp_left)));
    total_seat += minimum;
    seat[p] = 1;
  }
  cout << total_seat << endl;
  return 0;
}
