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];
            var costs = new int[(n + 1) / 2];
            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)));
                    costs[seats[i, j]]++;
                }
            }

            var count = 0;
            foreach (var number in permutation)
            {
                var row = number / n;
                var col = number % n;
                count += seats[row, col];
                exists[row, col] = false;
                costs[seats[row, col]]--;
                var canContinue = true;
                for(var i = seats[row, col] + 1; i < costs.Length; i++)
                {
                    if(costs[i] > 0)
                    {
                        canContinue = false;
                        break;
                    }
                }
                if(canContinue)
                {
                    continue;
                }

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

                while (queue.Count > 0)
                {
                    var seat = queue.Dequeue();

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

            WriteLine(count);
        }
    }
}
