#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC target("avx2")
using namespace std;
using ll = long long;
using ld = long double;
const int p = 17;
int mul(int a, int b) { return (1LL * a * b) % p; }
int add(int a, int b) {
  int s = (a + b);
  if (s >= p) s -= p;
  return s;
}
int sub(int a, int b) {
  int s = (a + p - b);
  if (s >= p) s -= p;
  return s;
}
int po(int a, int deg) {
  if (deg == 0) return 1;
  if (deg % 2 == 1) return mul(a, po(a, deg - 1));
  int t = po(a, deg / 2);
  return mul(t, t);
}
int inv(int n) { return po(n, p - 2); }
mt19937 rnd(time(0));
struct DSU {
  vector<int> sz;
  vector<int> parent;
  void make_set(int v) {
    parent[v] = v;
    sz[v] = 1;
  }
  int find_set(int v) {
    if (v == parent[v]) return v;
    return parent[v] = find_set(parent[v]);
  }
  void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (sz[a] < sz[b]) swap(a, b);
      parent[b] = a;
      sz[a] += sz[b];
    }
  }
  DSU(int n) {
    sz.resize(n);
    parent.resize(n);
    for (int i = 0; i < n; i++) make_set(i);
  }
};
vector<vector<int>> d;
vector<vector<bool>> in;
int n;
vector<pair<int, int>> neighbours(int x, int y) {
  vector<pair<int, int>> ans;
  if (x > 0) ans.push_back(make_pair(x - 1, y));
  if (x < n - 1) ans.push_back(make_pair(x + 1, y));
  if (y > 0) ans.push_back(make_pair(x, y - 1));
  if (y < n - 1) ans.push_back(make_pair(x, y + 1));
  return ans;
}
void dfs(int x, int y) {
  auto n = neighbours(x, y);
  for (auto it : n) {
    int d1 = d[x][y];
    if (in[x][y]) d1++;
    if (d1 < d[it.first][it.second]) {
      d[it.first][it.second] = d1;
      dfs(it.first, it.second);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  vector<pair<int, int>> a(n * n);
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    t--;
    a[i] = make_pair(t / n, t % n);
  }
  d = vector<vector<int>>(n, vector<int>(n));
  in = vector<vector<bool>>(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) d[i][j] = min({i, n - i - 1, j, n - j - 1});
  ll res = 0;
  for (auto it : a) {
    res += d[it.first][it.second];
    in[it.first][it.second] = false;
    dfs(it.first, it.second);
  }
  cout << res << endl;
}
