#include <bits/stdc++.h>
const int MXN = 2e5 + 1;
const long long MNN = 1e6 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int OO = 1e9 + 500;
using namespace std;
void istxt(bool yes) {
  freopen("/Users/mac/Desktop/input.txt", "r", stdin);
  freopen("/Users/mac/Desktop/output.txt", "w", stdout);
}
int n;
vector<vector<int>> table;
int ans = 0;
int dfs(int row, int col, vector<vector<int>> &table) {
  if (row < 0 || row >= n || col < 0 || col >= n) {
    return 0;
  }
  if (table[row][col] == 2) {
    return INT_MAX / 2;
  }
  int count = table[row][col] == 1;
  table[row][col] = 2;
  bool up = dfs(row - 1, col, table) + count;
  bool down = dfs(row + 1, col, table) + count;
  bool left = dfs(row, col - 1, table) + count;
  bool right = dfs(row, col + 1, table) + count;
  table[row][col] = 1;
  return min({up, down, left, right});
}
void solve() {
  cin >> n;
  vector<int> p(n * n);
  table.assign(n + 1, vector<int>(n + 1, 1));
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
  }
  int count = 0;
  for (int i = 0; i < p.size(); ++i) {
    int person = p[i];
    int row = (person + n - 1) / n;
    int total = row * n;
    int start = total - n + 1;
    int col = person - start + 1;
    table[row - 1][col - 1] = 0;
    int a = dfs(row - 1, col - 1, table);
    count += a;
  }
  cout << count << endl;
}
int main() {
  solve();
  return 0;
}
