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