#include <bits/stdc++.h>
using namespace std;
const int SIZE = 250000;
int board[SIZE] = {
    0,
};
int exist[SIZE] = {
    0,
};
int visit[SIZE] = {
    0,
};
int main() {
  queue<int> q;
  int n, p, i, k, ans;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    board[i] = 1;
    q.push(i);
    board[n * n - n + i] = 1;
    q.push(n * n - n + i);
    board[i * n] = 1;
    q.push(i * n);
    board[i * n + n - 1] = 1;
    q.push(i * n + n - 1);
  }
  while (!q.empty()) {
    k = q.front();
    q.pop();
    if (k >= n) {
      if (board[k - n] == 0 || board[k - n] > board[k] + 1) {
        board[k - n] = board[k] + 1;
        q.push(k - n);
      }
    }
    if (k < n * n - n) {
      if (board[k + n] == 0 || board[k + n] > board[k] + 1) {
        board[k + n] = board[k] + 1;
        q.push(k + n);
      }
    }
    if (k % n != 0) {
      if (board[k - 1] == 0 || board[k - 1] > board[k] + 1) {
        board[k - 1] = board[k] + 1;
        q.push(k - 1);
      }
    }
    if ((k + 1) % n != 0) {
      if (board[k + 1] == 0 || board[k + 1] > board[k] + 1) {
        board[k + 1] = board[k] + 1;
        q.push(k + 1);
      }
    }
  }
  for (i = 0; i < n * n; i++) {
    exist[i] = 1;
    visit[i] = -1;
  }
  ans = 0;
  for (i = 0; i < n * n; i++) {
    scanf("%d", &p);
    p--;
    board[p]--;
    exist[p] = 0;
    ans += board[p];
    q.push(p);
    visit[p] = i;
    while (!q.empty()) {
      k = q.front();
      q.pop();
      if (k >= n) {
        if (visit[k - n] != i) {
          if (exist[k - n] == 0) {
            board[k - n] = min(board[k - n], board[k]);
            q.push(k - n);
            visit[k - n] = i;
          } else if (board[k - n] > board[k] + 1) {
            board[k - n] = board[k] + 1;
            q.push(k - n);
            visit[k - n] = i;
          }
        }
      }
      if (k < n * n - n) {
        if (visit[k + n] != i) {
          if (exist[k + n] == 0) {
            board[k + n] = min(board[k + n], board[k]);
            q.push(k + n);
            visit[k + n] = i;
          } else if (board[k + n] > board[k] + 1) {
            board[k + n] = board[k] + 1;
            q.push(k + n);
            visit[k + n] = i;
          }
        }
      }
      if (k % n != 0) {
        if (visit[k - 1] != i) {
          if (exist[k - 1] == 0) {
            board[k - 1] = min(board[k - 1], board[k]);
            q.push(k - 1);
            visit[k - 1] = i;
          } else if (board[k - 1] > board[k] + 1) {
            board[k - 1] = board[k] + 1;
            q.push(k - 1);
            visit[k - 1] = i;
          }
        }
      }
      if ((k + 1) % n != 0) {
        if (visit[k + 1] != i) {
          if (exist[k + 1] == 0) {
            board[k + 1] = min(board[k + 1], board[k]);
            q.push(k + 1);
            visit[k + 1] = i;
          } else if (board[k + 1] > board[k] + 1) {
            board[k + 1] = board[k] + 1;
            q.push(k + 1);
            visit[k + 1] = i;
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
