#include <bits/stdc++.h>
int main() {
  int N;
  scanf("%d", &N);
  int P[N * N];
  int c[N][N];
  int x, y;
  for (int i = 0; i < N * N; i++) {
    scanf("%d", &P[i]);
  }
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++) {
      c[i][k] = 1;
    }
  }
  int count_left = 0;
  int count_right = 0;
  int count_up = 0;
  int count_down = 0;
  int count_min = 0;
  int count_sum = 0;
  for (int i = 0; i < N * N; i++) {
    x = P[i] % N;
    y = P[i] / N;
    if (x != 0) {
      x = x - 1;
    } else if (x == 0) {
      x = N - 1;
      y = y - 1;
    }
    c[y][x] = 0;
    for (int k = x - 1; k >= 0; k--) {
      if (c[y][k] == 1) {
        count_left += 1;
      }
    }
    for (int k = x + 1; k <= N - 1; k++) {
      if (c[y][k] == 1) {
        count_right += 1;
      }
    }
    if (count_left >= count_right) {
      count_min = count_right;
    } else {
      count_min = count_left;
    }
    for (int k = y - 1; k >= 0; k--) {
      if (c[k][x] == 1) {
        count_up += 1;
      }
    }
    if (count_min >= count_up) {
      count_min = count_up;
    }
    for (int k = y + 1; k <= N - 1; k++) {
      if (c[k][x] == 1) {
        count_down += 1;
      }
    }
    if (count_min >= count_down) {
      count_min = count_down;
    }
    count_sum += count_min;
    count_left = 0;
    count_right = 0;
    count_up = 0;
    count_down = 0;
  }
  printf("%d\n", count_sum);
  return 0;
}
