#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
int main() {
  int n, p[503][503] = {0}, x[503 * 503] = {0}, y[503 * 503] = {0}, X, Y,
         ans = 0;
  scanf("%d", &n);
  for (int i = (int)(0); i < (int)(n); i++)
    for (int j = (int)(0); j < (int)(n); j++) {
      scanf("%d", &X);
      Y = (X - 1) / n + 1;
      y[i * n + j + 1] = Y;
      X = (X % n ? X % n : n);
      x[i * n + j + 1] = X;
      p[X][Y] = i * n + j + 1;
    }
  for (int i = (int)(1); i < (int)(1 + n * n); i++) {
    int flag = 1, search = 0;
    while (flag) {
      search++;
      for (int j = (int)(0); j < (int)(search); j++)
        if (!p[x[i] + j][y[i] + search - j]) flag = 0;
      for (int j = (int)(0); j < (int)(search); j++)
        if (!p[x[i] + search - j][y[i] - j]) flag = 0;
      for (int j = (int)(0); j < (int)(search); j++)
        if (!p[x[i] - j][y[i] - search + j]) flag = 0;
      for (int j = (int)(0); j < (int)(search); j++)
        if (!p[x[i] - search + j][y[i] + j]) flag = 0;
    }
    p[x[i]][y[i]] = 1 - search;
    ans += search - 1;
  }
  printf("%d", ans);
}
