#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, dp[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
pair<int, int> ord[N];
int a[N][N];
bool emp[N][N];
bool inG(int first, int second) {
  return (first >= 1 && first <= n && second >= 1 && second <= n);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int id = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      emp[i][j] = 1;
      id++;
      int foo;
      cin >> foo;
      ord[id] = {(foo - 1) / n + 1, (foo - 1) % n + 1};
      a[ord[id].first][ord[id].second] = id;
      dp[i][j] = min({i, j, n + 1 - i, n + 1 - j});
    }
  }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    queue<pair<int, int>> q;
    q.push(ord[i]);
    emp[ord[i].first][ord[i].second] = 0;
    vector<vector<bool>> check(n + 1, vector<bool>(n + 1, 0));
    check[ord[i].first][ord[i].second] = 1;
    while (!q.empty()) {
      pair<int, int> u = q.front();
      q.pop();
      dp[u.first][u.second]--;
      for (int j = 0; j < 4; j++) {
        int first = u.first + dx[j];
        int second = u.second + dy[j];
        if (!inG(first, second)) continue;
        if (check[first][second]) continue;
        if (dp[first][second] ==
            dp[u.first][u.second] + 1 + emp[first][second]) {
          check[first][second] = 1;
          q.push({first, second});
        }
      }
    }
    ans += dp[ord[i].first][ord[i].second];
  }
  cout << ans;
  return 0;
}
