#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using minPQ = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
inline bool inside(int y, int x, int H, int W) {
  return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
  int x;
  cin >> x;
  return x;
}
inline ll IN() {
  ll x;
  cin >> x;
  return x;
}
inline vs split(const string& t, char c) {
  vs v;
  stringstream s(t);
  string b;
  while (getline(s, b, c)) v.emplace_back(b);
  return v;
}
template <typename T>
inline bool chmin(T& a, const T& b) {
  if (a > b) a = b;
  return a > b;
}
template <typename T>
inline bool chmax(T& a, const T& b) {
  if (a < b) a = b;
  return a < b;
}
template <typename T, typename S>
inline void print(const pair<T, S>& p) {
  cout << p.first << " " << p.second << endl;
}
template <typename T>
inline void print(const T& x) {
  cout << x << endl;
}
template <typename T, typename S>
inline void print(const vector<pair<T, S>>& v) {
  for (auto&& p : v) print(p);
}
template <typename T>
inline void print(const vector<T>& v, string s = " ") {
  for (ll i = (0); i < (ll)(v.size()); ++i)
    cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");
}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {
  cout << (ok ? yes : no) << endl;
}
bool seated[500][500];
int dp[500][500];
signed main() {
  int N;
  cin >> N;
  vi v(N * N);
  for (ll i = (0); i < (ll)(N * N); ++i) cin >> v[i];
  int pos, x, y, nx, ny;
  int ans = 0;
  int best;
  for (ll i = (0); i < (ll)(N); ++i)
    for (ll j = (0); j < (ll)(N); ++j) {
      dp[i][j] = 0;
    }
  for (ll pid = (N * N - 1); pid >= (ll)(0); --pid) {
    pos = v[pid];
    pos--;
    x = pos / N;
    y = pos % N;
    if (x == 0 || x == N - 1 || y == 0 || y == N - 1) {
      best = INF;
      for (ll t = (0); t < (ll)(4); ++t) {
        nx = x + dx[t];
        ny = y + dy[t];
        if (0 <= nx && nx < N && 0 <= ny && ny < N) {
          chmin(best, dp[nx][ny] + 1);
        }
      }
      dp[x][y] = best;
      continue;
    }
    best = INF;
    for (ll t = (0); t < (ll)(4); ++t) {
      nx = x + dx[t];
      ny = y + dy[t];
      if (0 <= nx && nx < N && 0 <= ny && ny < N) {
        chmin(best, dp[nx][ny]);
      }
    }
    ans += best;
    best = INF;
    for (ll t = (0); t < (ll)(4); ++t) {
      nx = x + dx[t];
      ny = y + dy[t];
      if (0 <= nx && nx < N && 0 <= ny && ny < N) {
        chmin(best, dp[nx][ny] + 1);
      }
    }
    dp[x][y] = best;
  }
  print(ans);
  return 0;
}
