#include<iostream>
#include<algorithm>

using namespace std;
long long a(long long b)
{
    return b < 0? -b: b;
}
int main()
{
    long long k;
    long long mn, mx;
    long long n;
    long long x;
    cin >> n >> x;
    cin >> k;
    mn = k;
    mx = k;
    for (long long i = 1; i < x; i ++)
    {
        cin >> k;
        mn = min(mn, k);
        mx = max(mx, k);
    }
    if(n <= mn)
        cout << mx;
    if(n >= mx)
        cout << mn;
    else
        cout << a(mn - n) + a(mx - n) + min(a(mx - n),a(mn - n));
}