#include <bits/stdc++.h>
using namespace std;
template <class A>
ostream& operator<<(ostream& out, const vector<A>& v) {
  out << "[";
  for (int ind = 0; ind < int(int(v.size())); ++ind) {
    if (ind) out << ", ";
    out << v[ind];
  }
  return out << "]";
}
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
  return out << "(" << p.x << ", " << p.y << ")";
}
const int INF = int(1e9);
const long long int INF64 = long long int(1e18);
const int MOD = int(1e9) + 7;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int N = 500 + 1;
int n;
int p[N * N];
int g[N][N];
bool read() {
  if (scanf("%d", &n) != 1) return false;
  for (int i = 0; i < int(n * n); ++i) {
    scanf("%d", &p[i]);
    --p[i];
  }
  for (int r = 0; r < int(n); ++r)
    for (int c = 0; c < int(n); ++c) {
      g[r][c] = r * n + c;
    }
  return true;
}
bool isOut(int r, int c) {
  if (0 <= r && r < n && 0 <= c && c < n) return false;
  return true;
}
int getMin(int pi) {
  int r = pi / n;
  int c = pi % n;
  int curMin = min({r, c, (n - 1) - r, (n - 1) - c});
  int dr[] = {-1, 0, 1, 0};
  int dc[] = {0, 1, 0, -1};
  queue<pair<pair<int, int>, int>> q;
  q.push(make_pair(make_pair(r, c), 0));
  bool vis[n][n];
  for (int r = 0; r < int(n); ++r)
    for (int c = 0; c < int(n); ++c) vis[r][c] = false;
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    vis[cur.first.first][cur.first.second] = true;
    for (int dir = 0; dir < int(4); ++dir) {
      int nr = cur.first.first + dr[dir];
      int nc = cur.first.second + dc[dir];
      if (cur.second >= curMin) continue;
      if (isOut(nr, nc)) {
        curMin = min(curMin, cur.second);
        continue;
      }
      if (!vis[nr][nc]) {
        q.push(make_pair(make_pair(nr, nc),
                         cur.second + (g[nr][nc] == -1 ? 0 : 1)));
      }
    }
  }
  return curMin;
}
void rem(int pi) {
  int r = pi / n;
  int c = pi % n;
  g[r][c] = -1;
}
void solve() {
  int cnt = 0;
  for (int pi : p) {
    int curMin = getMin(pi);
    cnt += curMin;
    rem(pi);
  }
  printf("%d\n", cnt);
}
int main() {
  cerr.precision(15);
  cout.precision(15);
  cerr << fixed;
  cout << fixed;
  while (read()) {
    solve();
  }
  return 0;
}
