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)

/*
f[q[i]][j]=f[q[i-1]][j]+abs(q[i]-q[i-1])
f[q[i]][q[i-1]]=min{f[q[i-1]][j]+abs(q[i]-j)}
*/

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define INF 1LL<<60
typedef long long ll;
template<typename T>
void read(T &x)
{
	char ch=getchar();
	for (x=0;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
}
int n,q,a,b;
int ope[MAXN];
ll seq[MAXN],tmp;
namespace Segtree
{
	struct Node{
		ll mn,add;
	};
	struct Tree{
		Node tree[MAXN<<2];
		void modify_add_Node(int node,ll val)
		{
			tree[node].mn+=val;
			tree[node].add+=val;
		}
		void pushup(int node)
		{
			tree[node].mn=min(tree[node<<1].mn,tree[node<<1|1].mn);
		}
		void pushdown(int node)
		{
			if (tree[node].add)
			{
				modify_add_Node(node<<1,tree[node].add);
				modify_add_Node(node<<1|1,tree[node].add);
				tree[node].add=0;
			}
		}
		void build(int l,int r,int node)
		{
			if (l==r)
			{
				tree[node].mn=seq[l];
				tree[node].add=0;
				return;
			}
			int mid=(l+r)>>1;
			build(l,mid,node<<1);
			build(mid+1,r,node<<1|1);
			pushup(node);
		}
		void modify_add(int L,int R,int l,int r,int node,int val)
		{
			if (r<L||R<l) return;
			if (L<=l&&r<=R)
			{
				modify_add_Node(node,val);
				return;
			}
			pushdown(node);
			int mid=(l+r)>>1;
			modify_add(L,R,l,mid,node<<1,val);
			modify_add(L,R,mid+1,r,node<<1|1,val);
			pushup(node);
		}
		void modify_min(int pos,int l,int r,int node,ll val)
		{
			if (l==r)
			{
				tree[node].mn=min(tree[node].mn,val);
				return;
			}
			pushdown(node);
			int mid=(l+r)>>1;
			if (pos<=mid) modify_min(pos,l,mid,node<<1,val);
			else modify_min(pos,mid+1,r,node<<1|1,val);
			pushup(node);
		}
		ll query_min(int L,int R,int l,int r,int node)
		{
			if (r<L||R<l) return INF;
			if (L<=l&&r<=R) return tree[node].mn;
			pushdown(node);
			int mid=(l+r)>>1;
			return min(query_min(L,R,l,mid,node<<1),query_min(L,R,mid+1,r,node<<1|1));			
		}
		ll dfs(int l,int r,int node)
		{
			if (l==r) return tree[node].mn+l;
			pushdown(node);
			int mid=(l+r)>>1;
			return min(dfs(l,mid,node<<1),dfs(mid+1,r,node<<1|1));
		}
	};
}
Segtree::Tree T1,T2;
int main()
{
	read(n),read(q),read(a),read(b);
	ope[0]=a;
	for (int i=1;i<=q;i++)
		read(ope[i]);
	for (int i=1;i<=n;i++)
		seq[i]=INF;
	seq[b]=-b;
	T1.build(1,n,1);
	seq[b]=b;
	T2.build(1,n,1);
	for (int i=1;i<=q;i++)
	{
		tmp=min(T1.query_min(1,ope[i],1,n,1)+ope[i],T2.query_min(ope[i],n,1,n,1)-ope[i]);
		T1.modify_add(1,n,1,n,1,abs(ope[i]-ope[i-1]));
		T2.modify_add(1,n,1,n,1,abs(ope[i]-ope[i-1]));
		T1.modify_min(ope[i-1],1,n,1,tmp-ope[i-1]);
		T2.modify_min(ope[i-1],1,n,1,tmp+ope[i-1]);
	}
	
	printf("%lld",T1.dfs(1,n,1));
	return 0;
}