#include <bits/stdc++.h>
using namespace std;
struct Node {
  int cost;
  bool ocp;
};
vector<vector<Node> > work;
vector<vector<bool> > castmap;
int SIZE = 0;
int findmin(int a, int b, int c, int d) {
  vector<int> output;
  output.push_back(a);
  output.push_back(b);
  output.push_back(c);
  output.push_back(d);
  sort(output.begin(), output.end());
  int temp = 0;
  for (int i = 0; i < output.size(); i++) {
    if (output[i] != -1) {
      return output[i];
    }
  }
  return temp;
}
int Findmin(int a, int b, int c, int d) {
  vector<int> output;
  output.push_back(a);
  output.push_back(b);
  output.push_back(c);
  output.push_back(d);
  sort(output.begin(), output.end());
  return output[0];
}
void bordcost(int x, int y);
void Bordcost(int x, int y, int cost_t, bool ocpa) {
  if (work[x][y].cost > cost_t) {
    bool bc = !castmap[x][y];
    if (ocpa && work[x][y].ocp) {
      if (work[x][y].cost + 1 == cost_t) {
        bc = false;
      }
      work[x][y].cost = cost_t + 1;
    } else {
      work[x][y].cost = cost_t;
    }
    if (bc) bordcost(x, y);
  }
}
void bordcost(int x, int y) {
  int temp = work[x][y].cost;
  int ocpa = work[x][y].ocp;
  castmap[x][y] = true;
  Bordcost(x - 1, y, temp, ocpa);
  Bordcost(x + 1, y, temp, ocpa);
  Bordcost(x, y + 1, temp, ocpa);
  Bordcost(x, y - 1, temp, ocpa);
}
int leave(int le) {
  int x = le / (SIZE - 2) + 1;
  int y = le % (SIZE - 2) + 1;
  int output = work[x][y].cost;
  work[x][y].ocp = false;
  bordcost(x, y);
  return output;
}
int main() {
  cin >> SIZE;
  SIZE += 2;
  work.resize(SIZE);
  castmap.resize(SIZE);
  long long output = 0;
  for (int i = 0; i < SIZE; i++) {
    work[i].resize(SIZE);
    castmap[i].resize(SIZE);
    for (int j = 0; j < SIZE; j++) {
      work[i][j].cost = (Findmin(i - 1, j - 1, SIZE - i - 2, SIZE - j - 2));
    }
  }
  for (int i = 0; i < SIZE * SIZE; i++) {
    int now;
    cin >> now;
    for (int i = 0; i < SIZE; i++) {
      for (int j = 0; j < SIZE; j++) {
        castmap[i][j] = false;
      }
    }
    output += leave(now - 1);
  }
  cout << output;
  return 0;
}
