#include <bits/stdc++.h>
using namespace std;
class Debugger {
 public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }
  template <typename T>
  void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};
const int maxN = 100005;
class TaskB {
 private:
  Debugger debugger;
  vector<int> dx = {0, 0, 1, -1};
  vector<int> dy = {1, -1, 0, 0};

 public:
  void solveOne(int it) {
    int n;
    cin >> n;
    vector<int> vals(n * n);
    for (int i = 0; i < n * n; i++) {
      cin >> vals[i];
    }
    reverse(vals.begin(), vals.end());
    vector<vector<int>> cost(n, vector<int>(n, 0));
    int total = 0;
    for (int i = 0; i < n * n; i++) {
      vals[i]--;
      int x = vals[i] / n;
      int y = vals[i] % n;
      deque<pair<int, int>> dq;
      dq.push_front({x, y});
      const int INF = 1e9;
      vector<vector<int>> dist(n, vector<int>(n, INF));
      dist[x][y] = 0;
      int ans = -1;
      while (!dq.empty() and ans == -1) {
        x = dq.front().first;
        y = dq.front().second;
        dq.pop_front();
        for (int dir = 0; dir < 4; dir++) {
          int xnew = dx[dir] + x;
          int ynew = dy[dir] + y;
          if (xnew >= n or xnew < 0 or ynew >= n or ynew < 0) {
            ans = dist[x][y];
            break;
          }
          if (cost[xnew][ynew] + dist[x][y] < dist[xnew][ynew]) {
            dist[xnew][ynew] = cost[xnew][ynew] + dist[x][y];
            if (cost[xnew][ynew] == 0) {
              dq.push_front({xnew, ynew});
            } else {
              dq.push_back({xnew, ynew});
            }
          }
        }
      }
      x = vals[i] / n;
      y = vals[i] % n;
      cost[x][y] = 1;
      total += ans;
    }
    cout << total << endl;
  }
  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  TaskB solver;
  solver.solve();
  return 0;
}
