#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> seat(
      N, vector<pair<int, int>>(N, make_pair(0, 1)));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      seat[i][j].first = min({i, j, N - 1 - i, N - 1 - j});
      seat[i][j].second = 1;
    }
  }
  vector<int> order(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> order[i];
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int x, y;
    x = (order[i] - 1) / N;
    y = (order[i] - 1) % N;
    int now = 1000;
    int cntx = 0, cnty = 0, cntxm = 0, cntym = 0;
    seat[x][y].second = 0;
    for (int j = 0; j < N; j++) {
      if (x + j <= N - 1) {
        cntx += seat[x + j][y].second;
        now = min(now, seat[x + j][y].first + cntx);
      }
      if (x - j >= 0) {
        cntxm += seat[x - j][y].second;
        now = min(now, seat[x - j][y].first + cntxm);
      }
      if (y + j <= N - 1) {
        cnty += seat[x][y + j].second;
        now = min(now, seat[x][y + j].first + cnty);
      }
      if (y - j >= 0) {
        cntym += seat[x][y - j].second;
        now = min(now, seat[x][y - j].first + cntym);
      }
    }
    ans += now;
    for (int j = 1; j < N; j++) {
      if (x + j <= N - 1 && 0 <= x + j - 1 && x + j - 1 <= N - 1)
        seat[x + j][y].first =
            min(seat[x + j][y].first,
                seat[x + j - 1][y].first + seat[x + j - 1][y].second);
      if (x - j >= 0 && x - j + 1 >= 0 && x - j + 1 <= N - 1)
        seat[x - j][y].first =
            min(seat[x - j][y].first,
                seat[x - j + 1][y].first + seat[x - j + 1][y].second);
      if (y + j <= N - 1 && y + j - 1 >= 0 && y + j - 1 <= N - 1)
        seat[x][y + j].first =
            min(seat[x][y + j].first,
                seat[x][y + j - 1].first + seat[x][y + j - 1].second);
      if (y - j >= 0 && y - j + 1 <= N - 1 && y - j + 1 >= 0)
        seat[x][y - j].first =
            min(seat[x][y - j].first,
                seat[x][y - j + 1].first + seat[x][y - j + 1].second);
    }
  }
  cout << ans << endl;
  return 0;
}
