#include <bits/stdc++.h>
using namespace std;
int n, m, P[500005], dis[500005];
int nxt[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct pi {
  int x, y;
};
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
inline pi calc(int val) {
  pi tp;
  tp.x = val / n + 1, tp.y = val % n == 0 ? n : val % n;
  return tp;
}
inline int ccalc(int x, int y) { return (x - 1) * n + y; }
inline void bfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
  }
}
int main() {
  n = read(), m = n * n;
  for (int i = 1; i <= m; i++) P[i] = read();
  for (int i = 1; i <= m; i++) {
    pi tp = calc(i);
    dis[i] = min(tp.x - 1, n - tp.x) + min(tp.y - 1, n - tp.y);
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    pi tp = calc(i);
    ans += dis[i], bfs(tp.x, tp.y);
  }
  printf("%lld\n", ans);
  return 0;
}
