#include <bits/stdc++.h>
using namespace std;
int ijToNum(int nLine, int i, int j) {
  int seatNum = j * nLine + i + 1;
  return seatNum;
}
int numToJ(int nLine, int seatNum) {
  int jGuest = ((seatNum - 1) / nLine);
  return jGuest;
}
int numToI(int nLine, int seatNum) {
  int yGuest = numToJ(nLine, seatNum) + 1;
  int xGuest = seatNum - nLine * (yGuest - 1);
  return xGuest - 1;
}
int main() {
  int answer = 0;
  int nLine;
  cin >> nLine;
  int mvCost[nLine][nLine];
  for (int i = 0; i < nLine; i++) {
    for (int j = 0; j < nLine; j++) {
      mvCost[i][j] = std::min({i, j, nLine - 1 - i, nLine - 1 - j});
    }
  }
  int nGuest = nLine * nLine;
  int seatNum[nGuest];
  for (int iGuest = 0; iGuest < nGuest; iGuest++) {
    cin >> seatNum[iGuest];
  }
  std::vector<int> subtrSeat;
  for (int iGuest = 0; iGuest < nGuest; iGuest++) {
    subtrSeat.push_back(seatNum[iGuest]);
    while ((int)subtrSeat.size() > 0) {
      int iGuest = numToI(nLine, subtrSeat[0]);
      int jGuest = numToJ(nLine, subtrSeat[0]);
      answer += mvCost[iGuest][jGuest];
      mvCost[iGuest][jGuest]--;
      if (mvCost[iGuest][jGuest] + 1 < mvCost[iGuest + 1][jGuest]) {
        subtrSeat.push_back(ijToNum(nLine, iGuest + 1, jGuest));
      }
      if (mvCost[iGuest][jGuest] + 1 < mvCost[iGuest - 1][jGuest]) {
        subtrSeat.push_back(ijToNum(nLine, iGuest - 1, jGuest));
      }
      if (mvCost[iGuest][jGuest] + 1 < mvCost[iGuest][jGuest + 1]) {
        subtrSeat.push_back(ijToNum(nLine, iGuest, jGuest + 1));
      }
      if (mvCost[iGuest][jGuest] + 1 < mvCost[iGuest + 1][jGuest]) {
        subtrSeat.push_back(ijToNum(nLine, iGuest, jGuest + 1));
      }
      subtrSeat.erase(subtrSeat.begin());
    }
  }
  cout << answer;
}
