#include <bits/stdc++.h>
using namespace std;
int main() {
  const int max_n = 500;
  const int INF = 100000;
  int n;
  int p[max_n * max_n + 1];
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<vector<bool>> costmer(max_n + 1, vector<bool>(max_n + 1, 1));
  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  int counter = 0;
  for (int c = 0; c < n * n; c++) {
    queue<pair<int, int>> todo;
    vector<vector<int>> cost(max_n + 1, vector<int>(max_n + 1, INF));
    int ans = INF;
    int x = p[c] / n;
    int y = p[c] % n;
    todo.push(make_pair(x, y));
    costmer[x][y] = 0;
    cost[x][y] = 0;
    while (!todo.empty()) {
      pair<int, int> pos = todo.front();
      x = pos.first;
      y = pos.second;
      todo.pop();
      if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
        ans = min(ans, cost[x][y]);
      }
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        int temp;
        if (costmer[nx][ny] == 1) {
          temp = cost[x][y] + 1;
        } else
          temp = cost[x][y];
        if (cost[nx][ny] > temp) {
          cost[nx][ny] = temp;
          todo.push(make_pair(nx, ny));
        }
      }
    }
    if (ans >= 1) counter += ans;
  }
  cout << counter << endl;
  return 0;
}
