#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar) {
  for (int i = 0; i < (ar.size() - 1); ++i) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar) {
  for (int i = 0; i < (ar.size()); ++i) {
    for (int j = 0; j < (ar[0].size() - 1); ++j) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S>
bool range(S a, S b, S x) {
  return (a <= x && x < b);
}
int main() {
  int n;
  cin >> n;
  vector<int> ar(n * n);
  for (int i = 0; i < (n * n); ++i) cin >> ar[i];
  vector<vector<int>> co(n, vector<int>(n, 0));
  vector<vector<bool>> iru(n, vector<bool>(n, true));
  for (int i = 0; i < ((n - 1) / 2); ++i) {
    for (int j = i + 1; j < (n - 1 - i); ++j) {
      for (int k = i + 1; k < (n - 1 - i); ++k) {
        co[j][k]++;
      }
    }
  }
  int ans = 0;
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  for (int i = 0; i < (n * n); ++i) {
    int x = (ar[i] - 1) / n;
    int y = (ar[i] - 1) % n;
    ans += co[x][y];
    iru[x][y] = false;
    if (co[x][y] != 0) {
      queue<P> q;
      for (int j = 0; j < (4); ++j) {
        if (range(0, n, x + dx[j]) && range(0, n, y + dy[j]) &&
            co[x + dx[j]][y + dy[j]] != -1) {
          q.push(P(x + dx[j], y + dy[j]));
        }
      }
      while (!q.empty()) {
        P p = q.front();
        q.pop();
        int ax = p.first;
        int ay = p.second;
        int me = co[ax][ay];
        int c = 1000;
        for (int j = 0; j < (4); ++j) {
          if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
              co[ax + dx[j]][ay + dy[j]] == -1) {
            c = min(c, -1);
          } else if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
                     iru[ax + dx[j]][ay + dy[j]]) {
            c = min(c, co[ax + dx[j]][ay + dy[j]]);
          } else if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
                     !iru[ax + dx[j]][ay + dy[j]]) {
            c = min(c, co[ax + dx[j]][ay + dy[j]] - 1);
          }
        }
        if (c + 1 < me) {
          co[ax][ay] = c + 1;
          for (int j = 0; j < (4); ++j) {
            if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
                co[ax + dx[j]][ay + dy[j]] != -1) {
              q.push(P(ax + dx[j], ay + dy[j]));
            }
          }
        }
      }
    }
    if (co[x][y] == 0) {
      co[x][y] = -1;
      queue<P> q;
      for (int j = 0; j < (4); ++j) {
        if (range(0, n, x + dx[j]) && range(0, n, y + dy[j]) &&
            co[x + dx[j]][y + dy[j]] != -1) {
          q.push(P(x + dx[j], y + dy[j]));
        }
      }
      while (!q.empty()) {
        P p = q.front();
        q.pop();
        int ax = p.first;
        int ay = p.second;
        int me = co[ax][ay];
        bool t = false;
        for (int j = 0; j < (4); ++j) {
          if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
              co[ax + dx[j]][ay + dy[j]] == -1) {
            t = true;
          }
        }
        if (!iru[ax][ay] && t) {
          co[ax][ay] = -1;
          for (int j = 0; j < (4); ++j) {
            if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
                co[ax + dx[j]][ay + dy[j]] != -1) {
              q.push(P(ax + dx[j], ay + dy[j]));
            }
          }
          continue;
        }
        int c = 1000;
        for (int j = 0; j < (4); ++j) {
          if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j])) {
            c = min(c, co[ax + dx[j]][ay + dy[j]]);
          }
        }
        if (c + 1 < me) {
          co[ax][ay] = c + 1;
          for (int j = 0; j < (4); ++j) {
            if (range(0, n, ax + dx[j]) && range(0, n, ay + dy[j]) &&
                co[ax + dx[j]][ay + dy[j]] != -1) {
              q.push(P(ax + dx[j], ay + dy[j]));
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
