#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec) is >> x;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < (int)vec.size(); i++)
    os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  for (auto &&itr : map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  for (auto &&itr : set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
void dump_func() { cerr << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  cerr << head;
  if (sizeof...(Tail) > 0) cerr << ", ";
  dump_func(std::move(tail)...);
}
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
using vec = std::vector<T>;
template <class T>
void print(const T &x) {
  cout << x << "\n";
}
const long long MOD = 1000000007, INF0 = 1061109567, INF = INF0 * INF0;
const double EPS = 1e-7, PI = acos(-1.0);
const long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
long long t[550][550];
long long cost[550][550];
long long cost2[550][550];
long long N;
void bfs(long long v0, long long v1, long long id, long long ans) {
  queue<pair<long long, long long> > q;
  q.push(make_pair(v0, v1));
  cost[v0][v1] = ans;
  while (!q.empty()) {
    pair<long long, long long> ele = q.front();
    long long ret = t[ele.first][ele.second];
    long long c0 = cost[ele.first][ele.second];
    q.pop();
    for (long long i = 0; i < 4; i++) {
      pair<long long, long long> next =
          make_pair(ele.first + dx[i], ele.second + dy[i]);
      if (0 <= next.first && next.first < N && 0 <= next.second &&
          next.second < N) {
        long long val = t[next.first][next.second];
        long long c1 = cost[next.first][next.second];
        if (ret <= id && val <= id) {
          if (c0 > c1) {
            cost[ele.first][ele.second] = c1;
            q.push(make_pair(ele.first, ele.second));
          } else if (c1 > c0) {
            cost[next.first][next.second] = c0;
            q.push(make_pair(next.first, next.second));
          }
        }
        if (id == 0)
          ;
      }
    }
  }
}
signed main() {
  cin.tie(0), ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  cin >> N;
  long long N2 = N * N;
  vec<pair<long long, long long> > lis;
  for (long long i = 0; i < N2; i++) {
    long long p;
    cin >> p;
    p--;
    t[p / N][p % N] = i;
    lis.push_back(make_pair(p / N, p % N));
  }
  for (long long i = 0; i < N2; i++) {
    long long i0 = i / N;
    long long i1 = i % N;
    cost[i0][i1] = min({i0, i1, N - 1 - i0, N - 1 - i1});
  }
  long long ans = 0;
  for (long long i = 0; i < N2; i++) {
    pair<long long, long long> ele = lis[i];
    long long v0 = ele.first;
    long long v1 = ele.second;
    long long id = t[v0][v1];
    long long ret = cost[v0][v1];
    for (long long j = 0; j < 4; j++) {
      long long w0 = v0 + dx[j];
      long long w1 = v1 + dy[j];
      if (0 <= w0 && w0 < N && 0 <= w1 && w1 < N) {
        chmin(ret, cost[w0][w1] + (t[w0][w1] > id));
      }
    }
    ans += ret;
    bfs(v0, v1, i, ret);
  }
  print(ans);
}
