#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
template <typename T>
void input_vector(vector<T>& v, long long int n, long long int start = 0) {
  for (long long int i = start; i < n; ++i) cin >> v[i];
}
template <typename T>
void display_vector(vector<T> v, long long int start = 0, string sep = " ") {
  for (long long int i = start; i < v.size(); ++i) cout << v[i] << sep;
  cout << "\n";
}
long long int bfs(vector<vector<long long int>>& seats, long long int p) {
  long long int i, j, n, tmp, cost = 1e18;
  long long int row, col;
  n = seats.size();
  col = (p - 1) % n;
  row = (p - 1) / n;
  seats[row][col] = 0;
  queue<pair<long long int, long long int>> q;
  vector<bool> vis(n * n + 1, false);
  q.push({p, 0});
  pair<long long int, long long int> pr;
  while (!q.empty()) {
    pr = q.front();
    q.pop();
    col = (pr.first - 1) % n;
    row = (pr.first - 1) / n;
    vis[pr.first] = true;
    if (row == 0 || row == n - 1 || col == 0 || col == n - 1) {
      cost = min(cost, pr.second);
      continue;
    }
    if (!vis[pr.first + 1])
      q.push({pr.first + 1, pr.second + seats[row][col + 1]});
    if (!vis[pr.first - 1])
      q.push({pr.first - 1, pr.second + seats[row][col - 1]});
    if (!vis[pr.first + n])
      q.push({pr.first + n, pr.second + seats[row + 1][col]});
    if (!vis[pr.first - n])
      q.push({pr.first - n, pr.second + seats[row - 1][col]});
  }
  return cost;
}
void solve(long long int n, vector<long long int> p) {
  vector<vector<long long int>> seats(n, vector<long long int>(n, 1));
  long long int cost = 0;
  for (long long int i = 0; i < p.size(); ++i) {
    cost += bfs(seats, p[i]);
  }
  cout << cost << "\n";
}
int main() {
  long long int t = 1;
  for (long long int cs = 1; cs <= t; ++cs) {
    long long int n;
    cin >> n;
    vector<long long int> p(n * n, 0);
    input_vector(p, n * n);
    solve(n, p);
  }
  return 0;
}
