#include <bits/stdc++.h>
using namespace std;
template <class T>
void mini(T& a, T b) {
  a = min(a, b);
}
template <class T>
void maxi(T& a, T b) {
  a = max(a, b);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  return out << "(" << a.first << ", " << a.second << ")";
}
template <class T>
ostream& operator<<(ostream& out, vector<T> a) {
  out << "(";
  for (auto& v : a) out << v << ", ";
  return out << ")";
}
template <class T>
ostream& operator<<(ostream& out, set<T> a) {
  out << "(";
  for (auto& v : a) out << v << ", ";
  return out << ")";
}
template <class L, class R>
ostream& operator<<(ostream& out, map<L, R> a) {
  out << "(";
  for (auto& v : a) out << v << ", ";
  return out << ")";
}
template <class T>
void out(T a) {
  cout << a << endl;
}
const int N = 505;
const int K = 1 << 9;
struct seg {
  int a[N];
  int t[2 * K];
  void build() {
    for (int i = 1; i < N; i++) t[i + K - 1] = a[i];
    for (int i = K - 1; i >= 1; i--) t[i] = t[i + i] + t[i + i + 1];
  }
  void upd(int id, int x) {
    id += K - 1;
    while (id) {
      t[id] += x;
      id /= 2;
    }
  }
  int get(int l, int r) {
    l += K - 1;
    r += K - 1;
    int res = 0;
    while (l <= r) {
      if (l % 2 == 1) res += t[l++];
      if (r % 2 == 0) res += t[r--];
      l /= 2;
      r /= 2;
    }
    return res;
  }
};
seg r[N];
seg c[N];
void Solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      r[i].a[j] = 1;
      c[j].a[i] = 1;
    }
  }
  for (int i = 1; i <= n; i++) r[i].build();
  for (int j = 1; j <= n; j++) c[j].build();
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int cur;
    cin >> cur;
    int x = (cur - 1) / n + 1;
    int y = cur % n;
    if (!y) y = n;
    int lef = r[x].get(1, y);
    int rig = r[x].get(y, n);
    int down = c[y].get(1, x);
    int up = c[y].get(x, n);
    int res = lef;
    mini(res, rig);
    mini(res, up);
    mini(res, down);
    ans += res - 1;
    r[x].upd(y, -1);
    c[y].upd(x, -1);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  Solve();
  return 0;
}
