using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace AtCoder.Contests.AGC044
{
    /// <summary>
    /// 
    /// </summary>
    static class B
    {
        static int n;
        static bool[,] nn;


        static int[,] mm;
        static void Main(string[] args)
        {
            n = NextInt();
            var ps = ReadIntArray();
            nn = new bool[n + 2, n + 2];

            var sum = 0;

            mm = new int[n + 2, n + 2];
            Initializemm();
            for (int i = 0; i < n * n; i++)
            {
                var p = ps[i];
                var startp = new P { x = (p-1) / n + 1, y = (p-1) % n + 1 };


                var newCost = E4(startp).Min(po => mm[po.x, po.y]);

                sum += newCost;
                mm[startp.x, startp.y] = newCost;
                nn[startp.x, startp.y] = true;

                //var done = new bool[n + 2, n + 2];
                var q = new Queue<P>(n * n);
                foreach (var item in E4(startp))
                {
                    if (mm[item.x, item.y] > newCost + (nn[item.x, item.y] ? 0 : 1))
                    {
                        mm[item.x, item.y] = newCost + (nn[item.x, item.y] ? 0 : 1);
                        q.Enqueue(item);
                    }
                }
                while (q.Any())
                {
                    var next = q.Dequeue();
                    var cost = mm[next.x, next.y];
                    //done[next.x, next.y] = true;
                    foreach (var item in E4(next))
                    {
                        //if (done[item.x, item.y]) continue;
                        if (mm[item.x, item.y] > cost + (nn[next.x, next.y] ? 0 : 1))
                        {
                            mm[item.x, item.y] = cost + (nn[next.x, next.y] ? 0 : 1);
                            q.Enqueue(item);
                        }
                    }
                }
            }

            Console.WriteLine(sum);
        }
        static void Initializemm()
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    mm[i, j] = Math.Min(Math.Min(i, n - i + 1), Math.Min(j, n - j + 1));
                }
            }
        }
        static IEnumerable<P> E4(P c)
        {
            yield return new P { x = c.x - 1, y = c.y };
            yield return new P { x = c.x + 1, y = c.y };
            yield return new P { x = c.x, y = c.y - 1 };
            yield return new P { x = c.x, y = c.y + 1 };
        }
        static List<P> nexts = new List<P>(4);

        struct P
        {
            public int x;
            public int y;
        }

        #region Console
        static int _index = 0;
        static string[] _buffer;

        static string Next()
        {
            if (_buffer == null || _index >= _buffer.Length)
            {
                _buffer = Console.ReadLine().Split();
                _index = 0;
            }

            return _buffer[_index++];
        }
        static int NextInt()
        {
            return int.Parse(Next());
        }
        static long NextLong()
        {
            return long.Parse(Next());
        }
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        static int[] ReadIntArray(int n)
        {
            var a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadInt();
            }
            return a;
        }
        static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        static long[] ReadLongArray(int n)
        {
            var a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadLong();
            }
            return a;
        }
        static List<T> ReadObjects<T>(int n, Func<int[], T> creator)
        {
            var l = new List<T>(n);

            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                l.Add(creator(a));
            }

            return l;
        }

        static int[,] ReadMatrix(int n, int m)
        {
            var r = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                for (int j = 0; j < m; j++)
                {
                    r[i, j] = a[j];
                }
            }
            return r;
        }
        #endregion
    }
}
