#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<bitset>
#include<map>

#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)

using namespace std;

typedef long long LL;
typedef double db;

int get(){
	char ch;
	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
	if (ch=='-'){
		int s=0;
		while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
		return -s;
	}
	int s=ch-'0';
	while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
	return s;
}

const int N = 2e+5+10;

int n,q,sa,sb;
int a[N];
struct node{
	LL val,vala,valb,ad;
	int l,r;
}A[N*2],B[N*2];
int tot;

LL minll(LL x,LL y){return x<y?x:y;}

void update(node &a,node l,node r){
	a.val=minll(l.val,r.val);
	a.vala=minll(l.vala,r.vala);
	a.valb=minll(l.valb,r.valb);
}

void build(int &now,int l,int r){
	now=++tot;
	A[now].ad=B[now].ad=0;
	if (l==r){
		if (l==a[1])B[now].val=0;
		else B[now].val=1e+12;
		A[now].val=1e+12;
		A[now].vala=A[now].val-l,A[now].valb=A[now].val+l;
		B[now].vala=B[now].val-l,B[now].valb=B[now].val+l;
		return;
	}
	int mid=(l+r)/2;
	build(A[now].l,l,mid);
	build(A[now].r,mid+1,r);
	B[now].l=A[now].l,B[now].r=A[now].r;
	update(A[now],A[A[now].l],A[A[now].r]);
	update(B[now],B[B[now].l],B[B[now].r]);
}

void down(node &a,node &l,node &r){
	if (a.ad){
		l.ad+=a.ad,l.val+=a.ad,l.vala+=a.ad,l.valb+=a.ad;
		r.ad+=a.ad,r.val+=a.ad,r.vala+=a.ad,r.valb+=a.ad;
		a.ad=0;
	}
}

LL getvalA(int now,int l,int r,int x){
	if (l==r)return A[now].val;
	int mid=(l+r)/2;
	down(A[now],A[A[now].l],A[A[now].r]);
	if (x<=mid)return getvalA(A[now].l,l,mid,x);
	return getvalA(A[now].r,mid+1,r,x);
}

LL getvalaA(int now,int l,int r,int x,int y){
	if (x<=l&&r<=y)return A[now].vala;
	int mid=(l+r)/2;
	LL val=1e+13;
	down(A[now],A[A[now].l],A[A[now].r]);
	if (x<=mid)val=minll(val,getvalaA(A[now].l,l,mid,x,y));
	if (y>mid)val=minll(val,getvalaA(A[now].r,mid+1,r,x,y));
	return val;
}

LL getvalbA(int now,int l,int r,int x,int y){
	if (x<=l&&r<=y)return A[now].valb;
	int mid=(l+r)/2;
	LL val=1e+13;
	down(A[now],A[A[now].l],A[A[now].r]);
	if (x<=mid)val=minll(val,getvalbA(A[now].l,l,mid,x,y));
	if (y>mid)val=minll(val,getvalbA(A[now].r,mid+1,r,x,y));
	return val;
}

LL getvalB(int now,int l,int r,int x){
	if (l==r)return B[now].val;
	int mid=(l+r)/2;
	down(B[now],B[B[now].l],B[B[now].r]);
	if (x<=mid)return getvalB(B[now].l,l,mid,x);
	return getvalB(B[now].r,mid+1,r,x);
}

LL getvalaB(int now,int l,int r,int x,int y){
	if (x<=l&&r<=y)return B[now].vala;
	int mid=(l+r)/2;
	LL val=1e+13;
	down(B[now],B[B[now].l],B[B[now].r]);
	if (x<=mid)val=minll(val,getvalaB(B[now].l,l,mid,x,y));
	if (y>mid)val=minll(val,getvalaB(B[now].r,mid+1,r,x,y));
	return val;
}

LL getvalbB(int now,int l,int r,int x,int y){
	if (x<=l&&r<=y)return B[now].valb;
	int mid=(l+r)/2;
	LL val=1e+13;
	down(B[now],B[B[now].l],B[B[now].r]);
	if (x<=mid)val=minll(val,getvalbB(B[now].l,l,mid,x,y));
	if (y>mid)val=minll(val,getvalbB(B[now].r,mid+1,r,x,y));
	return val;
}

void changevalA(int now,int l,int r,int x,LL v){
	if (l==r){
		A[now].val=v;
		A[now].vala=A[now].val-l,A[now].valb=A[now].val+l;
		return;
	}
	int mid=(l+r)/2;
	down(A[now],A[A[now].l],A[A[now].r]);
	if (x<=mid)changevalA(A[now].l,l,mid,x,v);
	else changevalA(A[now].r,mid+1,r,x,v);
	update(A[now],A[A[now].l],A[A[now].r]);
}

void changevalB(int now,int l,int r,int x,LL v){
	if (l==r){
		B[now].val=v;
		B[now].vala=B[now].val-l,B[now].valb=B[now].val+l;
		return;
	}
	int mid=(l+r)/2;
	down(B[now],B[B[now].l],B[B[now].r]);
	if (x<=mid)changevalB(B[now].l,l,mid,x,v);
	else changevalB(B[now].r,mid+1,r,x,v);
	update(B[now],B[B[now].l],B[B[now].r]);
}

int main(){
	n=get();q=get();sa=get();sb=get();
	fo(i,3,q+2)a[i]=get();
	a[1]=sa;a[2]=sb;
	q+=2;
	int rt;
	build(rt,1,n);
	fo(i,2,q-1){
		int c=abs(a[i+1]-a[i]);
		LL tmpB=minll(getvalaA(1,1,n,1,a[i+1])+a[i+1],getvalbA(1,1,n,a[i+1],n)-a[i+1]),tmpA=minll(getvalaB(1,1,n,1,a[i+1])+a[i+1],getvalbB(1,1,n,a[i+1],n)-a[i+1]);
		A[1].ad+=c,A[1].val+=c,A[1].vala+=c,A[1].valb+=c;
		B[1].ad+=c,B[1].val+=c,B[1].vala+=c,B[1].valb+=c;
		LL tmp=getvalB(1,1,n,a[i]);
		tmp=minll(tmp,tmpB);
		changevalB(1,1,n,a[i],tmp);
		tmp=getvalA(1,1,n,a[i]);
		tmp=minll(tmp,tmpA);
		changevalA(1,1,n,a[i],tmp);
	}
	printf("%lld\n",minll(A[1].val,B[1].val));
	return 0;
}

Measured memory_bytes: 32256