#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double EPS = 1e-10;
const int INF = 1e9 + 1;
struct Mod {
  long long x;
  Mod(long long x = 0) : x(x) {}
  Mod operator+(Mod b) { return Mod((x + b.x) % MOD); }
  Mod operator-(Mod b) { return Mod((x - b.x + MOD) % MOD); }
  Mod operator*(Mod b) { return Mod((x * b.x) % MOD); }
  Mod operator/(Mod b) { return *this * inverse(b); }
  Mod operator%(Mod b) { return Mod(x % b.x); }
  static long long euclid(long long a, long long b, long long &x,
                          long long &y) {
    if (b) {
      long long d = euclid(b, a % b, y, x);
      y -= a / b * x;
      return d;
    }
    x = 1;
    y = 0;
    return a;
  }
  Mod inverse(Mod a) {
    long long x, y, g = euclid(a.x, MOD, x, y);
    assert(g == 1);
    return Mod((x + MOD) % MOD);
  }
  Mod operator^(long long e) {
    if (!e) return Mod(1);
    Mod r = *this ^ (e / 2);
    r = r * r;
    return e & 1 ? *this * r : r;
  }
};
template <typename A, typename B>
istream &operator>>(istream &in, pair<A, B> &p) {
  in >> p.first >> p.second;
  return in;
}
template <typename A, typename B>
ostream &operator<<(ostream &out, pair<A, B> &p) {
  out << p.first << " " << p.second;
  return out;
}
template <typename I>
istream &operator>>(istream &in, vector<I> &a) {
  for (auto &i : (a)) in >> i;
  return in;
}
template <typename I>
ostream &operator<<(ostream &out, vector<I> &a) {
  for (int i = 0; i < ((int)(a).size()); ++i) {
    if (i) out << " ";
    out << a[i];
  }
  return out;
}
void fast_io() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
}
void solve_test() {
  int n;
  cin >> n;
  vector<int> a(n * n, 1), p(n * n), d(n * n);
  for (auto &el : p) {
    cin >> el;
    el--;
  }
  auto enc = [&](int x, int y) { return x * n + y; };
  auto dec = [&](int z) { return make_pair(z / n, z % n); };
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      d[enc(i, j)] = min(min(i, j), min(n - 1 - i, n - 1 - j));
    }
  }
  int ans = 0;
  for (auto &el : p) {
    int temp = d[el];
    ans += temp;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        q;
    q.emplace(d[el] - 1, el);
    while (!q.empty()) {
      int dist, z;
      tie(dist, z) = q.top();
      q.pop();
      if (dist >= d[z]) {
        continue;
      }
      d[z] = dist;
      dist += a[z];
      int x, y;
      tie(x, y) = dec(z);
      vector<int> adj;
      if (x > 0) adj.push_back(enc(x - 1, y));
      if (x + 1 < n) adj.push_back(enc(x + 1, y));
      if (y > 0) adj.push_back(enc(x, y - 1));
      if (y + 1 < n) adj.push_back(enc(x, y + 1));
      for (int i : adj) {
        q.emplace(dist, i);
      }
    }
    a[el] = 0;
    d[el] = temp;
  }
  cout << ans << endl;
}
void setup() {}
int main() {
  setup();
  srand(time(0));
  fast_io();
  int t = 1;
  while (t--) {
    solve_test();
  }
}
