#include <bits/stdc++.h>

#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define irrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define irrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)

#define fi first
#define se second

using namespace std;

using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 1000000007;
const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
    return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
    for(auto &v : vec) { is >> v; }
    return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
    for(auto &v : vec) { os << v << ",";}
    return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
    for(auto &row : mat) { os << row << endl; }
    return os;
}
template<typename T> inline
void chmin(T &a, T b) { a = min(a, b); }
template<typename T> inline
void chmax(T &a, T b) { a = max(a, b); }

int main(void)
{
    int n, m; 
    cin >> n >> m;

    v_int input(n+5, 0);
    
    irep(i, m) {
        int l, r;
        cin >> l >> r;
        input[l] += 1;
        input[r+1] -= 1;
    }
    
    v_int card(n+5, 0);
    irep2(i, 1, n) {
        card[i] = card[i-1] + input[i];
    }

    int ans = 0;
    irep2(i, 1, n) {
        if(card[i] == m) { 
            ans++;
        }
    }
    cout << ans << endl;

    return 0;
} /*atcoder*/

#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
#define ls(_o) (_o << 1)
#define rs(_o) ((_o << 1) | 1)
#define abs(_o) ((_o < 0) ? -(_o) : _o)
using namespace std;
namespace ywy {
    inline int get() {
        int n = 0;
        char c;
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                break;
            if (c == '-')
                goto s;
        }
        n = c - '0';
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                n = n * 10 + c - '0';
            else
                return (n);
        }
    s:
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                n = n * 10 - c + '0';
            else
                return (n);
        }
    }
    ll min1[1000001], min2[1000001], adds[1000001];
    int x[200001];
    inline void down(int tree) {
        if (!adds[tree])
            return;
        ll c = adds[tree];
        adds[tree] = 0;
        adds[ls(tree)] += c;
        adds[rs(tree)] += c;
        min1[ls(tree)] += c;
        min2[ls(tree)] += c;
        min1[rs(tree)] += c;
        min2[rs(tree)] += c;
    }
    inline void up(int tree) {
        min1[tree] = min(min1[ls(tree)], min1[rs(tree)]);
        min2[tree] = min(min2[ls(tree)], min2[rs(tree)]);
    }
    void setpt(int l, int r, int tree, int pt, ll num) {
        if (l == r) {
            min1[tree] = min(min1[tree], num - l);
            min2[tree] = min(min2[tree], num + l);
            return;
        }
        int mid = (l + r) >> 1;
        down(tree);
        if (pt <= mid)
            setpt(l, mid, ls(tree), pt, num);
        else
            setpt(mid + 1, r, rs(tree), pt, num);
        up(tree);
    }
    ll query1(int rl, int rr, int l, int r, int tree) {
        if (rl == l && rr == r)
            return (min1[tree]);
        int mid = (l + r) >> 1;
        down(tree);
        if (rl > mid)
            return (query1(rl, rr, mid + 1, r, rs(tree)));
        if (rr <= mid)
            return (query1(rl, rr, l, mid, ls(tree)));
        return (min(query1(rl, mid, l, mid, ls(tree)), query1(mid + 1, rr, mid + 1, r, rs(tree))));
    }
    ll query2(int rl, int rr, int l, int r, int tree) {
        if (rl == l && rr == r)
            return (min2[tree]);
        int mid = (l + r) >> 1;
        down(tree);
        if (rl > mid)
            return (query2(rl, rr, mid + 1, r, rs(tree)));
        if (rr <= mid)
            return (query2(rl, rr, l, mid, ls(tree)));
        return (min(query2(rl, mid, l, mid, ls(tree)), query2(mid + 1, rr, mid + 1, r, rs(tree))));
    }
    ll query(int l, int r, int tree) {
        if (l == r)
            return (min1[tree] + l);
        int mid = (l + r) >> 1;
        down(tree);
        return (min(query(l, mid, ls(tree)), query(mid + 1, r, rs(tree))));
    }
    void ywymain() {
        int n = get(), q = get(), a = get(), b = get();
        x[0] = a;
        memset(min1, 0x3f, sizeof(min1));
        memset(min2, 0x3f, sizeof(min2));
        setpt(1, n, 1, b, 0);
        for (register int i = 1; i <= q; i++) {
            x[i] = get();
            ll cjr = min(query1(1, x[i], 1, n, 1) + x[i], query2(x[i], n, 1, n, 1) - x[i]);
            adds[1] += abs(x[i] - x[i - 1]);
            min1[1] += abs(x[i] - x[i - 1]);
            min2[1] += abs(x[i] - x[i - 1]);
            setpt(1, n, 1, x[i - 1], cjr);
        }
        cout << query(1, n, 1) << endl;
    }
}
int main() {
    ywy::ywymain();
    return (0);
}