#include <bits/stdc++.h>
using namespace std;
int arr[4][505][505];
long long ans;
int k;
void culc(int h, int l) {
  int cur = 500;
  for (int i = 0; i < 4; i++) {
    cur = min(cur, arr[i][h][l]);
  }
  ans += cur;
  for (int i = h + 1; i < k + 1; i++) arr[0][i][l] -= 1;
  for (int i = h - 1; i >= 1; i--) arr[1][i][l] -= 1;
  for (int i = l + 1; i < k + 1; i++) arr[2][h][i] -= 1;
  for (int i = l - 1; i >= 1; i--) arr[3][h][i] -= 1;
}
int main() {
  ans = 0;
  cin >> k;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) {
      arr[0][i][j] = i - 1;
      arr[1][i][j] = k - i;
      arr[2][i][j] = j - 1;
      arr[3][i][j] = k - j;
    }
  for (int i = 0; i < k * k; i++) {
    int cur;
    cin >> cur;
    int l = cur % k;
    int h = cur / k + 1;
    if (l == 0) {
      l = k;
      h--;
    }
    culc(h, l);
  }
  cout << ans << endl;
  return 0;
}
