#include <bits/stdc++.h>
using namespace std;

#define pb push_back

#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;

template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}

template<typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (const T& x : v)
    o << x << ",";
  return o << "]";
}

template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T>>& v) {
  o << "[";
  for (const vector<T>& x : v)
    o << x << ",\n";
  return o << "]";
}

const int INF = 1e18;
constexpr pii offs[] = {{-1,0},{0,1},{1,0},{0,-1}};

vvi d;
vvi occ;

void propdists(int i, int j) {
  for (const pii& off : offs) {
    int ii = i+off.first;
    int jj = j+off.second;
    if (ii<0 || ii>=d.size() || jj<0 || jj>=d.size())
      continue;
    if (d[ii][jj] > d[i][j] + occ[i][j]) {
      d[ii][jj] = d[i][j] + occ[i][j];
      propdists(ii,jj);
    }
  }
}

void initdists() {
  const int n = d.size();
  for (int i=0; i<n; ++i) {
    d[i][0] = 0;
    propdists(i,0);
    d[i][n-1] = 0;
    propdists(i,n-1);
  }
  for (int j=1; j<n-1; ++j) {
    d[0][j] = 0;
    propdists(0,j);
    d[n-1][j] = 0;
    propdists(n-1,j);
  }
}

void unitestuff(int i, int j) {
  for (const pii& off : offs) {
    int ii = i + off.first;
    int jj = j + off.second;
    if (ii<0 || ii>=d.size() || jj<0 || jj>=d.size())
      continue;
    if (d[ii][jj] > d[i][j]) {
      d[ii][jj] = d[i][j];
      propdists(ii,jj);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vi ps(n*n);
  for (int& pi : ps)
    cin >> pi;

  occ = vvi(n, vi(n, 1));
  d = vvi(n, vi(n, INF));
  initdists();

  int haters = 0;
  for (int pi : ps) {
    int i = (pi-1)/n;
    int j = (pi-1)%n;
    haters += d[i][j];
    occ[i][j] = 0;
    unitestuff(i,j);
  }
  cout << haters << endl;
}
