#include <bits/stdc++.h>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
template <ostream& out = cout, class... T>
void printde(T&&...) {}
template <ostream& out = cout, class... T>
void printdbe(T&&...) {}
template <ostream& out = cout, class... T>
void printdwe(T&&...) {}
template <ostream& out = cout, class... T>
void printdu(T&&...){};
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator>>(istream& in, V<E>& v) {
  for (auto& e : v) {
    in >> e;
  }
  return in;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> ps(n * n);
  cin >> ps;
  bitset<512 * 512> board;
  board = ~board;
  int ans = 0;
  int cnt = 0;
  vector<int> vs[2];
  bitset<512 * 512> visited;
  for (auto& p : ps) {
    p--;
    p = p / n * 512 + p % n;
    board[p] = 0;
    vs[0].clear();
    vs[1].clear();
    vs[0].emplace_back(p);
    int mini = M;
    visited.reset();
    int cost = 0;
    while (1) {
      cnt++;
      if (vs[0].empty()) {
        vs[0].swap(vs[1]);
        cost++;
      }
      auto pa = vs[0].back();
      vs[0].pop_back();
      int pos = pa;
      if (visited[pos]) {
        continue;
      }
      visited[pos] = 1;
      int y = pos >> 9, x = pos & 511;
      if (y == 0 || y == n - 1 || x == 0 || x == n - 1) {
        mini = cost;
        break;
      }
      vs[board[pos + 1]].emplace_back(pos + 1);
      vs[board[pos - 1]].emplace_back(pos - 1);
      vs[board[pos + 512]].emplace_back(pos + 512);
      vs[board[pos - 512]].emplace_back(pos - 512);
    }
    ans += mini;
  }
  printde(cnt);
  cout << ans << '\n';
}
