#include <bits/stdc++.h>
using namespace std;
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
const double PI = 3.14159265358979323846;
const double eps = (1e-8);
int isGreater(long double x, long double y) {
  if (abs(x - y) < eps) return 0;
  if (x > y) return 1;
  return -1;
}
int n;
const int MAX_N = 505;
int dist[MAX_N][MAX_N], vis[MAX_N][MAX_N], vid, assigned[MAX_N][MAX_N];
bool danger[MAX_N][MAX_N];
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
bool valid(int i, int j) {
  return i >= 0 && j >= 0 && i < n && j < n && vis[i][j] != vid;
}
deque<pair<int, int> > dq;
int bfs(int sti, int stj) {
  ++vid;
  dist[sti][stj] = 0;
  assigned[sti][stj] = vid;
  dq.clear();
  dq.push_back({sti, stj});
  for (int size = (int)((dq).size()); !dq.empty(); size = (int)((dq).size())) {
    while (size--) {
      pair<int, int> u = dq.front();
      dq.pop_front();
      int i = u.first, j = u.second;
      if (vis[i][j] == vid) continue;
      if (!i || !j || i == n - 1 || j == n - 1) return dist[i][j];
      vis[i][j] = vid;
      for (int mv = 0; mv < 4; ++mv) {
        int ni = i + di[mv], nj = j + dj[mv];
        if (valid(ni, nj)) {
          int w = danger[ni][nj];
          if ((dist[ni][nj] > dist[i][j] + w && assigned[ni][nj] == vid) ||
              assigned[ni][nj] != vid) {
            dist[ni][nj] = dist[i][j] + w;
            assigned[ni][nj] = vid;
            if (w)
              dq.push_back({ni, nj});
            else {
              dq.push_front({ni, nj});
              size++;
            }
          }
        }
      }
    }
  }
  assert(false);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  memset(danger, 1, sizeof(danger));
  int ans = 0;
  for (int k = 0; k < n * n; ++k) {
    int x;
    cin >> x;
    --x;
    int sti = x / n, stj = x % n;
    danger[sti][stj] = false;
    ans += bfs(sti, stj);
  }
  cout << ans << '\n';
}
