#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
long long mpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
void yn(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
template <class Head>
void pt(Head&& head) {
  cout << head << endl;
  return;
}
template <class Head, class... Tail>
void pt(Head&& head, Tail&&... tail) {
  cout << head << " ";
  pt(forward<Tail>(tail)...);
}
template <class T>
void debug(T v) {
  for (int i = 0; i < (v.size()); ++i) cout << v[i] << " ";
  cout << endl;
}
template <class T>
void debug2(T v) {
  for (int i = 0; i < (v.size()); ++i) {
    for (int j = 0; j < (v[i].size()); ++j) cout << v[i][j] << " ";
    cout << endl;
  }
}
template <class T1, class T2>
long long bcount(T1 v, T2 a) {
  return (long long)distance((v).begin(),
                             upper_bound((v).begin(), (v).end(), a)) -
         (long long)distance((v).begin(),
                             lower_bound((v).begin(), (v).end(), a));
}
template <class T1, class T2>
inline bool chmin(T1& a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2>
inline bool chmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long sqN, N;
vector<vector<long long>> mp;
long long has(long long h, long long w) { return h * sqN + w; }
bool valid(long long h, long long w) {
  return (0 <= h && h < sqN && 0 <= w && w < sqN);
}
int main() {
  cin >> sqN;
  N = sqN * sqN;
  mp.resize(sqN, vector<long long>(sqN));
  vector<long long> p(N);
  for (int i = 0; i < (N); ++i) cin >> p[i];
  for (int i = 0; i < (N); ++i) --p[i];
  for (int i = 0; i < (sqN); ++i)
    for (int j = 0; j < (sqN); ++j) {
      mp[i][j] = i;
      chmin(mp[i][j], j);
      chmin(mp[i][j], sqN - i - 1);
      chmin(mp[i][j], sqN - j - 1);
    }
  long long ans = 0;
  for (int i = 0; i < (N); ++i) {
    long long now = p[i];
    queue<pair<long long, long long>> que;
    ans += mp[now / sqN][now % sqN];
    que.push(pair<long long, long long>(now, mp[now / sqN][now % sqN]));
    while (!que.empty()) {
      pair<long long, long long> cu = que.front();
      que.pop();
      long long y = cu.first / sqN;
      long long x = cu.first % sqN;
      if (valid(y + 1, x))
        if (chmin(mp[y + 1][x], cu.second))
          que.push(pair<long long, long long>(has(y + 1, x), cu.second + 1));
      if (valid(y - 1, x))
        if (chmin(mp[y - 1][x], cu.second))
          que.push(pair<long long, long long>(has(y - 1, x), cu.second + 1));
      if (valid(y, x + 1))
        if (chmin(mp[y][x + 1], cu.second))
          que.push(pair<long long, long long>(has(y, x + 1), cu.second + 1));
      if (valid(y, x - 1))
        if (chmin(mp[y][x - 1], cu.second))
          que.push(pair<long long, long long>(has(y, x - 1), cu.second + 1));
    }
    mp[now / sqN][now % sqN] = 0;
  }
  pt(ans);
}
