using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var a = NList; WriteLine(Heck(n, a)); } static long Heck(int n, int[] a) { if (n == 1) { return 0; } var pos = new int[n]; for (var i = 0; i < n; ++i) pos[a[i] - 1] = i; var tmp = 0L; var ans = tmp; for (var i = 1; i < n; ++i) { if (i == 1) tmp = pos[i - 1] - pos[i]; else tmp += pos[i - 1] * 2 - pos[i] + 1; ans = Math.Max(ans, tmp); } return ans; } }