#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using vect = vector<double>;
using Graph = vector<vector<ll>>;
ll ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
template <typename T>
T lcm(T x, T y) {
  T z = __gcd(x, y);
  return x * y / z;
}
template <typename T>
bool primejudge(T n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  double sqrtn = sqrt(n);
  for (T i = 3; i < sqrtn + 1; i++) {
    if (n % i == 0) {
      return false;
    }
    i++;
  }
  return true;
}
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
ll N;
vector<string> field;
ll bfs(ll sx, ll sy) {
  vector<vector<ll>> dist(N, vector<ll>(N));
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      dist[i][j] = 1e9;
    }
  }
  dist[sx][sy] = 0;
  deque<pair<ll, ll>> que;
  que.push_back({sx, sy});
  while (!que.empty()) {
    pair<ll, ll> p = que.front();
    que.pop_front();
    ll x = p.first, y = p.second;
    for (ll d = 0; d < 4; d++) {
      ll nx = x + dx[d], ny = y + dy[d];
      if (nx < 0 || nx >= N || ny < 0 || ny >= N) {
        return dist[x][y];
      }
      ll d2 = dist[x][y];
      if (field[nx][ny] == '#') d2++;
      if (dist[nx][ny] > d2) {
        dist[nx][ny] = d2;
        if (field[nx][ny] == '#')
          que.push_back({nx, ny});
        else
          que.push_front({nx, ny});
      }
    }
  }
}
int main() {
  cout << fixed << setprecision(30);
  cin >> N;
  vec P(N * N);
  for (ll i = 0; i < N * N; i++) cin >> P[i];
  string flat;
  for (ll i = 0; i < N; i++) flat += '#';
  field.resize(N);
  for (ll i = 0; i < N; i++) field[i] = flat;
  ll ans = 0;
  for (ll i = 0; i < N * N; i++) {
    P[i]--;
    ll sx = P[i] % N;
    P[i] /= N;
    ll sy = P[i];
    field[sx][sy] = '.';
    if (sx == 0 || sx == N - 1 || sy == 0 || sy == N - 1) continue;
    ans += bfs(sx, sy);
  }
  cout << ans << endl;
  return 0;
}
