#include <bits/stdc++.h>
using namespace std;
int cie[500][500];
int visit[500][500];
typedef struct position {
  int y;
  int x;
  int hater;
} pos;
int hate(int row, int col, int n, int visit[500][500]) {
  int result = INT_MAX;
  queue<pos*> q;
  int ny, nx;
  int l_visit[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      l_visit[i][j] = 0;
    }
  }
  vector<pair<int, int> > mov;
  mov.push_back(make_pair(0, 1));
  mov.push_back(make_pair(0, -1));
  mov.push_back(make_pair(1, 0));
  mov.push_back(make_pair(-1, 0));
  l_visit[row][col] = 1;
  visit[row][col] = 1;
  if (row == 0 || row == n - 1 || col == 0 || col == n - 1) return 0;
  pos* current = new pos;
  current->y = row;
  current->x = col;
  current->hater = 0;
  q.push(current);
  int haters;
  while (!q.empty()) {
    current = q.front();
    row = current->y;
    col = current->x;
    haters = current->hater;
    q.pop();
    for (int j = 0; j < 4; j++) {
      pos* child = new pos;
      ny = row + mov[j].first;
      nx = col + mov[j].second;
      child->y = ny;
      child->x = nx;
      if (visit[ny][nx] && !l_visit[ny][nx])
        child->hater = haters;
      else if (!visit[ny][nx] && !l_visit[ny][nx])
        child->hater = haters + 1;
      if (ny >= 0 && nx >= 0 && ny <= n - 1 && nx <= n - 1) {
        if (ny == 0 || nx == 0 || ny == n - 1 || nx == n - 1) {
          if (!l_visit[ny][nx]) {
            result = child->hater < result ? child->hater : result;
            l_visit[ny][nx] = 1;
          }
        } else if (!l_visit[ny][nx]) {
          q.push(child);
          l_visit[ny][nx] = 1;
        }
      }
    }
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, num, y, x, counter = 0;
  int result;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> num;
    if (num % n == 0)
      y = num / n - 1;
    else
      y = num / n;
    x = num - n * y - 1;
    result = hate(y, x, n, visit);
    counter += result;
  }
  cout << counter;
  return 0;
}
