#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
using Graph = vector<vector<int> >;
const int INF = 100005;
const int MAX_K = 55;
const int MAX_W = 200005;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
unsigned gcd(unsigned a, unsigned b) {
  while (1) {
    if (a < b) swap(a, b);
    if (!b) break;
    a %= b;
  }
  return a;
}
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct mint {
  ll x;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint& operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int n;
ll ans = 0;
vector<vector<int> > score(500, vector<int>(500));
vector<vector<int> > ex(500, vector<int>(500, 1));
bool visited[500][500];
void bfs(int x, int y) {
  int karians = 1000;
  deque<P> que;
  score[x][y] = 0;
  que.push_back(make_pair(x, y));
  while (!que.empty()) {
    P pos = que.front();
    que.pop_front();
    int w = pos.first;
    int h = pos.second;
    if (w == 0 || w == n - 1 || h == 0 || h == n - 1) {
      karians = min(karians, score[w][h]);
      if (karians == 0) return;
      continue;
    }
    if (score[w][h] >= karians) continue;
    for (int dir = 0; dir < (4); ++dir) {
      int nw = w + dx[dir];
      int nh = h + dy[dir];
      if (nh < 0 || nh > n - 1 || nw < 0 || nw > n - 1) {
        continue;
      }
      if ((score[nw][nh] > (score[w][h] + 1)) && ex[nw][nh] == 1) {
        que.push_back(make_pair(nw, nh));
        score[nw][nh] = score[w][h] + 1;
      }
      if (score[nw][nh] > score[w][h] && ex[nw][nh] == 0) {
        que.push_front(make_pair(nw, nh));
        score[nw][nh] = score[w][h];
      }
    }
  }
}
int main() {
  cin >> n;
  ll ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    score = vector<vector<int> >(500, vector<int>(500, 500));
    int num;
    cin >> num;
    int sx, sy;
    sx = (num - 1) % n;
    sy = (num - 1) / n;
    int minroot = 500;
    if (sx == 0 || sx == n - 1 || sy == 0 || sy == n - 1)
      minroot = 0;
    else
      bfs(sx, sy);
    for (int i = 0; i < (n); ++i) {
      minroot = min(minroot, score[0][i]);
      minroot = min(minroot, score[n - 1][i]);
      minroot = min(minroot, score[i][0]);
      minroot = min(minroot, score[i][n - 1]);
    }
    ans += minroot;
    ex[sx][sy] = 0;
  }
  cout << ans << endl;
}
