#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int> > &ans, vector<vector<bool> > &vis, int row,
         int col, int &flag, int n) {
  if (row == 0 || col == 0 || row == n + 1 || col == n + 1) {
    flag = 1;
    return;
  }
  if (vis[row][col] == true || ans[row][col] != 0) return;
  vis[row][col] = true;
  dfs(ans, vis, row, col + 1, flag, n);
  dfs(ans, vis, row, col - 1, flag, n);
  dfs(ans, vis, row + 1, col, flag, n);
  dfs(ans, vis, row - 1, col, flag, n);
  vis[row][col] = false;
}
int main() {
  int n;
  cin >> n;
  vector<vector<int> > ans(n + 2, vector<int>(n + 2, 1));
  for (int i = 0; i <= n + 1; i++) {
    ans[0][i] = 0;
    ans[i][0] = 0;
    ans[n + 1][i] = 0;
    ans[i][n + 1] = 0;
  }
  int res = 0;
  vector<vector<bool> > vis(n + 2, vector<bool>(n + 2, false));
  for (int i = 0; i < n * n; i++) {
    int x;
    cin >> x;
    int row = ceil((float)x / n);
    int col = x % n;
    if (col == 0) col = n;
    int flag = 0;
    ans[row][col] = 0;
    dfs(ans, vis, row, col, flag, n);
    if (flag == 0) {
      int a = 0, b = 0, c = 0, d = 0;
      for (int i = row + 1; i <= n; i++) {
        if (ans[i][col] != 0) a++;
      }
      for (int i = row - 1; i > 0; i--) {
        if (ans[i][col] != 0) b++;
      }
      for (int i = col - 1; i > 0; i--) {
        if (ans[row][i] != 0) c++;
      }
      for (int i = col + 1; i <= n; i++) {
        if (ans[row][i] != 0) d++;
      }
      res = res + min(min(a, b), min(c, d));
    }
  }
  cout << res << " ";
}
