#include <bits/stdc++.h>
using namespace std;
bool R() { return true; }
template <typename T, typename... Args>
bool R(T& x, Args&... args) {
  return cin >> x && R(args...);
}
void init_io() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
namespace sssp {
template <typename weight_val>
struct Edge {
  int from;
  int to;
  weight_val weight;
  Edge(int from, int to, const weight_val& weight)
      : from(from), to(to), weight(weight) {}
};
template <typename weight_val>
struct SSSP {
  vector<vector<int>> graph;
  vector<Edge<weight_val>> edges;
  weight_val INFINITY_VAL;
  SSSP(int N, const weight_val& INFINITY_VAL) : INFINITY_VAL(INFINITY_VAL) {
    graph.resize(N);
  }
  int add_edge(int from, int to, const weight_val& weight) {
    edges.push_back(Edge<weight_val>(from, to, weight));
    int eid = edges.size() - 1;
    graph[from].push_back(eid);
    return eid;
  }
  void dijkstra(int source, int* dist, int* prev = 0) {
    int n = graph.size();
    int m = edges.size();
    priority_queue<pair<weight_val, int>> pq;
    vector<bool> used;
    for (int i = (0); i < (n); i++) {
      dist[i] = INFINITY_VAL;
      if (prev) prev[i] = -1;
    }
    used.resize(n, false);
    dist[source] = 0;
    pq.push(make_pair(0, source));
    while (!pq.empty()) {
      auto pqpair = pq.top();
      pq.pop();
      int x = pqpair.second;
      if (used[x]) continue;
      used[x] = true;
      for (int i = (0); i < (graph[x].size()); i++) {
        int y = x ^ edges[graph[x][i]].from ^ edges[graph[x][i]].to;
        weight_val edge_dist = edges[graph[x][i]].weight;
        if (dist[x] + edge_dist < dist[y]) {
          dist[y] = dist[x] + edge_dist;
          pq.push(make_pair(-dist[y], y));
          if (prev) prev[y] = x;
        }
      }
    }
  }
  bool bellman_ford(int source, int* dist, int* prev = 0) {
    int n = graph.size();
    int m = edges.size();
    for (int i = (0); i < (n); i++) {
      dist[i] = INFINITY_VAL;
      if (prev) prev[i] = -1;
    }
    dist[source] = 0;
    for (int phase = (0); phase < (n - 1); phase++) {
      for (int i = (0); i < (m); i++) {
        if (dist[edges[i].from] + edges[i].weight < dist[edges[i].to]) {
          dist[edges[i].to] = dist[edges[i].from] + edges[i].weight;
          if (prev) prev[edges[i].to] = edges[i].from;
        }
      }
    }
    bool is_wo_cycle = true;
    for (int i = (0); i < (m); i++)
      if (dist[edges[i].from] + edges[i].weight < dist[edges[i].to]) {
        is_wo_cycle = false;
        break;
      }
    return is_wo_cycle;
  }
};
}  // namespace sssp
using namespace sssp;
int n, n2, p[250014], b[514][514], edg[514][514], d[510014], bd[510014];
map<int, pair<int, int>> rmap;
int getv(int i, int j) { return i * n2 + j; }
int getoutv(int i, int j) { return getv(i, j) + n2 * n2; }
void solve() {
  R(n);
  for (int k = (0); k < (n * n); k++) R(p[k]);
  n2 = n + 2;
  SSSP<int> g(2 * n2 * n2, 1000000007);
  for (int i = (0); i < (n2); i++)
    for (int j = (0); j < (n2); j++) {
      int inv = getv(i, j);
      int outv = getoutv(i, j);
      edg[i][j] = g.add_edge(inv, outv, 1);
      if (i > 0 && i <= n && j > 0 && j <= n) {
        for (int di = (-1); di < (2); di++)
          for (int dj = (-1); dj < (2); dj++)
            if (di * di + dj * dj == 1) {
              int targetv = getv(i + di, j + dj);
              g.add_edge(outv, targetv, 0);
            }
      }
    }
  int ci, cj;
  ci = cj = 1;
  for (int i = (1); i < (n * n + 1); i++) {
    rmap[i] = make_pair(ci, cj);
    ++cj;
    if (cj > n) {
      cj = 1;
      ++ci;
    }
  }
  for (int i = (0); i < (510014); i++) bd[i] = 1000000007;
  int res = 0;
  for (int k = (0); k < (n * n); k++) {
    pair<int, int> en = rmap[p[k]];
    int pi = en.first;
    int pj = en.second;
    g.edges[edg[pi][pj]].weight = 0;
    if (pi == 1 || pi == n || pj == 1 || pj == n) {
      bd[getv(pi, pj)] = 0;
      continue;
    }
    bool go = true;
    for (int di = (-1); di < (2); di++)
      for (int dj = (-1); dj < (2); dj++)
        if (di * di + dj * dj == 1) {
          int ni = pi + di;
          int nj = pj + dj;
          if (!bd[getv(ni, nj)]) {
            bd[getv(pi, pj)] = 0;
            go = false;
          }
        }
    if (!go) continue;
    g.dijkstra(getv(pi, pj), d);
    for (int i = (0); i < (n2); i++)
      bd[getv(pi, pj)] = min({bd[getv(pi, pj)], d[getv(0, i)], d[getv(i, 0)],
                              d[getv(n + 1, i)], d[getv(i, n + 1)]});
    res += bd[getv(pi, pj)];
  }
  printf("%d\n", res);
}
int main() {
  init_io();
  int t = 1;
  while (t--) solve();
  return 0;
}
