#include <bits/stdc++.h>
using namespace std;
inline long long int maxim(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
inline long long int minim(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int Extended_gcd(long long int a, long long int b, long long int& x,
                           long long int& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  } else {
    long long int x1, y1;
    long long int g = Extended_gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return g;
  }
}
bool prime[1000010];
void SieveOfEratosthenes() {
  memset(prime, true, sizeof(prime));
  prime[1] = false;
  for (int p = 2; p * p <= 1000001; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p) prime[i] = false;
    }
  }
}
long long int binpow(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
long long int modpow(long long int a, long long int b, long long int x) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % x;
    a = (a * a) % x;
    b >>= 1;
  }
  return res;
}
int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    int q = a / m;
    int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long int lpf[1000001] = {};
void precompute() {
  for (long long int i = (2), _b = (1000000); i <= _b; i++) {
    if (!lpf[i]) {
      for (long long int j = i; j <= 1000000; j += i) {
        if (!lpf[j]) lpf[j] = i;
      }
    }
  }
}
vector<long long int> adj[1000010];
void adj_former(long long int n) {
  for (long long int o = 0, _n = (n - 1); o < _n; o++) {
    long long int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
}
long long int func(long long int x) { return (5 * x * (x + 1)) / 2; }
long long int bs(long long int low, long long int high, long long int target) {
  while (low + 3 < high) {
    long long int mid = (low + high) / 2;
    if (func(mid) <= target) {
      low = mid;
    } else {
      high = mid - 1;
    }
  }
  for (long long int o = (high), _b = (low); o >= _b; o--) {
    if (func(o) <= target) return o;
  }
  return -1;
}
long long int arr[1000010] = {0};
long long int brr[1000010] = {0};
long long int crr[1000010] = {0};
long long int north[510][510];
long long int east[510][510];
long long int west[510][510];
long long int south[510][510];
map<pair<long long int, long long int>, long long int> m;
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  cout << setprecision(10) << fixed;
  long long int n;
  cin >> n;
  for (long long int o = (1), _b = (n); o <= _b; o++) {
    for (long long int p = (1), _b = (n); p <= _b; p++) {
      north[o][p] = o - 1;
    }
  }
  for (long long int o = (1), _b = (n); o <= _b; o++) {
    for (long long int p = (1), _b = (n); p <= _b; p++) {
      south[o][p] = n - o;
    }
  }
  for (long long int o = (1), _b = (n); o <= _b; o++) {
    for (long long int p = (1), _b = (n); p <= _b; p++) {
      west[o][p] = p - 1;
    }
  }
  for (long long int o = (1), _b = (n); o <= _b; o++) {
    for (long long int p = (1), _b = (n); p <= _b; p++) {
      east[o][p] = n - p;
    }
  }
  vector<pair<long long int, char> > temp;
  long long int total = 0;
  for (long long int z = 0, _n = (n * n); z < _n; z++) {
    long long int x;
    cin >> x;
    long long int o = (x / n) + 1;
    long long int p = x % n;
    if (p == 0) {
      p = n;
      o--;
    }
    temp.clear();
    temp.push_back(make_pair(north[o][p], 'n'));
    temp.push_back(make_pair(east[o][p], 'e'));
    temp.push_back(make_pair(west[o][p], 'w'));
    temp.push_back(make_pair(south[o][p], 's'));
    sort(temp.begin(), temp.end());
    total += temp[0].first;
    for (long long int x = (o + 1), _b = (n); x <= _b; x++) {
      north[x][p]--;
    }
    for (long long int x = (1), _b = (o - 1); x <= _b; x++) {
      south[x][p]--;
    }
    for (long long int y = (p + 1), _b = (n); y <= _b; y++) {
      west[o][y]--;
    }
    for (long long int y = (1), _b = (p - 1); y <= _b; y++) {
      east[o][y]--;
    }
  }
  cout << total << "\n";
  return 0;
}
