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: Runtime Error)

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
typedef long long   ll;
#define pb                push_back
#define mp                make_pair
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define mem(a,h)          memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a,b) (((a%MOD) * (b%MOD))%MOD)
#define forg(i, b, e, c)    for (ll i = (ll)b; i < (ll)e; i+=c)
#define forr(i, b, e)    for (ll i = b; i < e; i++)
 
 
using namespace std;
//using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll>  ii;
typedef pair<ii, ll>  iii;
typedef vector<ll>     vi;
typedef vector<vi>     vvi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll INF = 1e15;
const double PI = acos(-1);
#define tam 820000
#define offset 
const ll MOD= 1e9+7;
#define initseg int new_nodo=(pnodo*2),mid=(iz+der)/2;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct nodo
{
    ll vleft;
    ll vright;
    ll minn;
    ll lazy;
    nodo(){}
};
nodo tree[tam];
ll vsort[tam];
        
        ll newnod;
void push(ll pnodo)
{
    if (tree[pnodo].lazy==0)
        return;
    newnod=pnodo*2;
    tree[pnodo].vleft+=tree[pnodo].lazy;
    tree[pnodo].vright+=tree[pnodo].lazy;
    tree[pnodo].minn+=tree[pnodo].lazy;

    tree[newnod].lazy+=tree[pnodo].lazy;
    tree[newnod+1].lazy+=tree[pnodo].lazy;
    tree[pnodo].lazy=0;
}
void join(ll pnodo)
{
    push(pnodo*2);
    push(pnodo*2+1);
    newnod=pnodo*2;
    
    tree[pnodo].vleft=min(tree[newnod].vleft,tree[newnod+1].vleft);
    tree[pnodo].vright=min(tree[newnod].vright,tree[newnod+1].vright);
    tree[pnodo].minn=min(tree[newnod].minn,tree[newnod+1].minn);
}

void update(ll pnodo,ll iz,ll der,ll pos,ll valor)
{
    push(pnodo);
    if (iz==der)
    {
        tree[pnodo].vleft=min(tree[pnodo].vleft,valor-vsort[iz]);
        tree[pnodo].vright=min(tree[pnodo].vright,valor+vsort[iz]);
        tree[pnodo].minn=min(tree[pnodo].minn,valor);
        return;
    }
    initseg
    if (pos<=mid)
        update(new_nodo,iz,mid,pos,valor);
    else
        update(new_nodo+1,mid+1,der,pos,valor);
    join(pnodo);
}

    ll minleft,minright;// inicializar
    #define clrqry minleft=INF,minright=INF;
void query(ll pnodo,ll iz,ll der,ll li,ll ri)
{
    push(pnodo);
    if (iz>ri|| der<li)
        return;
    if (iz>=li && der<=ri){
        minleft=min(minleft,tree[pnodo].vleft);
        minright=min(minright,tree[pnodo].vright);
        return;
    }
    initseg
    query(new_nodo,iz,mid,li,ri);
    query(new_nodo+1,mid+1,der,li,ri);
}

/*void go(ll pnodo,ll iz,ll der)
{
    push(pnodo);
    if (iz==der)
    {
        cout<<tree[pnodo].minn<<"   ";
        return;
    }
    initseg;
    go(new_nodo,iz,mid);
    go(new_nodo+1,mid+1,der);
}*/
ll v[tam];
map<ii,ll> mapa;
int  main()
{
    forr(i,0,tam*4)
    {
        tree[i].minn=tree[i].vleft=tree[i].vright=INF;
        tree[i].lazy=0;
    }
    ios::sync_with_stdio(false);cin.tie(0);
    ll n,q;
    cin>>n>>q;
    cin>>v[1]>>v[2];
    forr(i,0,q)
    {
        cin>>v[i+3];
    }
    vector<ii> lst;
    forr(i,1,q+3)
    {
        lst.pb({v[i],i});
    }
    ll N=lst.size();
    sort(lst.begin(),lst.end());
    ll cnt=1;
    for(auto par:lst)
    {
        vsort[cnt]=par.fi;
       // cout<<par.fi<<' '<<par.se<<' '<<cnt<<endl;
        mapa[par]=cnt++;
    }   
    //return 0;
    update(1,1,N,mapa[{v[1],1}],0);
            
    ll valiz,valder,valupd;
    forr(i,3,q+3)
    {
        
        ll postree=mapa[{v[i],i}];
            
        //cout<<postree<<" update this pos: "<<endl;
        clrqry;
        query(1,1,N,1,postree);     valiz=minleft;
        clrqry;
        query(1,1,N,postree,N);     valder=minright;
        
        valupd=min(valiz+v[i],valder-v[i]);
        //valupd=min(valupd,INF);
        //cout<<valiz<<" "<<valder<<' ';
        //cout<<valupd<<endl;
        tree[1].lazy+=abs(v[i]-v[i-1]);
        postree=mapa[{v[i-1],i-1}];
        update(1,1,N,postree,valupd);
        //cout<<"final\n";
        //cout<<tree[1].minn<<endl;

        
            

    }
    push(1);
    cout<<tree[1].minn<<endl;

}
// PLUS ULTRA!ac