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)

//Link : https://atcoder.jp/contests/arc073/tasks/arc073_d

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define inf 1000000000000000LL
#define OUT (ll)4e18
int x[N];

int dist(int x,int y) {
  return abs(x-y);
}

ll segMinus[N*4],segPlus[N*4];
ll lazyMinus[N*4],lazyPlus[N*4];

void init(int pos,int l,int r) {
  segMinus[pos] = segPlus[pos] = inf;
  lazyPlus[pos] = lazyMinus[pos] = 0;
  if(l==r) {
    return;
  }
  int mid = (l+r)/2;
  init(pos*2,l,mid);
  init(pos*2+1,mid+1,r);
}

void push(ll *seg,ll *lazy,int pos,int l,int r) {
  if(lazy[pos]!=0) {
    seg[pos] += lazy[pos];
    if(l!=r) {
      lazy[pos*2] += lazy[pos];
      lazy[pos*2+1] += lazy[pos];
    }
    lazy[pos] = 0;
  }
}

void update(ll *seg,ll *lazy,int pos,int l,int r,int ql,int qr,ll value) {
  push(seg,lazy,pos,l,r);
  if(qr<l || r<ql) {
    return;
  }
  if(ql<=l && r<=qr) {
    lazy[pos] = value;
    push(seg,lazy,pos,l,r);
    return;
  }
  int mid = (l+r)/2;
  update(seg,lazy,pos*2,l,mid,ql,qr,value);
  update(seg,lazy,pos*2+1,mid+1,r,ql,qr,value);
  seg[pos] = min(seg[pos*2],seg[pos*2+1]);
}

ll query(ll *seg,ll *lazy,int pos,int l,int r,int ql,int qr) {
  push(seg,lazy,pos,l,r);
  if(qr<l || r<ql) {
    return OUT;
  }
  if(ql<=l && r<=qr) {
    return seg[pos];
  }
  int mid = (l+r)/2;
  return min(query(seg,lazy,pos*2,l,mid,ql,qr),
  query(seg,lazy,pos*2+1,mid+1,r,ql,qr));
}

ll solveMin(int q,int n) {
  ll ret = 0;
  swap(x[0],x[1]);
  init(1,1,n);
  update(segPlus,lazyPlus,1,1,n,x[0],x[0],x[0]-inf);
  update(segMinus,lazyMinus,1,1,n,x[0],x[0],-x[0]-inf);
  for(int i=2;i<q;++i) {
    ll d = dist(x[i-1],x[i]);
    ret += d;
    ll a = query(segPlus,lazyPlus,1,1,n,x[i],n)-x[i];
    ll b = query(segMinus,lazyMinus,1,1,n,1,x[i])+x[i];
    ll mini = min(a,b);
    ret = min(ret,mini);
    ll minPlus = mini + x[i-1];
    ll minMinus = mini - x[i-1];
    update(segPlus,lazyPlus,1,1,n,1,n,d);
    update(segMinus,lazyMinus,1,1,n,1,n,d);
    ll p = query(segPlus,lazyPlus,1,1,n,x[i-1],x[i-1]);
    ll m = query(segMinus,lazyMinus,1,1,n,x[i-1],x[i-1]);
    minPlus = min(p, minPlus);
    minMinus = min(m, minMinus);
    update(segPlus,lazyPlus,1,1,n,x[i-1],x[i-1], minPlus-p);
    update(segMinus,lazyMinus,1,1,n,x[i-1],x[i-1], minMinus-m);
  }
  return ret;
}

void solve() {
  int n,q;scanf("%d %d %d %d ", &n,&q,&x[1],&x[0]);
  for(int i=2;i<q+2;++i) {
    scanf("%d  ", &x[i]);
  }
  q += 2;
  printf("%lld\n", min(solveMin(q,n),solveMin(q,n)));
}

int main() {
    //freopen("input.txt","r",stdin);
    solve();
    return 0;
}