#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O2")
#pragma GCC optimization("unroll-loops")
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
  cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
  pr(t);
  pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
  pr("{", x.f, ", ", x.s, "}");
}
template <class T>
void pr(const T& x) {
  pr("{");
  bool fst = 1;
  for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
  pr("}");
}
void print() { pr("\n"); }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
  pr(t);
  if (sizeof...(ts)) pr(" ");
  print(ts...);
}
}  // namespace output
using namespace output;
bool graph[500][500][250];
bool cover[500][500];
int cur[500][500];
int N;
vector<pii> deltas = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {0, 0}};
void dfs(int i, int j, int k) {
  graph[i][j][k] = true;
  cur[i][j] = min(cur[i][j], k);
  for (pii d : deltas) {
    int ni = d.first + i;
    int nj = d.second + j;
    int nk = k + cover[i][j];
    if (ni < 0 || ni >= N || nj < 0 || nj >= N || nk >= N || nk >= 250)
      continue;
    if (graph[ni][nj][nk]) continue;
    dfs(ni, nj, nk);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  for (int i = (0); i < (500); ++i)
    for (int j = (0); j < (500); ++j) {
      cover[i][j] = true;
      for (int k = (0); k < (250); ++k) graph[i][j][k] = false;
    }
  for (int i = (0); i < (N); ++i)
    for (int j = (0); j < (N); ++j) {
      cur[i][j] = min(min(i, j), min(N - i - 1, N - j - 1));
      if (i < 250) graph[i][j][i] = true;
      if (j < 250) graph[i][j][j] = true;
      if (N - i - 1 < 250) graph[i][j][N - i - 1] = true;
      if (N - j - 1 < 250) graph[i][j][N - j - 1] = true;
    }
  int ans = 0;
  for (int t = (0); t < (N * N); ++t) {
    int x;
    cin >> x;
    --x;
    int i = x / N;
    int j = x % N;
    int best = cur[i][j];
    cover[i][j] = false;
    ans += best;
    dfs(i, j, best);
  }
  print(ans);
}
