using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(ReadLine());
            var permutation = ReadLine().Split(' ').Select(value => int.Parse(value) - 1).ToArray();

            var exists = new bool[n, n];
            var seats = new int[n, n];
            for(var i = 0; i < n; i++)
            {
                for (var j = 0; j < n; j++)
                {
                    exists[i, j] = true;
                    seats[i, j] = Min(Min(i, j), Min(n - (i + 1), n - (j + 1)));
                }
            }

            var count = 0;
            foreach(var number in permutation)
            {
                var row = number / n;
                var col = number % n;
                count += seats[row, col];
                exists[row, col] = false;

                var isFound = new bool[n, n];
                var queue = new Queue<int[]>();
                queue.Enqueue(new int[2] { row, col });

                while(queue.Count > 0)
                {
                    var seat = queue.Dequeue();
                    isFound[seat[0], seat[1]] = true;

                    var tmpRow = seat[0] + 1;
                    var tmpCol = seat[1];
                    if(tmpRow < n && !isFound[tmpRow, tmpCol] && seats[tmpRow, tmpCol] > 0 && seats[tmpRow, tmpCol] > seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0))
                    {
                        queue.Enqueue(new int[2] { tmpRow, tmpCol });
                        isFound[tmpRow, tmpCol] = true;
                        seats[tmpRow, tmpCol] = seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0);
                    }
                    tmpRow = seat[0] - 1;
                    tmpCol = seat[1];
                    if (tmpRow >= 0 && !isFound[tmpRow, tmpCol] && seats[tmpRow, tmpCol] > 0 && seats[tmpRow, tmpCol] > seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0))
                    {
                        queue.Enqueue(new int[2] { tmpRow, tmpCol });
                        isFound[tmpRow, tmpCol] = true;
                        seats[tmpRow, tmpCol] = seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0);
                    }
                    tmpRow = seat[0];
                    tmpCol = seat[1] + 1;
                    if (tmpCol < n && !isFound[tmpRow, tmpCol] && seats[tmpRow, tmpCol] > 0 && seats[tmpRow, tmpCol] > seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0))
                    {
                        queue.Enqueue(new int[2] { tmpRow, tmpCol });
                        isFound[tmpRow, tmpCol] = true;
                        seats[tmpRow, tmpCol] = seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0);
                    }
                    tmpRow = seat[0];
                    tmpCol = seat[1] - 1;
                    if (tmpCol >= 0 && !isFound[tmpRow, tmpCol] && seats[tmpRow, tmpCol] > 0 && seats[tmpRow, tmpCol] > seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0))
                    {
                        queue.Enqueue(new int[2] { tmpRow, tmpCol });
                        isFound[tmpRow, tmpCol] = true;
                        seats[tmpRow, tmpCol] = seats[seat[0], seat[1]] + (exists[seat[0], seat[1]] ? 1 : 0);
                    }
                }
            }

            WriteLine(count);
        }
    }
}
