#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int LARGE_INT = 1e9 + 100;
const int INF = 2e9 + 100;
const long long INF_LL = (long long)INF * (long long)INF;
const int MOD = 1e9 + 7;
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}
void print(vector<vector<int>> m, int n) {
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (n); ++j) {
      cerr << m[i][j] << " ";
    }
    cerr << endl;
  }
  cerr << endl;
}
void Main() {
  int n;
  cin >> n;
  vector<vector<int>> p;
  for (int i = (0); i < (n * n); ++i) {
    int a;
    cin >> a;
    a--;
    int x = a / n;
    int y = a % n;
    p.push_back({x, y});
  }
  vector<vector<int>> m(n);
  priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
  for (int i = (0); i < (n); ++i) {
    m[i].resize(n, INF);
    if (i == 0 || i == n - 1) {
      for (int j = (0); j < (n); ++j) {
        pq.push({1, i, j});
      }
    } else {
      pq.push({1, i, 0});
      pq.push({1, i, n - 1});
    }
  }
  while (!pq.empty()) {
    int d = pq.top()[0];
    int x = pq.top()[1];
    int y = pq.top()[2];
    pq.pop();
    if (m[x][y] <= d) {
      continue;
    }
    m[x][y] = d;
    vector<int> dx = {1, -1, 0, 0};
    vector<int> dy = {0, 0, 1, -1};
    for (int i = (0); i < (4); ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      int nd = d + 1;
      if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
        continue;
      }
      if (m[nx][ny] <= d) {
        continue;
      }
      pq.push({nd, nx, ny});
    }
  }
  print(m, n);
  vector<vector<bool>> f(n);
  for (int i = (0); i < (n); ++i) {
    f[i].resize(n, false);
  }
  long long result = 0;
  for (auto z : p) {
    int x = z[0];
    int y = z[1];
    result += m[x][y] - 1;
    f[x][y] = true;
    pq.push({m[x][y] - 1, x, y});
    while (!pq.empty()) {
      int dd = pq.top()[0];
      int xx = pq.top()[1];
      int yy = pq.top()[2];
      pq.pop();
      if (m[xx][yy] <= dd) {
        continue;
      }
      m[xx][yy] = dd;
      vector<int> dx = {1, -1, 0, 0};
      vector<int> dy = {0, 0, 1, -1};
      for (int i = (0); i < (4); ++i) {
        int nx = xx + dx[i];
        int ny = yy + dy[i];
        int nd = dd + 1;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
          continue;
        }
        if (f[nx][ny]) {
          if (m[nx][ny] <= dd) {
            continue;
          }
          pq.push({dd, nx, ny});
        } else {
          if (m[nx][ny] <= nd) {
            continue;
          }
          pq.push({nd, nx, ny});
        }
      }
    }
  }
  cout << result << endl;
  return;
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
