#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
void dfs(ll N, ll x, ll y, vvll &cost, const vvb &m) {
  ll t = cost[x][y];
  if (m[x][y]) {
    t++;
  }
  if (0 <= x - 1) {
    if (cost[x - 1][y] > t) {
      cost[x - 1][y] = t;
      dfs(N, x - 1, y, cost, m);
    }
  }
  if (0 <= y - 1) {
    if (cost[x][y - 1] > t) {
      cost[x][y - 1] = t;
      dfs(N, x, y - 1, cost, m);
    }
  }
  if (x + 1 < N) {
    if (cost[x + 1][y] > t) {
      cost[x + 1][y] = t;
      dfs(N, x + 1, y, cost, m);
    }
  }
  if (y + 1 < N) {
    if (cost[x][y + 1] > t) {
      cost[x][y + 1] = t;
      dfs(N, x, y + 1, cost, m);
    }
  }
}
int main() {
  ll N;
  cin >> N;
  vll p(N * N);
  for (ll i = 0; i < N * N; i++) {
    cin >> p[i];
  }
  vvb map(N, vb(N, true));
  vvll cost(N, vll(N));
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      cost[i][j] = min(min(i, j), min(N - 1 - i, N - 1 - j));
    }
  }
  ll c = 0;
  for (ll i = 0; i < N * N; i++) {
    ll k = p[i];
    ll m = k % N;
    ll x, y;
    if (m == 0) {
      x = N - 1;
      y = k / N - 1;
    } else {
      x = k % N - 1;
      y = k / N;
    }
    c += cost[x][y];
    map[x][y] = false;
    ll p = 1000000;
    bool t = true;
    if (0 <= x - 1) {
      ll d = cost[x - 1][y];
      if (map[x - 1][y]) {
        d++;
      }
      p = min(p, d);
    }
    if (0 <= y - 1) {
      ll e = cost[x][y - 1];
      if (map[x][y - 1]) {
        e++;
      }
      p = min(p, e);
    }
    if (x + 1 < N) {
      ll f = cost[x + 1][y];
      if (map[x + 1][y]) {
        f++;
      }
      p = min(p, f);
    }
    if (y + 1 < N) {
      ll l = cost[x][y + 1];
      if (map[x][y + 1]) {
        l++;
      }
      p = min(p, l);
    }
    bool s = false, g = false, r = false, n = false;
    if (0 <= x - 1) {
      if (cost[x - 1][y] > p) {
        s = true;
        cost[x - 1][y] = p;
      }
    }
    if (0 <= y - 1) {
      if (cost[x][y - 1] > p) {
        g = true;
        cost[x][y - 1] = p;
      }
    }
    if (x + 1 < N) {
      if (cost[x + 1][y] > p) {
        r = true;
        cost[x + 1][y] = p;
      }
    }
    if (y + 1 < N) {
      if (cost[x][y + 1] > p) {
        n = true;
        cost[x][y + 1] = p + 1;
      }
    }
    if (s) {
      dfs(N, x - 1, y, cost, map);
    }
    if (g) {
      dfs(N, x, y - 1, cost, map);
    }
    if (r) {
      dfs(N, x + 1, y, cost, map);
    }
    if (n) {
      dfs(N, x, y + 1, cost, map);
    }
  }
  cout << (c);
}
