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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<cstdio>
#define ll long long
#define MN 200000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,A,B;ll ans=1e18;
struct Tree{int l,r;ll val,x,mn1,mn2;}T[MN*4+5];
void Build(int x,int l,int r)
{
	if((T[x].l=l)==(T[x].r=r))
	{
		T[x].x=l==B?0:1e18;
		T[x].mn1=T[x].x+l;
		T[x].mn2=T[x].x-l;
		return;
	}
	int mid=l+r>>1;
	Build(x<<1,l,mid);Build(x<<1|1,mid+1,r);
	T[x].mn1=min(T[x<<1].mn1,T[x<<1|1].mn1);
	T[x].mn2=min(T[x<<1].mn2,T[x<<1|1].mn2);
}
inline void Mark(int x,ll v){T[x].val+=v;T[x].mn1+=v;T[x].mn2+=v;T[x].x+=v;}
inline void pushdown(int x){Mark(x<<1,T[x].val);Mark(x<<1|1,T[x].val);T[x].val=0;}
void Modify(int x,int k,ll v)
{
	if(T[x].l==T[x].r)
	{
		T[x].x=min(T[x].x,v);
		T[x].mn1=T[x].x+T[x].l;
		T[x].mn2=T[x].x-T[x].l;
		return;
	}
	int mid=T[x].l+T[x].r>>1;if(T[x].val)pushdown(x);
	Modify(x<<1|(k>mid),k,v);
	T[x].mn1=min(T[x<<1].mn1,T[x<<1|1].mn1);
	T[x].mn2=min(T[x<<1].mn2,T[x<<1|1].mn2);
}
pair<ll,ll> operator + (pair<ll,ll> a,pair<ll,ll> b){return make_pair(min(a.first,b.first),min(a.second,b.second));}
pair<ll,ll> Query(int x,int l,int r)
{
	if(T[x].l==l&&T[x].r==r) return make_pair(T[x].mn1,T[x].mn2);
	if(T[x].val) pushdown(x);int mid=T[x].l+T[x].r>>1;
	if(r<=mid) return Query(x<<1,l,r);
	else if(l>mid) return Query(x<<1|1,l,r); 
	else return Query(x<<1,l,mid)+Query(x<<1|1,mid+1,r);
}
inline int Abs(int x){return x<0?-x:x;}
int main()
{
	n=read();m=read();A=read();B=read();Build(1,1,n);
	for(int i=1,last=A;i<=m;++i)
	{
		int x=read();
		ll val=min(Query(1,1,x).second+x,Query(1,x,n).first-x);
		Mark(1,Abs(x-last));Modify(1,last,val);last=x;
	}
	for(int i=1;i<=MN*4;++i) 
	{
		if(T[i].l&&T[i].l==T[i].r) ans=min(ans,T[i].x);
		else if(T[i].val) pushdown(i);
	}
	cout<<ans;
	return 0;
}