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

namespace AtCoder
{
	class Program
	{
		static void Main(string[] args)
		{
			Joker.Run();
		}

		public static class Joker
		{
			public static void Run()
			{
				var n = int.Parse(Console.ReadLine());
				var permutation = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();

				var ar = new int[n + 2, n + 2];
				for (int i = 1; i <= n; i++)
					for (int j = 1; j <= n; j++)
					{
						var toBot = n - i;
						var toRight = n - j;

						ar[i, j] = Min(Min(i - 1, toBot), Min(j - 1, toRight));
					}

				var queue = new Queue<IntPair>();

				long score = 0;
				foreach (var p in permutation)
				{
					var pp = p - 1;
					var x = pp % n + 1;
					var y = pp / n + 1;

					score += ar[y, x];
					var currentP = ar[y, x];

					Append(ar, queue, x, y, currentP, 0, 1);
					Append(ar, queue, x, y, currentP, 0, -1);
					Append(ar, queue, x, y, currentP, 1, 0);
					Append(ar, queue, x, y, currentP, -1, 0);

					while (queue.Count != 0)
					{
						var p2 = queue.Dequeue();
						var currentP2 = ar[p2.First, p2.Second] + 1;
						Append(ar, queue, p2.Second, p2.First, currentP2, 0, 1);
						Append(ar, queue, p2.Second, p2.First, currentP2, 0, -1);
						Append(ar, queue, p2.Second, p2.First, currentP2, 1, 0);
						Append(ar, queue, p2.Second, p2.First, currentP2, -1, 0);
					}
				}

				Console.WriteLine(score);
			}

			private static void Append(int[,] ar, Queue<IntPair> queue, int x, int y, int currentP, int mx, int my)
			{
				if (ar[y + my, x + mx] > currentP)
				{
					ar[y + my, x + mx] = currentP;
					queue.Enqueue(new IntPair(y + my, x + mx));
				}
			}
		}

		public struct IntPair
		{
			public int First;
			public int Second;

			public IntPair(string s)
			{
				var split = s.Split();
				First = int.Parse(split[0]);
				Second = int.Parse(split[1]);
			}

			public IntPair(int first, int second)
			{
				First = first;
				Second = second;
			}

			public static bool operator ==(IntPair p1, IntPair p2) => p1.First == p2.First && p1.Second == p2.Second;
			public static bool operator !=(IntPair p1, IntPair p2) => p1.First != p2.First || p1.Second != p2.Second;
			public override string ToString() => $"{First} {Second}";
			public override bool Equals(object obj)
			{
				var p = obj as IntPair?;
				return p.HasValue && p.Value.First == First && p.Value.Second == Second;
			}

			public override int GetHashCode()
			{
				uint rol5 = ((uint)First << 5) | ((uint)First >> 27);
				return ((int)rol5 + First) ^ Second;
			}
		}

	}
}
