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

#include<cstdio>
const long long inf = 1e18+7;
long long x[200005];
struct SegmentT{
    int l,r;
    //f[i], f[i]-i, f[i]+i
    long long d[3];
    long long c;
    SegmentT(){
        for(int i = 0; i < 3; i++) d[i] = inf;
        c = 0;
    }
};
SegmentT t[800005];

long long max(long long a,long long b){ return a>b?a:b; }

long long min(long long a,long long b){ return a>b?b:a; }

SegmentT merge(SegmentT& u,SegmentT& v){
    SegmentT res;
    res.l = u.l, res.r = v.r, res.c = 0;
    for(int i = 0; i < 3; i++) res.d[i] = min(u.d[i],v.d[i]);
    return res;
}

void build(int p,int l,int r){
    t[p].l = l, t[p].r = r, t[p].c = 0;
    if(l==r){
        for(int i = 0; i < 3; i++) t[p].d[i] = inf;
    }
    else{
        int m = (l+r)/2;
        build(2*p,l,m);
        build(2*p+1,m+1,r);
        t[p] = merge(t[2*p],t[2*p+1]);
    }
}

void spread(int p){
    if(t[p].c!=0){
        long long c = t[p].c; t[p].c = 0;
        for(int i = 0; i < 3; i++){
            t[2*p].d[i] += c;
            t[2*p+1].d[i] += c;
        }
        t[2*p].c += c, t[2*p+1].c += c;
    }
}

void segChange(int p,int l,int r,long long v){
    if(l<=t[p].l && r>=t[p].r){
        for(int i = 0; i < 3; i++) t[p].d[i] += v;
        t[p].c += v;
    }
    else{
        spread(p);
        int m = (t[p].l+t[p].r)/2;
        if(l<=m) segChange(2*p,l,r,v);
        if(r>=m+1) segChange(2*p+1,l,r,v);
        t[p] = merge(t[2*p],t[2*p+1]);
    }
}

void pointChange(int p,int x,long long v){
    if(t[p].l==t[p].r){
        long long offset = t[p].l;
        if(v<t[p].d[0]){
            t[p].d[0] = v;
            t[p].d[1] = v-offset;
            t[p].d[2] = v+offset;
        }
    }
    else{
        spread(p);
        int m = (t[p].l+t[p].r)/2;
        if(x<=m) pointChange(2*p,x,v);
        else pointChange(2*p+1,x,v);
        t[p] = merge(t[2*p],t[2*p+1]);
    }
}

SegmentT segAsk(int p,int l,int r){
    if(l<=t[p].l && r>=t[p].r) return t[p];
    else{
        spread(p);
        int m = (t[p].l+t[p].r)/2;
        SegmentT u,v;
        if(l<=m) u = segAsk(2*p,l,r);
        if(r>=m+1) v = segAsk(2*p+1,l,r);
        return merge(u,v);
    }
}

long long abs(long long u){ return u>0?u:-u; }

void printTable(int n){
    for(int i = 1; i <= n; i++){
        SegmentT res = segAsk(1,i,i);
        printf("%lld ",res.d[0]);
    }
    printf("\n");
}

int main(){
    int n,q,a,b;
    scanf("%d%d%d%d",&n,&q,&a,&b);
    for(int i = 1; i <= q; i++) scanf("%lld",&x[i]);
    build(1,1,n);
    pointChange(1,a,abs(x[1]-b));
    pointChange(1,b,abs(x[1]-a));
    //printf("%lld\n",t[1].d[0]);
    //printTable(n);
    for(int i = 2; i <= q; i++){
        SegmentT left = segAsk(1,1,x[i]);
        long long offer = left.d[1]+x[i];
        if(x[i]+1<=n){
            SegmentT right = segAsk(1,x[i]+1,n);
            long long cur = right.d[2]-x[i];
            if(offer>cur) offer = cur;
        }
        segChange(1,1,n,abs(x[i]-x[i-1]));
        pointChange(1,x[i-1],offer);
        //printTable(n);
        //printf("curAns: %lld\n",t[1].d[0]);
    }   
    printf("%lld\n",t[1].d[0]);
    return 0;
}