#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VB = vector<bool>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;
using TI3 = std::tuple<int, int, int>;
using TI4 = std::tuple<int, int, int, int>;
using TL3 = std::tuple<ll, ll, ll>;
using TL4 = std::tuple<ll, ll, ll, ll>;
const int mod1 = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9;
const int mod = mod1;
const ll inf = 1e18;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";
template <typename T>
void show1dvec(const vector<T> &v) {
  if (v.size() == 0) return;
  int n = v.size() - 1;
  for (int i = 0; i < (int)(n); i++) cout << v[i] << wsp;
  cout << v[n] << rt;
  return;
}
template <typename T>
void show2dvec(const vector<vector<T>> &v) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++) show1dvec(v[i]);
}
template <typename T, typename S>
void show1dpair(const vector<pair<T, S>> &v) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++)
    cout << v[i].first << wsp << v[i].second << rt;
  return;
}
template <typename T, typename S>
void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++) {
    t.push_back(v[i].first);
    s.push_back(v[i].second);
  }
  return;
}
template <typename T>
void maxvec(vector<T> &v) {
  T s = v[0];
  int n = v.size();
  for (int i = 0; i < (int)(n - 1); i++) {
    if (s > v[i + 1]) {
      v[i + 1] = s;
    }
    s = v[i + 1];
  }
}
template <typename T, typename S>
bool myfind(T t, S s) {
  return find(t.cbegin(), t.cend(), s) != t.cend();
}
bool check(int y, int x, int h, int w) {
  return 0 <= y && y < h && 0 <= x && x < w;
}
template <typename T>
vector<T> cumsum(const vector<T> &v) {
  T s = 0;
  vector<T> ret;
  for (int i = 0; i < (int)(v.size()); i++) {
    s += v[i];
    s %= mod;
    ret.emplace_back(s);
  }
  return ret;
}
bool iskadomatsu(int a, int b, int c) {
  return (a != b && b != c && c != a) && ((a > b && b < c) || (a < b && b > c));
}
VS split(string s, char c) {
  VS ret;
  string part;
  s += c;
  for (int i = 0; i < (int)(s.length()); i++) {
    if (s[i] == c) {
      ret.emplace_back(part);
      part = "";
    } else if (s[i] != c) {
      part += s[i];
    }
  }
  return ret;
}
string removezero(string &s) {
  string ret;
  for (auto z : s)
    if (z != '0') ret += z;
  return ret;
}
ll sumdigit(ll x) {
  ll ans{0};
  while (x > 0) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}
template <typename T, typename S, typename R>
ll pow_mod(T p, S q, R mod = 1ll) {
  ll ret = 1, r = p;
  while (q) {
    if (q % 2) ret *= r, ret %= mod;
    r = (r * r) % mod, q /= 2;
  }
  return ret % mod;
}
void make_frac_tables(VL &frac_list, VL &frac_inv_list) {
  for (int i = 0; i < (int)(frac_list.size() - 1); i++) {
    frac_list[i + 1] *= frac_list[i] * (i + 1);
    frac_list[i + 1] %= mod;
    frac_inv_list[i + 1] *= frac_inv_list[i] * pow_mod(i + 1, mod - 2, mod);
    frac_inv_list[i + 1] %= mod;
  }
}
ll comb(int a, int b, const VL &frac_list, const VL &frac_inv_list) {
  if (a < b) return 0;
  ll ret = frac_list[a];
  ret *= frac_inv_list[b];
  ret %= mod;
  ret *= frac_inv_list[a - b];
  ret %= mod;
  return ret;
}
class Weited_Unionfind {
  vector<int> p;
  vector<int> dist;
  VVI children;

 public:
  int find(int x);
  void unite(int x, int y);
  bool isunion(int x, int y);
  Weited_Unionfind(int n);
  int showdist(int n);
  void setdist(int n, int x);
  void showall();
};
Weited_Unionfind::Weited_Unionfind(int n) {
  children.resize(n);
  for (int i = 0; i < n; i++) {
    p.emplace_back(i);
    dist.emplace_back(0);
    children[i].emplace_back(i);
  }
}
int Weited_Unionfind::find(int x) {
  while (p[x] != x) {
    p[x] = p[p[x]];
    x = p[x];
  }
  return x;
}
void Weited_Unionfind::setdist(int n, int x) { dist[n] = x; }
int Weited_Unionfind::showdist(int n) {
  int pn = find(n);
  return dist[pn];
}
void Weited_Unionfind::unite(int x, int y) {
  int px, py, bigtree, smalltree;
  px = Weited_Unionfind::find(x);
  py = Weited_Unionfind::find(y);
  if (px == py) return;
  bigtree = (children[px].size() >= children[py].size() ? px : py);
  smalltree = (bigtree == px ? py : px);
  p[smalltree] = bigtree;
  dist[bigtree] = min(dist[smalltree], dist[bigtree]);
  for (int z : children[smalltree]) {
    children[bigtree].emplace_back(z);
    p[z] = bigtree;
  }
  children[smalltree].clear();
}
void Weited_Unionfind::showall() {
  show1dvec(dist);
  show1dvec(p);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, y, x, d, z, za, zb, ans{0}, mindist, ud, dd, ld, rd;
  cin >> n;
  Weited_Unionfind uf((n + 2) * (n + 2));
  for (int k = 0; k < (int)(n * n); k++) {
    y = k / n + 1;
    x = k % n + 1;
    za = k / n;
    z = k + n + 3 + 2 * za;
    d = min(min(y, n + 1 - y), min(x, n + 1 - x));
    uf.setdist(z, d);
  }
  for (int i = 0; i < (int)(n + 1); i++) {
    uf.unite(i, i + 1);
    uf.unite(i + (n + 1) * (n + 2), i + 1 + (n + 1) * (n + 2));
  }
  for (int i = 0; i < (int)(n + 1); i++) {
    uf.unite(i * (n + 2), (i + 1) * (n + 2));
    uf.unite(i * (n + 2) + n + 1, (i + 1) * (n + 2) + n + 1);
  }
  for (int k = 0; k < (int)(n * n); k++) {
    cin >> z;
    za = (z - 1) / n;
    z += n + 2 + 2 * za;
    ud = uf.showdist(z - n - 2);
    dd = uf.showdist(z + n + 2);
    ld = uf.showdist(z - 1);
    rd = uf.showdist(z + 1);
    mindist = min(ud, dd);
    mindist = min(mindist, rd);
    mindist = min(mindist, ld);
    if (mindist == ud) {
      uf.unite(z, z - n - 2);
    }
    if (mindist == dd) {
      uf.unite(z, z + n + 2);
    }
    if (mindist == ld) {
      uf.unite(z, z - 1);
    }
    if (mindist == rd) {
      uf.unite(z, z + 1);
    }
    ans += uf.showdist(z);
  }
  cout << ans << rt;
  return 0;
}
