#include <bits/stdc++.h>
struct read_item {
  read_item(){};
  template <class T>
  operator T() {
    T t;
    std::cin >> t;
    return t;
  }
};
char splf(int i, int n) { return (i + 1 < n) ? ' ' : '\n'; };
template <bool up>
struct _RI {
  int i;
  _RI(int a) : i(a) {}
  int operator*() { return i; }
  void operator++() { i += (up ? 1 : -1); }
  bool operator!=(const _RI& r) { return up ? i < r.i : i >= r.i; }
};
template <bool up>
struct _RX {
  _RI<up> x, y;
  _RI<up> begin() { return x; }
  _RI<up> end() { return y; }
  _RX(int a, int b) : x(up ? a : (a - 1)), y(b) {}
  _RX(int a) : _RX(up ? 0 : a, up ? a : 0) {}
};
using namespace std;
const int DXS[] = {0, -1, 1, 0};
const int DYS[] = {-1, 0, 0, 1};
int main() {
  const int n = read_item();
  vector<int> ps(n * n);
  for (auto i : _RX<true>(n * n)) {
    ps[i] = read_item();
    ps[i] = ps[i] - 1;
  }
  vector<vector<int>> dist(n);
  vector<vector<bool>> seat(n);
  for (auto& d : dist) {
    d.resize(n, 0);
  }
  for (auto& s : seat) {
    s.resize(n, true);
  }
  for (auto i : _RX<true>(n)) {
    for (auto j : _RX<true>(n)) {
      dist[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }
  int64_t ans = 0;
  for (auto p_it : _RX<true>(n * n)) {
    int p = ps[p_it];
    int sx = p / n;
    int sy = p % n;
    ans += dist[sx][sy];
    seat[sx][sy] = false;
    deque<pair<int, int>> q;
    for (auto i : _RX<true>(4)) {
      int x = sx + DXS[i];
      int y = sy + DYS[i];
      if (x >= 0 && y >= 0 && x < n && y < n) {
        q.push_back({x, y});
      }
    }
    while (!q.empty()) {
      int cx = q.front().first;
      int cy = q.front().second;
      q.pop_front();
      int d = dist[cx][cy];
      for (auto i : _RX<true>(4)) {
        int x = cx + DXS[i];
        int y = cy + DYS[i];
        if (x >= 0 && y >= 0 && x < n && y < n) {
          int nd = dist[x][y] + (seat[x][y] ? 1 : 0);
          d = min(d, nd);
        }
      }
      if (d >= dist[cx][cy]) {
        continue;
      }
      dist[cx][cy] = d;
      for (auto i : _RX<true>(4)) {
        int x = cx + DXS[i];
        int y = cy + DYS[i];
        if (x >= 0 && y >= 0 && x < n && y < n) {
          if (seat[cx][cy]) {
            q.push_back({x, y});
          } else {
            q.push_front({x, y});
          }
        }
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
