#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
vvi grid;
int n;
int bfs(int s) {
  int y = s / n;
  int x = s % n;
  grid[y][x] = 0;
  vvi d(n, vi(n, -1));
  d[y][x] = 0;
  using PP = pair<int, P>;
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.emplace(0, P(y, x));
  int res = 1001001001;
  if (y == 0 || x == 0 || y == n - 1 || x == n - 1) return 0;
  while (!q.empty()) {
    int vy = q.top().second.first, vx = q.top().second.second;
    q.pop();
    for (int i = 0; i < (int)(4); i++) {
      int ny = vy + dy[i], nx = vx + dx[i];
      if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
      if (d[ny][nx] >= 0) continue;
      d[ny][nx] = d[vy][vx] + grid[ny][nx];
      if (ny == 0 || nx == 0 || ny == n - 1 || nx == n - 1)
        chmin(res, d[ny][nx]);
      q.emplace(d[ny][nx], P(ny, nx));
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vi p(n * n);
  for (int i = 0; i < (int)(n * n); i++) cin >> p[i];
  grid.resize(n, vi(n, 1));
  ll ans = 0;
  for (int i = 0; i < (int)(n * n); i++) ans += bfs(--p[i]);
  cout << ans << endl;
}
