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 FILE_IO

const int NMAX = 2e5 + 5;

typedef long long LL;

class SegmentTree
{
public:
    LL aint[4 * NMAX];
    LL add[4 * NMAX];
    LL v[NMAX];
    int N;
    LL ans;

    void init(int _N, LL _v[])
    {
        N = _N;
        for(int i = 1; i <= N; i++) v[i] = _v[i];
        B(1, 1, N);
    }

    void B(int nod, int st, int dr)
    {
        add[nod] = 0;
        if(st == dr)
        {
            aint[nod] = v[st];
            return;
        }

        int mij = st + (dr - st) / 2;
        B(nod * 2, st, mij);
        B(nod * 2 + 1, mij + 1, dr);

        aint[nod] = min(aint[nod * 2], aint[nod * 2 + 1]);
    }

    void U(int st, int dr, LL val) { U(1, 1, N, st, dr, val); }

    void U(int pos, LL val) { U(1, 1, N, pos, val); }

    void U(int nod, int st, int dr, int sti, int dri, LL val)
    {
        if(sti <= st && dr <= dri)
        {
            aint[nod] += val;
            add[nod] += val;
            return;
        }

        int mij = st + (dr - st) / 2;
        if(add[nod])
        {
            U(nod * 2, st, mij, st, dr, add[nod]);
            U(nod * 2 + 1, mij + 1, dr, st, dr, add[nod]);
            add[nod] = 0;
        }

        if(sti <= mij)  U(nod * 2, st, mij, sti, dri, val);
        if(mij < dri)   U(nod * 2 + 1, mij + 1, dr, sti, dri, val);

        aint[nod] = min(aint[nod * 2], aint[nod * 2 + 1]);
    }

    void U(int nod, int st, int dr, int pos, LL val)
    {
        if(st == dr)
        {
            aint[nod] = val;
            return;
        }

        int mij = st + (dr - st) / 2;
        if(add[nod])
        {
            U(nod * 2, st, mij, st, dr, add[nod]);
            U(nod * 2 + 1, mij + 1, dr, st, dr, add[nod]);
            add[nod] = 0;
        }

        if(pos <= mij)  U(nod * 2, st, mij, pos, val);
        else            U(nod * 2 + 1, mij + 1, dr, pos, val);

        aint[nod] = min(aint[nod * 2], aint[nod * 2 + 1]);
    }

    void Q(int nod, int st, int dr, int sti, int dri)
    {
        if(sti <= st && dr <= dri)
        {
            ans = min(ans, aint[nod]);
            return;
        }

        int mij = st + (dr - st) / 2;
        if(add[nod])
        {
            U(nod * 2, st, mij, st, dr, add[nod]);
            U(nod * 2 + 1, mij + 1, dr, st, dr, add[nod]);
            add[nod] = 0;
        }

        if(sti <= mij)  Q(nod * 2, st, mij, sti, dri);
        if(mij < dri)   Q(nod * 2 + 1, mij + 1, dr, sti, dri);
    }

    LL q(int st, int dr)
    {
        if(st > dr) return (1LL << 60);
        ans = 1LL << 60;
        Q(1, 1, N, st, dr);
        return ans;
    }
};

SegmentTree aint, amns, apls;

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    int N, M, A, B;
    scanf("%d%d%d%d", &N, &M, &A, &B);

    LL v[NMAX];
    for(int i = 1; i <= N; i++) v[i] = 1LL << 40;
    v[B] = 0;
    aint.init(N, v);

    for(int i = 1; i <= N; i++) v[i] -= i;
    amns.init(N, v);

    for(int i = 1; i <= N; i++) v[i] += 2 * i;
    apls.init(N, v);

    for(int i = 1; i <= M; i++)
    {
        int p; scanf("%d", &p);
        int dst = abs(p - A);

        LL st = amns.q(1, p);
        LL dr = apls.q(p, N);
        LL val = min(st + p, dr - p);

        aint.U(1, N, dst); amns.U(1, N, dst); apls.U(1, N, dst);
        aint.U(A, val); amns.U(A, val - A); apls.U(A, val + A);

        A = p;
    }

    LL ans = aint.q(1, N);
    printf("%lld\n", ans);

    return 0;
}