#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <cctype>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <stdint.h>
#include <stdio.h>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <tuple>
#include <unordered_set>
#include <assert.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define TR(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
#define ALL(x) x.begin(),x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define FILL(x,c) memset(x,c,sizeof(x))

using namespace std;

#define PB push_back
#define MP make_pair

typedef map<int,int> MII;
typedef map<string,int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int,int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;
typedef unsigned long long ULL;

const int N = 400007;

LL delta[N * 4], max1[N * 4], max2[N * 4], max0[N * 4];

void up(int x) {
    max0[x] = min(max0[x * 2], max0[x * 2 + 1]);
    max1[x] = min(max1[x * 2], max1[x * 2 + 1]);
    max2[x] = min(max2[x * 2], max2[x * 2 + 1]);
}

void down(int x) {
    if (delta[x] == 0) {
        return;
    }
    delta[x * 2] += delta[x];
    max0[x * 2] += delta[x];
    max1[x * 2] += delta[x];
    max2[x * 2] += delta[x];
    delta[x * 2 + 1] += delta[x];
    max0[x * 2 + 1] += delta[x];
    max1[x * 2 + 1] += delta[x];
    max2[x * 2 + 1] += delta[x];
    delta[x] = 0;
}

void build(int left, int right, int x) {
    if (left != right) {
        int mid = (left + right) >> 1;
        build(left, mid, x * 2);
        build(mid + 1, right, x * 2 + 1);
        up(x);
        return;
    }
    max0[x] = 0x3F3F3F3F3F3F3F3FLL;
    max1[x] = 0x3F3F3F3F3F3F3F3FLL + left;
    max2[x] = 0x3F3F3F3F3F3F3F3FLL - left;
}

void add(int left, int right, int l, int r, int x, int val) {
    if (l <= left && r >= right) {
        delta[x] += val;
        max0[x] += val;
        max1[x] += val;
        max2[x] += val;
        return;
    }
    down(x);
    int mid = (left + right) >> 1;
    if (l <= mid) add(left, mid, l, r, x * 2, val);
    if (r > mid) add(mid + 1, right, l, r, x * 2 + 1, val);
    up(x);
}

LL get(int left, int right, int l, int r, int x, LL* maxv) {
    if (l <= left && r >= right) {
        return maxv[x];
    }
    down(x);
    int mid = (left + right) >> 1;
    LL ret = 0x3F3F3F3F3F3F3F3FLL;
    if (l <= mid) {
        ret = min(ret, get(left, mid, l, r, x * 2, maxv));
    }
    if (r > mid) {
        ret = min(ret, get(mid + 1, right, l, r, x * 2 + 1, maxv));
    }
    return ret;
}

void update(int left, int right, int at, int x, LL val) {
    if (left == right) {
        if (max0[x] > val) {
            max0[x] = val;
            max1[x] = val + left;
            max2[x] = val - left;
        }
        return;
    }
    down(x);
    int mid = (left + right) >> 1;
    if (at <= mid) {
        update(left, mid, at, x * 2, val);
    } else {
        update(mid + 1, right, at, x * 2 + 1, val);
    }
    up(x);
}

int main() {
    int n, q, a, b;
    cin >> n >> q >> a >> b;
    --a; --b;
    build(0, n - 1, 1);
    update(0, n - 1, a, 1, 0);
    int last = b;
    while (q--) {
        int x;
        scanf("%d", &x);
        --x;
        LL cost = 0x3F3F3F3F3F3F3F3FLL;
        LL best1 = get(0, n - 1, x, n - 1, 1, max1) - x;
        LL best2 = get(0, n - 1, 0, x, 1, max2) + x;
        cost = min(best1, best2);

        add(0, n - 1, 0, n - 1, 1, abs(x - last));
        update(0, n - 1, last, 1, cost);
        last = x;
    }
    cout << get(0, n - 1, 0, n - 1, 1, max0) << endl;
    return 0;
}

Measured memory_bytes: 24960