Problem:
Score : 900 points

Problem Statement

There are N squares in a row. The squares are numbered 1, 2, ..., N from left to right.

You have two pieces, initially placed on square A and B, respectively.
You will be asked to process Q queries of the following kind, in the order received:

Given an integer x_i, move one of the two pieces of your choice to square x_i.

Here, it takes you one second to move a piece one square.
That is, the time it takes to move a piece from square X to Y is |X-Y| seconds.

Your objective is to process all the queries in the shortest possible time.

You may only move the pieces in response to queries, and you may not move both pieces at the same time.
Also, it is not allowed to rearrange the order in which queries are given.
It is, however, allowed to have both pieces in the same square at the same time.

Constraints

1 ≤ N, Q ≤ 200,000

1 ≤ A, B ≤ N

1 ≤ x_i ≤ N

Input

Input is given from Standard Input in the following format:

N Q A B
x_1 x_2 ... x_Q

Output

Let the shortest possible time to process all the queries be X seconds. Print X.

Sample Input 1

8 3 1 8
3 5 1

Sample Output 1

7

All the queries can be processed in seven seconds, by:

moving the piece at square 1 to 3

moving the piece at square 8 to 5

moving the piece at square 3 to 1

Sample Input 2

9 2 1 9
5 1

Sample Output 2

4

The piece at square 9 should be moved first.

Sample Input 3

9 2 1 9
5 9

Sample Output 3

4

The piece at square 1 should be moved first.

Sample Input 4

11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7

Sample Output 4

21

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

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

namespace Codeforces
{
	internal class Template
	{

		private long[] queries = new long[200200];
		private long qq;
		private Dictionary<Tuple<long, long, long>, long> dp; 

		private void Solve()
		{
			//var n = cin.NextLong();
			//var x = new long[n];
			//var y = new long[n];
			//var min = long.MaxValue;
			//var max = long.MinValue;
			//for (var i = 0; i < n; i++)
			//{
			//	x[i] = cin.NextLong();
			//	y[i] = cin.NextLong();
			//	min = Math.Min(min, Math.Min(x[i], y[i]));
			//	max = Math.Max(max, Math.Max(x[i], y[i]));
			//	if (x[i] > y[i])
			//	{
			//		var temp = x[i];
			//		x[i] = y[i];
			//		y[i] = temp;
			//	}
			//}
			//var ma
			var n = cin.NextLong();
			qq = cin.NextLong();
			var a = cin.NextLong();
			var b = cin.NextLong();
			for (var i = 0; i < qq; i++)
			{
				queries[i] = cin.NextLong();
			}
			dp = new Dictionary<Tuple<long, long, long>, long>();
			var res = Calc(0, a, b);
			Console.WriteLine(res);
		}

		private long Calc(long q, long a, long b)
		{
			if (q == qq)
			{
				return 0;
			}
			var key = Tuple.Create(q, Math.Min(a, b), Math.Max(a, b));
			if (dp.ContainsKey(key))
			{
				return dp[key];
			}
			var best = Math.Min(Calc(q + 1, queries[q], b) + Math.Abs(a - queries[q]),
				Calc(q + 1, a, queries[q]) + Math.Abs(b - queries[q]));
			dp[key] = best;
			return best;
		}

		private static readonly Scanner cin = new Scanner();

		private static void Main()
		{
#if DEBUG
			var inputText = File.ReadAllText(@"..\..\input.txt");
			var testCases = inputText.Split(new[] { "input" }, StringSplitOptions.RemoveEmptyEntries);
			var consoleOut = Console.Out;
			for (var i = 0; i < testCases.Length; i++)
			{
				var parts = testCases[i].Split(new[] { "output" }, StringSplitOptions.RemoveEmptyEntries);
				Console.SetIn(new StringReader(parts[0].Trim()));
				var stringWriter = new StringWriter();
				Console.SetOut(stringWriter);
				var sw = Stopwatch.StartNew();
				new Template().Solve();
				sw.Stop();
				var output = stringWriter.ToString();

				Console.SetOut(consoleOut);
				var color = ConsoleColor.Green;
				var status = "Passed";
				if (parts[1].Trim() != output.Trim())
				{
					color = ConsoleColor.Red;
					status = "Failed";
				}
				Console.ForegroundColor = color;
				Console.WriteLine("Test {0} {1} in {2}ms", i + 1, status, sw.ElapsedMilliseconds);
			}
			Console.ReadLine();
			Console.ReadKey();
#else
			new Template().Solve();
			Console.ReadLine();
#endif
		}
	}

	internal class Scanner
	{
		private string[] s = new string[0];
		private int i;
		private readonly char[] cs = { ' ' };

		public string NextString()
		{
			if (i < s.Length) return s[i++];
			var line = Console.ReadLine() ?? string.Empty;
			s = line.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			i = 1;
			return s.First();
		}

		public double NextDouble()
		{
			return double.Parse(NextString());
		}

		public int NextInt()
		{
			return int.Parse(NextString());
		}

		public long NextLong()
		{
			return long.Parse(NextString());
		}
	}
}