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

namespace AtCoder.B
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var N = ReadLong();
            var P = ReadLongs();
            var chairs = new bool[N, N];

            var ans = 0L;
            foreach (var p in P)
            {
                var x = (p - 1) % N;
                var y = (p - 1) / N;

                chairs[x, y] = true;

                // 明らかな場合
                if (x == 0 || x == N - 1 || y == 0 || y == N - 1)
                {
                    continue;
                }

                var min = long.MaxValue;
                // 上方向
                var up = 0L;
                for (var i = y - 1; i >= 0; i--)
                {
                    if (!chairs[x, i])
                    {
                        up++;
                    }
                }
                min = Math.Min(min, up);

                // 下方向
                var down = 0L;
                for (var i = y; i < N; i++)
                {
                    if (!chairs[x, i])
                    {
                        down++;
                    }
                }
                min = Math.Min(min, down);

                // 右方向
                var right = 0L;
                for (var i = x; i < N; i++)
                {
                    if (!chairs[i, y])
                    {
                        right++;
                    }
                }
                min = Math.Min(min, right);

                // 左方向
                var left = 0L;
                for (var i = x; i >= 0; i--)
                {
                    if (!chairs[i, y])
                    {
                        left++;
                    }
                }
                min = Math.Min(min, left);

                ans += min;
            }

            return ans;
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}
