#include <bits/stdc++.h>
using namespace std;
long long t;
vector<vector<long long>> seats;
void bfs(long long x, long long y) {
  queue<pair<long long, long long>> q;
  q.push({x, y});
  while (!q.empty()) {
    auto&& xy = q.front();
    q.pop();
    long long x = xy.first, y = xy.second;
    if (y > 0 && seats[y - 1][x] > seats[y][x] + 1) {
      seats[y - 1][x] = seats[y][x] + 1;
      q.push({y - 1, x});
    }
    if (y < t - 1 && seats[y + 1][x] > seats[y][x] + 1) {
      seats[y + 1][x] = seats[y][x] + 1;
      q.push({y + 1, x});
    }
    if (x > 0 && seats[y][x - 1] > seats[y][x] + 1) {
      seats[y][x - 1] = seats[y][x] + 1;
      q.push({y, x - 1});
    }
    if (x < t - 1 && seats[y][x + 1] > seats[y][x] + 1) {
      seats[y][x + 1] = seats[y][x] + 1;
      q.push({y, x + 1});
    }
  }
}
int main() {
  cin >> t;
  for (long long y = 0; y < t; y++) {
    vector<long long> row;
    for (long long x = 0; x < t; x++) {
      row.push_back(min({x, t - x - 1, y, t - y - 1}));
    }
    seats.push_back(row);
  }
  long long result = 0;
  for (long long i = 0; i < t * t; i++) {
    long long number;
    cin >> number;
    number--;
    long long x = number % t;
    long long y = number / t;
    result += seats[y][x];
    seats[y][x]--;
    bfs(x, y);
  }
  cout << fixed << result;
}
