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 <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int INF = 1e9, SEGMENT_TREE_SIZE = 1 << 19;
 
long long f[200005];
struct info{int L, R;};
info merge(info a, info b) {info x = {f[a.L]-a.L < f[b.L]-b.L ? a.L : b.L, f[a.R]+a.R < f[b.R]+b.R ? a.R : b.R}; return(x);}
 
template <class T>
class SegmentTree {
    T * a; int * L, * R;
    void divide(int i, int l, int r) {
        L[i] = l; R[i] = r;
        if (L[i] == R[i]) return;
        divide(2*i+1, l, (l+r)/2);
        divide(2*i+2, (l+r)/2+1, r);
    }
    void update(int i, int pos, T x) {
        if (L[i] > pos || R[i] < pos) return;
        if (L[i] == pos && R[i] == pos) {a[i] = x; return;}
        update(2*i+1, pos, x);
        update(2*i+2, pos, x);
        a[i] = merge(a[2*i+1], a[2*i+2]);
    }
    T search(int i, int l, int r) {
        if (L[i] >= l && R[i] <= r) return a[i];
        if (R[2*i+1] >= r) return search(2*i+1, l, r);
        if (L[2*i+2] <= l) return search(2*i+2, l, r);
        return merge(search(2*i+1, l, r), search(2*i+2, l, r));
    }
public:
    SegmentTree(int l, int r) {
        a = new T [SEGMENT_TREE_SIZE];
        L = new int [SEGMENT_TREE_SIZE];
        R = new int [SEGMENT_TREE_SIZE];
        divide(0, l, r);
    }
    void setval(int pos, T x) {update(0, pos, x);}
    T getval(int l, int r) {return search(0, l, r);}
};
 
int main() {
    int N, Q, A, B; scanf("%d%d%d%d", &N, &Q, &A, &B);
    fill(f + 1, f + N + 1, INF); f[A] = 0;
    SegmentTree<info> T(1, N);
    for (int i = 1; i <= N; ++i) {info x = {i, i}; T.setval(i, x);}
    int cur, prv = B; long long inc = 0, ans = 0;
    for (int i = 1; i <= Q; ++i) {
        scanf("%d", &cur); inc += abs(cur - prv);
        int l = T.getval(0, cur).L, r = T.getval(cur, N).R;
        ans = min(ans, f[prv] = min(f[l] + cur - l, f[r] - cur + r) - abs(cur - prv));
        info x = {prv, prv}; T.setval(prv, x); prv = cur;
    }
    printf("%lld", ans + inc);
    return 0;
}