#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int EPS = 1e-8;
const int MOD = 1000000007;
const long double PI = 3.14159265359;
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = {-1, 0, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
template <typename T>
T lcm(T a, T b) {
  return (a * b) / __gcd(a, b);
}
inline bool grid_inside(int x, int y, int n, int m) {
  return x >= 1 && x <= n && y >= 1 && y <= m;
}
template <typename ForwardIterator, typename T>
T first_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::lower_bound(first, last, value);
  return (it != last) ? (it - first) : -1;
}
template <typename ForwardIterator, typename T>
T last_val(ForwardIterator first, ForwardIterator last, T value) {
  auto it = std::upper_bound(first, last, value);
  return ((it != last) ? ((it - first) - 1) : -1);
}
template <typename T>
T Dis(T x1, T y1, T x2, T y2) {
  return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
  return atan((double)(y1 - y2) / (double)(x1 - x2));
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
template <class T, class X>
inline T togglebit(T a, X i) {
  T t = 1;
  return (a ^ (t << i));
}
bool compare(const pair<int, int> &i, const pair<int, int> &j) {
  return i.first < j.first;
}
int doubleCompare(double x, double y) {
  if (fabs(x - y) <= EPS) return 0;
  if (x < y) return -1;
  return 1;
}
int LSOne(int x) { return ((x) & (-x)); }
int countSetBits(int n) {
  int count = 0;
  while (n > 0) {
    count += (n & 1);
    n = n >> 1;
  }
  return count;
}
int getPosBit(int n, int k) { return (n & (1 << k)) != 0 ? 1 : 0; }
void setPosBit(int &n, int k) { n = n | (1 << k); }
long long int fast_power(int a, int x) {
  if (x == 0)
    return 1;
  else if (x == 1)
    return a;
  else {
    long long int R = fast_power(a, x >> 1);
    if (!(x & 1))
      return R * R;
    else
      return R * a * R;
  }
}
long long int fast_power_unlocked(long long int a, long long int n,
                                  long long int mod) {
  long long int res = 1;
  while (n > 0) {
    if (n & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    n = n >> 1;
  }
  return res;
}
long long int modInverse(long long int A, long long int M) {
  return fast_power_unlocked(A, M - 2, M);
}
bool checkPrime(int n) {
  int cnt = 0;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        cnt++;
      else
        cnt += 2;
    }
  }
  if (cnt == 2)
    return true;
  else
    return false;
}
const int MAX_N = 501;
int c[MAX_N][MAX_N];
int n;
int vis[MAX_N][MAX_N];
pair<int, bool> min_dist;
pair<int, bool> check(int x, int y) {
  vis[x][y] = 1;
  min_dist.first = min(min_dist.first, min({x, y, n - 1 - x, n - 1 - y}));
  if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
    min_dist.second = true;
    return min_dist;
  }
  if (c[x - 1][y] == 0 && !vis[x - 1][y])
    min_dist =
        make_pair(min(min_dist.first, check(x - 1, y).first), min_dist.second);
  if (c[x][y + 1] == 0 && !vis[x][y + 1])
    min_dist =
        make_pair(min(min_dist.first, check(x, y + 1).first), min_dist.second);
  if (c[x + 1][y] == 0 && !vis[x + 1][y])
    min_dist =
        make_pair(min(min_dist.first, check(x + 1, y).first), min_dist.second);
  if (c[x][y - 1] == 0 && !vis[x][y - 1])
    min_dist =
        make_pair(min(min_dist.first, check(x, y - 1).first), min_dist.second);
  return min_dist;
}
void solve() {
  cin >> n;
  int sz = n * n;
  int f = 1;
  int cnt = 0, val;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      c[i][j] = f;
      f++;
    }
  }
  for (int i = 0; i < sz; i++) {
    cin >> val;
    int x = ((val - 1) / n);
    int y = ((val - 1) % n);
    min_dist = make_pair(10000, false);
    memset(vis, 0, sizeof(vis));
    auto it = check(x, y);
    if (it.second) {
      c[x][y] = 0;
    } else {
      cnt += it.first;
      c[x][y] = 0;
    }
  }
  cout << cnt << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ;
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true);
       blockTime.second;
       fprintf(stderr, "%s: %ld ms\n", "Time Taken",
               chrono::duration_cast<chrono::milliseconds>(
                   chrono::high_resolution_clock::now() - blockTime.first)
                   .count()),
            fflush(stderr), blockTime.second = false) {
    solve();
  }
}
