#include <bits/stdc++.h>
using namespace std;
template <class T>
void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
void func_to_create() {
  uint32_t N;
  cin >> N;
  std::vector<std::vector<bool>> seats;
  for (uint32_t i = 0; i < N; i++) {
    std::vector<bool> row(N, true);
    seats.push_back(row);
  }
  uint64_t count_hateness = 0;
  for (uint32_t i = 0; i < N * N; i++) {
    uint32_t P;
    cin >> P;
    int row_number = (P - 1) / N;
    int col_number = (P - 1) % N;
    seats[row_number][col_number] = false;
    uint32_t count_up = 0;
    for (uint32_t k = 0; k < row_number; k++) {
      count_up += seats[k][col_number];
    }
    uint32_t count_down = 0;
    for (uint32_t k = row_number + 1; k < N; k++) {
      count_down += seats[k][col_number];
    }
    uint32_t count_left = 0;
    for (uint32_t k = 0; k < col_number; k++) {
      count_left += seats[row_number][k];
    }
    uint32_t count_right = 0;
    for (uint32_t k = col_number + 1; k < N; k++) {
      count_right += seats[row_number][k];
    }
    uint32_t new_count =
        std::min({count_up, count_down, count_left, count_right});
    count_hateness += new_count;
  }
  cout << count_hateness << endl;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  func_to_create();
  return 0;
}
