#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
  cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
  pr(t);
  pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
  pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
  pr("{");
  bool fst = 1;
  for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
  pr("}");
}
void print() { pr("\n"); }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
  pr(t);
  if (sizeof...(ts)) pr(" ");
  print(ts...);
}
}  // namespace output
using namespace output;
template <class T>
struct Seg {
  const T ID = 0;
  T combine(T a, T b) { return a + b; }
  int n;
  vector<T> seg;
  void init(int _n) {
    n = _n;
    seg.assign(2 * n, ID);
  }
  void update(int p, T value) {
    seg[p += n] = value;
    for (p /= 2; p; p /= 2) seg[p] = combine(seg[2 * p], seg[2 * p + 1]);
  }
  T query(int l, int r) {
    T ra = ID, rb = ID;
    for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
      if (l & 1) ra = combine(ra, seg[l++]);
      if (r & 1) rb = combine(seg[--r], rb);
    }
    return combine(ra, rb);
  }
};
int N;
vector<pii> solve(vi row) {
  vector<pii> res(N);
  Seg<int> seg;
  seg.init(N);
  vector<pii> newarr(N);
  for (int i = (0); i < (N); ++i) newarr[i] = {row[i], i};
  sort(newarr.rbegin(), newarr.rend());
  for (pii data : newarr) {
    int i = data.second;
    int onl = seg.query(0, i);
    int onr = seg.query(i, N - 1);
    res[i] = {onl, onr};
    seg.update(i, 1);
  }
  return res;
}
pair<vector<vi>, vector<vi>> calc(vector<vi> grid) {
  vector<vi> d1(N, vi(N));
  vector<vi> d2(N, vi(N));
  for (int i = (0); i < (N); ++i) {
    vi row = grid[i];
    vector<pii> res = solve(row);
    for (int j = (0); j < (N); ++j) {
      d1[i][j] = res[j].first;
      d2[i][j] = res[j].second;
    }
  }
  return {d1, d2};
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  vector<vi> arr1(N, vi(N));
  vector<vi> arr2(N, vi(N));
  for (int i = (0); i < (N * N); ++i) {
    int x;
    cin >> x;
    --x;
    arr1[x / N][x % N] = i;
  }
  for (int i = (0); i < (N); ++i)
    for (int j = (0); j < (N); ++j) {
      arr2[i][j] = arr1[j][i];
    }
  pair<vector<vi>, vector<vi>> data1 = calc(arr1);
  pair<vector<vi>, vector<vi>> data2 = calc(arr2);
  int ans = 0;
  vector<vi> rrr(N, vi(N));
  for (int i = (0); i < (N); ++i)
    for (int j = (0); j < (N); ++j) {
      rrr[i][j] = min(min(data1.first[i][j], data1.second[i][j]),
                      min(data2.first[j][i], data2.second[j][i]));
      ans += min(min(data1.first[i][j], data1.second[i][j]),
                 min(data2.first[j][i], data2.second[j][i]));
    }
  print(ans);
}
