#include <bits/stdc++.h>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream& operator<<(ostream& ost, const pair<T, U>& right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible& val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible& val) {
  for (int i = 0; i < M; ++i) Fill(dest[i], val);
}
template <class T>
T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream& Ignore(istream& ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
istream& operator>>(istream& ist, __int128& val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream& operator<<(ostream& ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
int N;
int dist[500][500];
int used[500][500];
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
void rec(int i, int j) {
  int nd = used[i][j] ? dist[i][j] : dist[i][j] + 1;
  for (LL k = (0), k_max_ = (4); k < k_max_; ++k) {
    int ni = i + di[k];
    int nj = j + dj[k];
    if (!Inside(ni, nj, N, N)) return;
    if (dist[ni][nj] > nd) {
      dist[ni][nj] = nd;
      rec(ni, nj);
    }
  }
}
int sub(int i, int j) {
  int ret = dist[i][j];
  used[i][j] = true;
  rec(i, j);
  return ret;
}
int main() {
  cin >> N;
  for (LL i = (0), i_max_ = (N); i < i_max_; ++i) {
    for (LL j = (0), j_max_ = (N); j < j_max_; ++j) {
      dist[i][j] = min({i, N - 1 - i, j, N - 1 - j});
    }
  }
  int sum = 0;
  for (LL z = (0), z_max_ = (N * N); z < z_max_; ++z) {
    int pos = Next<int>() - 1;
    int i = pos / N;
    int j = pos % N;
    sum += sub(i, j);
  }
  cout << sum << endl;
  return 0;
}
