#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, A[505 * 505];
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> A[i];
  }
  int map[n + 2][n + 2];
  int fill[n + 2][n + 2];
  for (int y = 0; y < n + 2; y++) {
    for (int x = 0; x < n + 2; x++) {
      int X = x - 1;
      int Y = y - 1;
      map[y][x] = max(0, min(min(min(X, Y), n - X - 1), n - Y - 1));
      fill[y][x] = 1;
    }
  }
  long cost = 0;
  int currentX[505 * 505];
  int currentY[505 * 505];
  int currentX2[505 * 505];
  int currentY2[505 * 505];
  int currentCount = 0;
  int currentCount2 = 0;
  for (int i = 0; i < n * n; i++) {
    int a = A[i] - 1;
    int y = a / n + 1;
    int x = a % n + 1;
    cost += map[y][x];
    fill[y][x] = 0;
    currentX[currentCount] = x;
    currentY[currentCount] = y;
    currentCount++;
    while (currentCount) {
      currentCount2 = 0;
      for (int j = 0; j < currentCount; j++) {
        int yy = currentY[j], xx = currentX[j];
        int newCost = map[yy][xx] + fill[yy][xx];
        if (map[yy][xx - 1] > newCost) {
          map[yy][xx - 1] = newCost;
          currentX2[currentCount2] = yy;
          currentY2[currentCount2] = xx - 1;
          currentCount2++;
        }
        if (map[yy - 1][xx] > newCost) {
          map[yy - 1][xx] = newCost;
          currentX2[currentCount2] = yy - 1;
          currentY2[currentCount2] = xx;
          currentCount2++;
        }
        if (map[yy][xx + 1] > newCost) {
          map[yy][xx + 1] = newCost;
          currentX2[currentCount2] = yy;
          currentY2[currentCount2] = xx + 1;
          currentCount2++;
        }
        if (map[yy + 1][xx] > newCost) {
          map[yy + 1][xx] = newCost;
          currentX2[currentCount2] = yy + 1;
          currentY2[currentCount2] = xx;
          currentCount2++;
        }
      }
      currentCount = currentCount2;
      for (int j = 0; j < currentCount2; j++) {
        currentX[j] = currentX2[j];
        currentY[j] = currentY2[j];
      }
    }
  }
  cout << cost << endl;
}
