#include <bits/stdc++.h>
std::vector<int> dijkstra(const std::vector<std::map<int, int> >& adj,
                          int points, int st) {
  std::vector<int> ret(points, -1);
  std::priority_queue<std::array<int, 2> > pq;
  pq.push({0, st});
  while (!pq.empty()) {
    auto top = pq.top();
    if (ret[top[1]] == -1) ret[top[1]] = -top[0];
    pq.pop();
    for (const auto& elem : adj[top[1]]) {
      if (ret[elem.first] == -1) pq.push({top[0] - elem.second, elem.first});
    }
  }
  return ret;
}
void update(std::vector<std::map<int, int> >& adj, std::vector<int>& dist,
            int points, int updated, int n) {
  std::priority_queue<std::array<int, 2> > pq;
  if (updated - n > 0) {
    adj[updated - n][updated] = 0;
    pq.push({-dist[updated - n], updated - n});
  }
  if (updated + n <= points) {
    adj[updated + n][updated] = 0;
    pq.push({-dist[updated + n], updated + n});
  }
  if (updated % n != 1) {
    adj[updated - 1][updated] = 0;
    pq.push({-dist[updated - 1], updated - 1});
  }
  if (updated % n != 0) {
    adj[updated + 1][updated] = 0;
    pq.push({-dist[updated + 1], updated + 1});
  }
  std::vector<bool> checked(points + 1, false);
  checked[0] = true;
  while (!pq.empty()) {
    auto top = pq.top();
    std::cout << top[0] << " " << top[1] << std::endl;
    if (dist[top[1]] > -top[0]) {
      dist[top[1]] = -top[0];
    }
    checked[top[1]] = true;
    pq.pop();
    for (const auto& elem : adj[top[1]]) {
      if (checked[elem.first] == false)
        pq.push({top[0] - elem.second, elem.first});
    }
  }
  return;
}
int main(void) {
  int n;
  std::cin >> n;
  std::vector<std::map<int, int> > adj(n * n + 1);
  for (int i = 1; i <= n * n; i++) {
    if (i - n > 0) {
      adj[i - n][i] = 1;
    } else {
      adj[i][0] = 0;
      adj[0][i] = 0;
    }
    if (i + n <= n * n) {
      adj[i + n][i] = 1;
    } else {
      adj[i][0] = 0;
      adj[0][i] = 0;
    }
    if (i % n != 1) {
      adj[i - 1][i] = 1;
    } else {
      adj[i][0] = 0;
      adj[0][i] = 0;
    }
    if (i % n != 0) {
      adj[i + 1][i] = 1;
    } else {
      adj[i][0] = 0;
      adj[0][i] = 0;
    }
  }
  std::vector<int> dist = dijkstra(adj, n * n + 1, 0);
  long long int total = 0;
  for (int i = 0; i < n * n; i++) {
    int tmp;
    std::cin >> tmp;
    total += dist[tmp];
    update(adj, dist, n * n + 1, tmp, n);
  }
  std::cout << total << std::endl;
  return 0;
}
