#include <bits/stdc++.h>
using namespace std;
clock_t timeStart, timeFinish;
void timeBegin() { timeStart = clock(); }
void timeEnd() { timeFinish = clock(); }
void timeDuration() {
  double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
  cout << "Time taken by program is : " << fixed << time_taken
       << setprecision(5);
  cout << " sec " << endl;
}
class InputReader {
 public:
  InputReader() {
    input_file = stdin;
    cursor = 0;
    fread(buffer, SIZE, 1, input_file);
  }
  InputReader(const char *file_name) {
    input_file = fopen(file_name, "r");
    cursor = 0;
    fread(buffer, SIZE, 1, input_file);
  }
  inline InputReader &operator>>(int &n) {
    while ((buffer[cursor] < '0' || buffer[cursor] > '9') &&
           buffer[cursor] != '-') {
      advance();
    }
    int sign = 1;
    if (buffer[cursor] == '-') {
      sign = -1;
      advance();
    }
    n = 0;
    while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
      n = n * 10 + buffer[cursor] - '0';
      advance();
    }
    n *= sign;
    return *this;
  }

 private:
  FILE *input_file;
  static const int SIZE = 1 << 17;
  int cursor;
  char buffer[SIZE];
  inline void advance() {
    ++cursor;
    if (cursor == SIZE) {
      cursor = 0;
      fread(buffer, SIZE, 1, input_file);
    }
  }
};
const int MAXN = 500;
int fenwickRow[1 + MAXN][1 + MAXN], fenwickColumn[1 + MAXN][1 + MAXN];
int outRow[1 + MAXN], outColumn[1 + MAXN];
void update(int x, int n, int *fenwick) {
  for (; x <= n; x += (x & -x)) fenwick[x]++;
}
int query(int x, int *fenwick) {
  int answer = 0;
  for (; x > 0; x -= (x & -x)) answer += fenwick[x];
  return answer;
}
int main() {
  timeBegin();
  int n;
  cin >> n;
  long long answer = 0;
  for (int i = 1; i <= n * n; i++) {
    int x;
    cin >> x;
    int row = (x - 1) / n + 1, column = (x - 1) % n + 1;
    int toUp = query(row, fenwickColumn[column]),
        toDown = outColumn[column] - toUp;
    int toLeft = query(column, fenwickRow[row]), toRight = outRow[row] - toLeft;
    answer += min(min(row - 1 - toUp, n - row - toDown),
                  min(column - 1 - toLeft, n - column - toRight));
    outRow[row]++;
    outColumn[column]++;
    update(column, n, fenwickRow[row]);
    update(row, n, fenwickColumn[column]);
  }
  cout << answer << "\n";
  timeEnd();
  return 0;
}
