import std;

immutable int dirs = 4;
immutable int [dirs] dRow = [-1,  0, +1,  0];
immutable int [dirs] dCol = [ 0, -1,  0, +1];

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto board = new int [] [] (n + 2, n + 2);
		foreach (row; 1..n + 1)
		{
			foreach (col; 1..n + 1)
			{
				board[row][col] = min (row - 1, n - row,
				    col - 1, n - col);
			}
		}
		auto mark = new bool [] [] (n + 2, n + 2);

		void recur (int row, int col)
		{
			auto cost = board[row][col] + !mark[row][col];
			foreach (dir; 0..dirs)
			{
				auto nRow = row + dRow[dir];
				auto nCol = col + dCol[dir];
				if (board[nRow][nCol] > cost)
				{
					board[nRow][nCol] = cost;
					recur (nRow, nCol);
				}
			}
		}

		int res = 0;
		foreach (cur; readln.splitter.map !(to !(int)))
		{
			cur -= 1;
			auto row = cur % n + 1;
			auto col = cur / n + 1;
			res += board[row][col];
			mark[row][col] = true;
			recur (row, col);
		}
		writeln (res);
	}
}
