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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = 1e8;
const ll INF = 1e16;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

using T = ll;
const int MAX_V = 5e5;
//演算の内容
T f(T a, T b){return min(a, b);}
//何で初期化するか
T other = 1e18;

struct segment_tree{
    int n;
    T seg[2*MAX_V];
    //要素数N(0〜N-1)の初期化されたセグメント木を構築
    segment_tree(int N){
        n = 1;
        while(n < N) n *= 2;
        fill_n(seg, 2*n-1, other);
    }
    //i番目の要素をxに変更(0番目から数える)
    void update(int i, T x){
        i += n-1;
        seg[i] = x;
        while(i > 0){
            i = (i-1)/2;
            seg[i] = f(seg[2*i+1], seg[2*i+2]);
        }
    }
    //query(a, b)で[a, b)における演算の結果を取得
    T query(int a, int b, int i = 0, int l = 0, int r = -1){
        if(r < 0) r = n;
        if(a >= r || b <= l) return other;
        elif(a <= l && r <= b) return seg[i];
        else{
            T vl = query(a, b, 2*i+1, l, (l+r)/2);
            T vr = query(a, b, 2*i+2, (l+r)/2, r);
            return f(vl, vr);
        }
    }
};

int main(){
    int N, Q, A;
    cin >> N >> Q >> A;
    A--;
    int x[Q+1];
    rep(i, Q+1){
        cin >> x[i];
        x[i]--;
    }
    segment_tree seg1(N), seg2(N);
    rep(i, N){
        seg1.update(i, INF+i);
        seg2.update(i, INF-i);
    }
    seg1.update(A, A);
    seg2.update(A, -A);
    ll sum = 0;
    rep(i, Q){
        ll tmp = INF;
        tmp = min(tmp, seg1.query(x[i+1], N) - x[i+1]);
        tmp = min(tmp, seg2.query(0, x[i+1]) + x[i+1]);
        ll c = tmp-seg1.query(x[i], x[i]+1)+x[i];
        ll d = abs(x[i+1]-x[i]);
        if(c < d){
            seg1.update(x[i], seg1.query(x[i], x[i]+1) + c-d);
            seg2.update(x[i], seg2.query(x[i], x[i]+1) + c-d);   
        }
        sum += d;
    }
    ll ans = INF;
    rep(i, N){
        ans = min(ans, sum+seg1.query(i, i+1) - i);
    }
    cout << ans << endl;
}