#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
typedef long long   ll;
#define pb                push_back
#define mp                make_pair
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define mem(a,h)          memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a,b) (((a%MOD) * (b%MOD))%MOD)
#define forg(i, b, e, c)    for (ll i = (ll)b; i < (ll)e; i+=c)
#define forr(i, b, e)    for (ll i = b; i < e; i++)
 
 
using namespace std;
//using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll>  ii;
typedef pair<ii, ll>  iii;
typedef vector<ll>     vi;
typedef vector<vi>     vvi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll INF = 1e18;
const double PI = acos(-1);
#define tam 1620000
#define offset 
const ll MOD= 1e9+7;
#define initseg int new_nodo=(pnodo*2),mid=(iz+der)/2;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct nodo
{
    ll vleft;
    ll vright;
    ll minn;
    ll lazy;
    nodo(){}
};
nodo tree[tam];
ll vsort[tam];
        
        ll newnod;
void push(ll pnodo)
{
    if (tree[pnodo].lazy==0)
        return;
    newnod=pnodo*2;

    tree[pnodo].vleft+=tree[pnodo].lazy;
    tree[pnodo].vright+=tree[pnodo].lazy;
    tree[pnodo].minn+=tree[pnodo].lazy;

    
    if (newnod<tam)
    tree[newnod].lazy+=tree[pnodo].lazy;
    if (newnod<tam)
    tree[newnod+1].lazy+=tree[pnodo].lazy;
    tree[pnodo].lazy=0;
}
void join(ll pnodo)
{
    push(pnodo*2);
    push(pnodo*2+1);
    newnod=pnodo*2;
    
    tree[pnodo].vleft=min(tree[newnod].vleft,tree[newnod+1].vleft);
    tree[pnodo].vright=min(tree[newnod].vright,tree[newnod+1].vright);
    tree[pnodo].minn=min(tree[newnod].minn,tree[newnod+1].minn);
}

void update(ll pnodo,ll iz,ll der,ll pos,ll valor)
{
    push(pnodo);
    if (iz==der)
    {
        tree[pnodo].vleft=min(tree[pnodo].vleft,valor-vsort[iz]);
        tree[pnodo].vright=min(tree[pnodo].vright,valor+vsort[iz]);
        tree[pnodo].minn=min(tree[pnodo].minn,valor);
        return;
    }
    initseg
    if (pos<=mid)
        update(new_nodo,iz,mid,pos,valor);
    else
        update(new_nodo+1,mid+1,der,pos,valor);
    join(pnodo);
}

    ll minleft,minright;// inicializar
    #define clrqry minleft=INF,minright=INF;
void query(ll pnodo,ll iz,ll der,ll li,ll ri)
{
    push(pnodo);
    if (iz>ri|| der<li)
        return;
    if (iz>=li && der<=ri){
        minleft=min(minleft,tree[pnodo].vleft);
        minright=min(minright,tree[pnodo].vright);
        return;
    }
    initseg
    query(new_nodo,iz,mid,li,ri);
    query(new_nodo+1,mid+1,der,li,ri);
}

/*void go(ll pnodo,ll iz,ll der)
{
    push(pnodo);
    if (iz==der)
    {
        cout<<tree[pnodo].minn<<"   ";
        return;
    }
    initseg;
    go(new_nodo,iz,mid);
    go(new_nodo+1,mid+1,der);
}*/
ll v[tam];
map<ii,ll> mapa;
int  main()
{
    forr(i,0,tam)
    {
        tree[i].minn=tree[i].vleft=tree[i].vright=INF;
        tree[i].lazy=0;
    }
    ios::sync_with_stdio(false);cin.tie(0);
    ll n,q;
    cin>>n>>q;
    cin>>v[1]>>v[2];
    forr(i,0,q)
    {
        cin>>v[i+3];
    }
    vector<ii> lst;
    forr(i,1,q+3)
    {
        lst.pb({v[i],i});
    }
    ll N=lst.size();
    sort(lst.begin(),lst.end());
    ll cnt=1;
    for(auto par:lst)
    {
        vsort[cnt]=par.fi;
       // cout<<par.fi<<' '<<par.se<<' '<<cnt<<endl;
        mapa[par]=cnt++;
    }   
    //return 0;
    update(1,1,N,mapa[{v[1],1}],0);
            
    ll valiz,valder,valupd;
    forr(i,3,q+3)
    {
        
        ll postree=mapa[{v[i],i}];
            
        //cout<<postree<<" update this pos: "<<endl;
        clrqry;
        query(1,1,N,1,postree);     valiz=minleft;
        clrqry;
        query(1,1,N,postree,N);     valder=minright;
        
        valupd=min(valiz+v[i],valder-v[i]);
        //valupd=min(valupd,INF);
        //cout<<valiz<<" "<<valder<<' ';
        //cout<<valupd<<endl;
        tree[1].lazy+=abs(v[i]-v[i-1]);
        postree=mapa[{v[i-1],i-1}];
        update(1,1,N,postree,valupd);
        //cout<<"final\n";
        //cout<<tree[1].minn<<endl;

        
            

    }
    push(1);
    cout<<tree[1].minn<<endl;

}
// PLUS ULTRA!ac

Measured memory_bytes: 71788