#include <bits/stdc++.h>
using namespace std;
template <typename T>
static ostream& operator<<(ostream& stream, const vector<T>& t);
template <typename T>
static istream& read(T, T, istream& = cin);
template <typename T>
static istream& operator>>(istream& stream, vector<T>& t) {
  return read((t).begin(), (t).end(), stream);
}
template <typename T>
static istream& read(T b, T e, istream& stream) {
  for (T it = b; it != e; ++it) stream >> *it;
  return stream;
}
struct _print {
  string sep, end;
  bool space;
  ostream& stream;
  _print(string _sep = " ", string _end = "\n", ostream& _stream = cout)
      : sep(_sep), end(_end), space(false), stream(_stream) {}
  ~_print() { stream << end; }
  template <typename T>
  _print& operator,(const T& t) {
    if (space) stream << sep;
    space = true;
    stream << t;
    return *this;
  }
};
int f(int r, int c, vector<vector<bool> >& occ) {
  int ans = r;
  {
    int cur = 0;
    for (int i = 0; i < (r); ++i) {
      cur += occ[i][c];
    }
    ans = min(ans, cur);
  }
  {
    int cur = 0;
    for (int i = (r + 1); i <= (long long(occ.size()) - 1); ++i) {
      cur += occ[i][c];
    }
    ans = min(ans, cur);
  }
  {
    int cur = 0;
    for (int j = 0; j < (c); ++j) {
      cur += occ[r][j];
    }
    ans = min(ans, cur);
  }
  {
    int cur = 0;
    for (int j = (c + 1); j <= (long long(occ[0].size()) - 1); ++j) {
      cur += occ[r][j];
    }
    ans = min(ans, cur);
  }
  occ[r][c] = 0;
  return ans;
}
void __() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  cin >> p;
  for (int& x : p) --x;
  vector<vector<bool> > occ(n, vector<bool>(n, 1));
  int ans = 0;
  for (int x : p) {
    ans += f(x / n, x % n, occ);
  }
  _print(), ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  __();
}
