#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500;
bool grid[MAX_N][MAX_N];
struct Node {
  int y;
  int x;
  int dist;
  Node(int y = -1, int x = -1, int dist = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
  }
};
const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};
int main() {
  int N;
  cin >> N;
  vector<int> P;
  for (int i = 0; i < N * N; ++i) {
    int p;
    cin >> p;
    P.push_back(p);
  }
  memset(grid, false, sizeof(grid));
  int ans = 0;
  for (int i = 0; i < N * N; ++i) {
    int p = P[i];
    int y = (p - 1) / N;
    int x = (p - 1) % N;
    deque<Node> dque;
    dque.push_back(Node(y, x, 0));
    bool loop = true;
    bool visited[N][N];
    memset(visited, false, sizeof(visited));
    while (loop && !dque.empty()) {
      Node node = dque.front();
      dque.pop_front();
      if (visited[node.y][node.x]) continue;
      visited[node.y][node.x] = true;
      for (int i = 0; loop && i < 4; ++i) {
        int ny = node.y + DY[i];
        int nx = node.x + DX[i];
        if (ny < 0 || nx < 0 || N <= ny || N <= nx) {
          ans += node.dist;
          loop = false;
        } else {
          if (grid[ny][nx]) {
            dque.push_front(Node(ny, nx, node.dist));
          } else {
            dque.push_back(Node(ny, nx, node.dist + 1));
          }
        }
      }
    }
    grid[y][x] = true;
  }
  cout << ans << endl;
  return 0;
}
