#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long INF = 1e18 + 18;
const long long MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
istream &operator>>(istream &i, vector<T> &v) {
  for (long long j = (0); j < (v.size()); j++) i >> v[j];
  return i;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1) s << "	";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < MOD);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);
  long long n;
  cin >> n;
  auto in = [&](long long y, long long x) {
    return 0 <= y && y < n && 0 <= x && x < n;
  };
  vector<long long> dx = {0, 1, 0, -1};
  vector<long long> dy = {1, 0, -1, 0};
  long long N = n * n;
  vector<long long> v(N);
  cin >> v;
  for (long long i = (0); i < (N); i++) v[i]--;
  vector<vector<long long>> g(n, vector<long long>(n));
  auto idx = [&](long long y, long long x) { return y * n + x; };
  auto to = [&](long long id) -> pair<long long, long long> {
    return make_pair(id / n, id % n);
  };
  reverse(v.begin(), v.end());
  vector<pair<long long, long long>> order;
  for (long long i = (0); i < (N); i++) {
    pair<long long, long long> p = to(v[i]);
    g[p.first][p.second] = i + 1;
    order.emplace_back(i + 1, v[i]);
  }
  sort(order.rbegin(), order.rend());
  ;
  long long ans = 0;
  vector<vector<long long>> dp(n, vector<long long>(n));
  for (long long i = (0); i < (n); i++) {
    for (long long j = (0); j < (n); j++) {
      long long x = min(i, n - 1 - i);
      long long y = min(j, n - 1 - j);
      dp[i][j] = min(x, y);
    }
  };
  vector<vector<long long>> udp(n, vector<long long>(n));
  vector<vector<long long>> ddp(n, vector<long long>(n));
  vector<vector<long long>> rdp(n, vector<long long>(n));
  vector<vector<long long>> ldp(n, vector<long long>(n));
  for (long long ord = (0); ord < (N); ord++) {
    pair<long long, long long> p = order[ord];
    long long y = to(p.second).first;
    long long x = to(p.second).second;
    ans += max(dp[y][x], 0LL);
    for (long long i = (0); i < (n); i++) {
      if (i > y) {
        udp[i][x]++;
      }
      if (i < y) {
        ddp[i][x]++;
      }
    }
    for (long long i = (0); i < (n); i++) {
      if (i > x) {
        ldp[y][i]++;
      }
      if (i < x) {
        rdp[y][i]++;
      }
    }
    for (long long i = (0); i < (n); i++) {
      long long tmpx = min(i - ldp[y][i], n - 1 - i - rdp[y][i]);
      long long tmpy = min(y - udp[y][i], n - 1 - y - ddp[y][i]);
      dp[y][i] = min(tmpx, tmpy);
    }
    for (long long i = (0); i < (n); i++) {
      long long tmpx = min(x - ldp[i][x], n - 1 - x - rdp[i][x]);
      long long tmpy = min(i - udp[i][x], n - 1 - i - ddp[i][x]);
      dp[i][x] = min(tmpx, tmpy);
    }
  }
  cout << ans << endl;
  return 0;
}
