#include <bits/stdc++.h>
using namespace std;
int64_t seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline int64_t rnd(int64_t l = 0, int64_t r = INT_MAX) {
  return uniform_int_distribution<int64_t>(l, r)(rng);
}
template <typename T, typename G>
ostream& operator<<(ostream& out, const pair<T, G>& a) {
  return out << "( " << a.first << ", " << a.second << ")";
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
  out << "[ ";
  for (const auto& i : a) out << i << ", ";
  return out << "]" << endl;
}
template <typename T>
ostream& operator<<(ostream& out, const set<T>& a) {
  out << "{ ";
  for (const auto& i : a) out << i << ", ";
  return out << "}" << endl;
}
template <typename T, typename G>
ostream& operator<<(ostream& out, const map<T, G>& a) {
  out << "< ";
  for (const auto& i : a) out << i << ", ";
  return out << ">" << endl;
}
template <typename T, size_t N>
typename enable_if<!is_same<typename remove_cv<T>::type, char>::value,
                   ostream&>::type
operator<<(ostream& out, T (&a)[N]) {
  out << "[ ";
  for (size_t i = 0; i < N; ++i) out << a[i] << ", ";
  out << "]" << endl;
  return out;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << (H) << " ";
  debug_out(T...);
}
template <typename T>
class fenwick {
 private:
  vector<T> bit;
  int64_t n;

 public:
  fenwick() {
    n = 100005;
    bit.assign(n, {0});
  }
  fenwick(int64_t size) {
    n = size;
    bit.assign(n, {0});
  }
  void update(int64_t id, T z) {
    while (id <= n) {
      bit[id] = bit[id] + z;
      id += (id & (-id));
    }
  }
  T query(int64_t id) {
    T ans = 0;
    while (id > 0) {
      ans = ans + bit[id];
      id -= (id & (-id));
    }
    return ans;
  }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int64_t n, i, j;
  cin >> n;
  vector<vector<int64_t> > a(n + 1, vector<int64_t>(n + 1, 1));
  vector<fenwick<int64_t> > rows(n + 1, fenwick<int64_t>(n + 1)),
      cols(n + 1, fenwick<int64_t>(n + 1));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      rows[i].update(j, 1);
      cols[j].update(i, 1);
    }
  }
  int64_t ans = 0;
  for (i = 0; i < n * n; i++) {
    int64_t first, r, c;
    cin >> first;
    first--;
    r = first / n + 1;
    c = first % n + 1;
    24;
    rows[r].update(c, -1);
    cols[c].update(r, -1);
    a[r][c] = 0;
    int64_t nmin = n * n, csum = cols[c].query(r), rsum = rows[r].query(c);
    for (j = 1; j <= n; j++) {
      int64_t lleft = rows[j].query(c - 1), rleft = lleft + a[j][c],
              right = rows[j].query(n);
      int64_t rem =
          (j <= r ? csum - cols[c].query(j - 1) : cols[c].query(j) - csum);
      24;
      24;
      nmin = min(nmin, rem + min(lleft, right - rleft));
    }
    for (j = 1; j <= n; j++) {
      int64_t lleft = cols[j].query(r - 1), rleft = lleft + a[r][j],
              right = cols[j].query(n);
      int64_t rem =
          (j <= c ? rsum - rows[r].query(j - 1) : rows[r].query(j) - rsum);
      24;
      24;
      nmin = min(nmin, rem + min(lleft, right - rleft));
    }
    24;
    ans += nmin;
  }
  cout << ans << "\n";
  return 0;
}
