#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] == 0 && b[y][i] != m - 1)
            j += abs(x - d[b[y][i] + 1]);
        else if (b[y][i] != m - 1)
            j += (abs(x - d[b[y][i] + 1]) + abs(x - d[b[y][i] - 1]));
        else if (i != 0)
            j += abs(x - d[b[y][i] - 1]);
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, t - (k - j));
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    if (k - j >= 0)
        return (- (k - j));
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m1;
            else
                l = m2;
        }
    }
    for (i = l; i <= r; i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = t;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] != 0)
            j += (- abs(a[b[y][i] - 1] - y) + abs(a[b[y][i] - 1] - x));
        if (b[y][i] != m - 1)
            j += (- abs(a[b[y][i] + 1] - y) + abs(a[b[y][i] + 1] - x));
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, j);
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    return (j);
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m1;
            else
                l = m2;
        }
    }
    long long k = 1;
    long long g = n;
    for (i = max(l - 5, k); i <= min(r + 5, g); i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
   // for (i = 0; i < m; i ++)
      //  cout << d[i] << " ";
       // cout << endl;
    j = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] == 0 && b[y][i] != m - 1)
            j += abs(x - d[b[y][i] + 1]);
        else if (b[y][i] != m - 1)
            j += (abs(x - d[b[y][i] + 1]) + abs(x - d[b[y][i] - 1]));
        else if (i != 0)
            j += abs(x - d[b[y][i] - 1]);
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, t - (k - j));
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    if (k - j >= 0)
        return (- (k - j));
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        if (i != m - 1)
            b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m1;
            else
                l = m2;
        }
    }
    for (i = l; i <= r; i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = t;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] != 0)
            j += (- abs(a[b[y][i] - 1] - y) + abs(a[b[y][i] - 1] - x));
        if (b[y][i] != m - 1)
            j += (- abs(a[b[y][i] + 1] - y) + abs(a[b[y][i] + 1] - x));
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, j);
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    return (j);
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m1;
            else
                l = m2;
        }
    }
    for (i = l - 5; i <= r + 5; i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <int> > b(200000);

long long f(int x, int y)
{
    k = t;
    for (int i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] > 0)
            k += (abs(a[b[y][i] - 1] - x) - abs(a[b[y][i] - 1] - y));
        if (b[y][i] != m - 1)
            k += (abs(a[b[y][i] + 1] - x) - abs(a[b[y][i] + 1] - y));
    }
    ans = min(ans, k);
    return k;
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }

    for (i = 0; i < m - 1; i ++)
        ans += abs(a[i] - a[i + 1]);
    t = ans;

    for (i = 1; i <= n; i ++)
        if (b[i].size() > 0)
        {
            l = 0;
            r = n + 5;
            while (l + 2 < r)
            {
                m1 = l + (r - l) / 3;
                m2 = r - (r - l) / 3;
                if (f(m1, i) < f(m2, i))
                    r = m1;
                else
                    l = m2;
            }
        }
    for (i = l; i <= r; i ++)
        for (j = 1; j <= n; j ++)
            f(i, j);
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] == 0 && b[y][i] != m - 1)
            j += abs(x - d[b[y][i] + 1]);
        else if (b[y][i] != m - 1)
            j += (abs(x - d[b[y][i] + 1]) + abs(x - d[b[y][i] - 1]));
        else if (b[y][i] != 0)
            j += abs(x - d[b[y][i] - 1]);
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, t - (k - j));
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    return (t - (k - j));
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m2;
            else
                l = m1;
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] == 0 && b[y][i] != m - 1)
            j += abs(x - d[b[y][i] + 1]);
        else if (b[y][i] != m - 1)
            j += (abs(x - d[b[y][i] + 1]) + abs(x - d[b[y][i] - 1]));
        else if (b[y][i] != 0)
            j += abs(x - d[b[y][i] - 1]);
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, t - (k - j));
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    return (t - (k - j));
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m1;
            else
                l = m2;
        }
    }
    for (i = l; i <= r; i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

long long i, j, n, m, k, a[200000], ans, m1, m2, l, r, c[200000], t, d[200000];
vector <vector <long long> > b(200000);

long long f(long long x, long long y)
{
    long long i, j, k;
    k = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        k += c[b[y][i]];
        d[b[y][i]] = x;
    }
    //for (i = 0; i < m; i ++)
        //cout << d[i] << " ";
        //cout << endl;
    j = 0;
    for (i = 0; i < b[y].size(); i ++)
    {
        if (b[y][i] == 0 && b[y][i] != m - 1)
            j += abs(x - d[b[y][i] + 1]);
        else if (b[y][i] != m - 1)
            j += (abs(x - d[b[y][i] + 1]) + abs(x - d[b[y][i] - 1]));
        else if (b[y][i] != 0)
            j += abs(x - d[b[y][i] - 1]);
    }
    //cout << x << " " << y << " " << k << " " << j << endl;
    ans = min(ans, t - (k - j));
    for (i = 0; i < b[y].size(); i ++)
        d[b[y][i]] = a[b[y][i]];
    return (t - (k - j));
}

int main()
{
    cin >> n >> m;
    for (i = 0; i < m; i ++)
    {
        scanf("%I64d", &a[i]);
        b[a[i]].push_back(i);
        d[i] = a[i];
    }
    ans = 0;
    for (i = 0; i < m; i ++)
    {
        if (i != m - 1)
            ans = ans + abs(a[i + 1] - a[i]);
        if (i == 0 && i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]));
        else if (i != m - 1)
            c[i] += (abs(a[i + 1] - a[i]) + abs(a[i - 1] - a[i]));
        else if (i != 0)
            c[i] += (abs(a[i - 1] - a[i]));
    }
    //for (i = 0; i < n - 1; i ++)
        //cout << c[i] << " ";
    t = ans;
    //cout << ans << endl;
    for (i = 1; i <= n; i ++)
    {
        l = 0;
        r = n + 5;
        while (r - l > 2)
        {
            m1 = l + (r - l) / 3;
            m2 = r - (r - l) / 3;
            if (f(m1, i) < f(m2, i))
                r = m2;
            else
                l = m1;
        }
    }
    for (i = l; i <= r; i ++)
    {
        for (j = 1; j <= n; j ++)
            f(j, i);
    }
    cout << ans;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += (ll)abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUG
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += (ll)abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0) { cost -= (ll)abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m) { cost -= (ll)abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += (ll)abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
ll c[maxn];

vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		c[i] = (i == 0) ? 0 : ((ll)abs(a[i] - a[i-1]) + c[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (sz(my[a[i]]) == 0) {
			continue;
		}
		int last = 0;
		ll cost = 0;
		vi stuffs;
		rep(j, sz(my[a[i]])) {
			int idx = my[a[i]][j];
			if (idx-1 >= 0) {
				cost += c[idx-1] - c[last];
				stuffs.push_back(a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
			}
			if (j == sz(my[a[i]])-1 && idx+1 < m) {
				cost += c[m-1] - c[idx+1];
			}
			last = idx+1;
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0 && a[idx-1] != i) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m && a[idx+1] != i) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		if (tmp.empty()) {
			printf("0\n");
			return 0;
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0 && a[idx-1] != i) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m && a[idx+1] != i) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
ll c[maxn];

vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		c[i] = (i == 0) ? 0 : ((ll)abs(a[i] - a[i-1]) + c[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (sz(my[i]) == 0) {
			continue;
		}
		int last = 0;
		ll cost = 0;
		vi stuffs;
		rep(j, sz(my[i])) {
			int idx = my[i][j];
			if (idx-1 >= 0) {
				cost += c[idx-1] - c[last];
				stuffs.push_back(a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
			}
			if (j == sz(my[i])-1 && idx+1 < m) {
				cost += c[m-1] - c[idx+1];
			}
			last = idx+1;
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += (ll)abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0) { cost -= (ll)abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m) { cost -= (ll)abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += (ll)abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
ll c[maxn];

vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		c[i] = (i == 0) ? 0 : ((ll)abs(a[i] - a[i-1]) + c[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) { //i is the chosen page
		if (sz(my[i]) == 0) {
			continue;
		}
		ll cost = c[m-1];
		vi stuffs;
		rep(j, sz(my[i])) {
			int idx = my[i][j]; //idx is where the chosen is
			if (idx-1 >= 0) {
				stuffs.push_back(a[idx-1]);
				cost -= (ll)abs(i - a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
				cost -= (ll)abs(a[idx+1] - i);
			}
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2-1];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
ll c[maxn];

vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		c[i] = (i == 0) ? 0 : ((ll)abs(a[i] - a[i-1]) + c[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) { //i is the chosen page
		if (sz(my[i]) == 0) {
			continue;
		}
		ll cost = c[m-1];
		vi stuffs;
		rep(j, sz(my[i])) {
			int idx = my[i][j]; //idx is where the chosen is
			if (idx-1 >= 0) {
				stuffs.push_back(a[idx-1]);
				cost -= (ll)abs(i - a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
				cost -= (ll)abs(a[idx+1] - i);
			}
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUG
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &m, &n);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += (ll)abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0) { cost -= (ll)abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m) { cost -= (ll)abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += (ll)abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
ll c[maxn];

vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		c[i] = (i == 0) ? 0 : ((ll)abs(a[i] - a[i-1]) + c[i-1]);
	}

	ll mincost = 1e15;
	rep(i, n) {
		if (sz(my[a[i]]) == 0) {
			continue;
		}
		int last = 0;
		ll cost = 0;
		vi stuffs;
		rep(j, sz(my[a[i]])) {
			int idx = my[a[i]][j];
			if (idx-1 >= 0) {
				cost += c[idx-1] - c[last];
				stuffs.push_back(a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
			}
			if (j == sz(my[a[i]])-1 && idx+1 < m) {
				cost += c[m-1] - c[idx+1];
			}
			last = idx+1;
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


//Template
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long ll;

#define N 100000
int n, m, a[N + 1];
vector <int> b[N + 1];

int main(int argc, char *argv[]) {
#ifdef KANARI
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) cin >> a[i];
	for (int i = 1; i <= m; ++i) {
		if (i > 1 && a[i - 1] != a[i]) b[a[i - 1]].push_back(a[i]);
		if (i < m && a[i + 1] != a[i]) b[a[i + 1]].push_back(a[i]);
	}

	ll ans = 0LL, sum = 0LL;
	for (int i = 1; i <= n; ++i) {
		if (!b[i].size()) continue;
		sort(b[i].begin(), b[i].end());
		int y = b[i][b[i].size() >> 1];
		ll before = 0LL, after = 0LL;
		for (int j = 0; j < b[i].size(); ++j) {
			before += abs(i - b[i][j]);
			after += abs(y - b[i][j]);
		}
		ans = max(ans, before - after), sum += before;
	}

	cout << sum / 2 - ans << endl;

	fclose(stdin);
	fclose(stdout);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0 && a[idx-1] != i) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m && a[idx+1] != i) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		if (tmp.empty()) continue;
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

const int maxn = 1e5+5;
int n, m;
int a[maxn];
vi my[maxn];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	ll total = 0;

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if(i>0)
			total += (ll)abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) { //i is the chosen page
		if (sz(my[i]) == 0) {
			continue;
		}
		ll cost = total;
		vi stuffs;
		rep(j, sz(my[i])) {
			int idx = my[i][j];
			if (idx-1 >= 0) {
				stuffs.push_back(a[idx-1]);
				cost -= (ll)abs(i - a[idx-1]);
			}
			if (idx+1 < m) {
				stuffs.push_back(a[idx+1]);
				cost -= (ll)abs(a[idx+1] - i);
			}
		}
		sort(all(stuffs));
		int best = stuffs[(int)stuffs.size()/2];
		rep(j, sz(stuffs)) {
			cost += (ll)abs(stuffs[j] - best);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d\n", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	if (m == 1 || n == 1) {
		printf("0\n");
		return;
	}
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0 && a[idx-1] != i) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m && a[idx+1] != i) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>

#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <bitset>
#include <list>
#include <stack>

#include <functional>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define mp make_pair
#define pb push_back
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define repr(i, l, r) for(int (i) = (l); (i) < (r); ++(i))
#define clr(t, v) memset((t), (v), sizeof(t))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second

#define DEBUG

int m, n;
ll total;
int a[(int)1e5+5];
vi my[(int)1e5+5];

int main() {
#ifdef DEBUGx
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);

	scanf("%d%d", &n, &m);
	rep(i, m) {
		scanf("%d", &a[i]);
		a[i]--;
		my[a[i]].push_back(i);
		if (i > 0) total += abs(a[i] - a[i-1]);
	}

	ll mincost = 1e17;
	rep(i, n) {
		if (my[i].empty()) continue;

		ll cost = total;
		vi tmp;
		rep(j, my[i].size()) {
			int idx = my[i][j];
			if (idx-1 >= 0) { cost -= abs(i - a[idx-1]); tmp.push_back(a[idx-1]); }
			if (idx+1 < m) { cost -= abs(i - a[idx+1]); tmp.push_back(a[idx+1]); }
		}
		sort(tmp.begin(), tmp.end());
		int n = tmp[tmp.size()>>1];
		rep(j, tmp.size()) {
			cost += abs(n - tmp[j]);
		}
		mincost = min(mincost, cost);
	}
	printf("%I64d", mincost);

	return 0;
}


#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node{int x;int y;};
bool cmp(node a,node b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int n,m;
	cin>>m>>n;
	int a[n],ans=0,num=0,res=0;
	node s[n*2];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)if(a[i]!=a[i-1])
	{
		ans+=abs(a[i]-a[i-1]);
		s[num].x=a[i];s[num++].y=a[i-1];
		s[num].y=a[i];s[num++].x=a[i-1];
	}
	sort(s,s+num,cmp);
	for(int i=0;i<num;i++)cout<<s[i].x<<" "<<s[i].y<<endl;
	//cout<<num;
	cout<<ans<<endl;
	for(int i=0,j=1;i<num;i=j,j++)
	{
		int ret1=0,ret2=0,ret;
		for(;s[j].x==s[j-1].x;j++);
		for(int k=i;k<j;k++)ret1+=abs(s[k].y-s[k].x);
		cout<<ret1<<" ";
		for(int k=0;k<j-i;k++)ret2+=abs(s[i+k].y-s[j-k-1].y);
		cout<<ret2<<" ";
		ret=ret1-ret2/2;
		res=max(res,ret);
		cout<<res<<endl;
	}
	cout<<ans-res<<endl;
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node{int x;int y;};
bool cmp(node a,node b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int n,m;
	cin>>m>>n;
	int a[n],ans=0,num=0,res=0;
	node s[n*2];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)if(a[i]!=a[i-1])
	{
		ans+=abs(a[i]-a[i-1]);
		s[num].x=a[i];s[num++].y=a[i-1];
		s[num].y=a[i];s[num++].x=a[i-1];
	}
	sort(s,s+num,cmp);
	//for(int i=0;i<num;i++)cout<<s[i].x<<" "<<s[i].y<<endl;
	//cout<<num;
	//cout<<ans<<endl;
	for(int i=0,j=1;i<num;i=j,j++)
	{
		int ret1=0,ret2=0,ret;
		for(;s[j].x==s[j-1].x;j++);
		for(int k=i;k<j;k++)ret1+=abs(s[k].y-s[k].x);
		//cout<<ret1<<" ";
		for(int k=0;k<j-i;k++)ret2+=abs(s[i+k].y-s[j-k-1].y);
		//cout<<ret2<<" ";
		ret=ret1-ret2/2;
		res=max(res,ret);
		//cout<<res<<endl;
	}
	cout<<ans-res<<endl;
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node{int x;int y;};
bool cmp(node a,node b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int n,m;
	cin>>m>>n;
	int a[n],ans=0,num=0,res=0;
	node s[n*2];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)if(a[i]!=a[i-1])
	{
		ans+=abs(a[i]-a[i-1]);
		s[num].x=a[i];s[num++].y=a[i-1];
		s[num].y=a[i];s[num++].x=a[i-1];
	}
	sort(s,s+num,cmp);
	//for(int i=0;i<num;i++)cout<<s[i].x<<" "<<s[i].y<<endl;
	//cout<<num;
	for(int i=0,j=1;i<num;i=j,j++)
	{
		int ret=0;
		for(;s[j].x==s[j-1].x;j++);
		for(int k=i;k<j;k++)ret+=abs(s[k].y-s[k].x);
		//cout<<ret<<" ";
		for(int k=0;k<j-i;k++)ret-=abs(s[i+k].y-s[j-k-1].y);
		//cout<<ret<<" ";
		res=max(res,ret);
		//cout<<res<<endl;
	}
	cout<<ans-res<<endl;
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node{int x;int y;};
bool cmp(node a,node b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int n,m;
	cin>>m>>n;
	int a[n],ans=0,num=0,res=0;
	node s[n*2];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)if(a[i]!=a[i-1])
	{
		ans+=abs(a[i]-a[i-1]);
		s[num].x=a[i];s[num++].y=a[i-1];
		s[num].y=a[i];s[num++].x=a[i-1];
	}
	sort(s,s+num,cmp);
	for(int i=0;i<num;i++)cout<<s[i].x<<" "<<s[i].y<<endl;
	//cout<<num;
	cout<<ans<<endl;
	for(int i=0,j=1;i<num;i=j,j++)
	{
		int ret1=0,ret2=0,ret;
		for(;s[j].x==s[j-1].x;j++);
		for(int k=i;k<j;k++)ret1+=abs(s[k].y-s[k].x);
		cout<<ret1<<" ";
		for(int k=0;k<j-i;k++)ret2+=abs(s[i+k].y-s[j-k-1].y);
		cout<<ret2<<" ";
		ret=ret1-ret2/2+ret2%2;
		res=max(res,ret);
		cout<<res<<endl;
	}
	cout<<ans-res<<endl;
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
struct feng
{
	int s;
	int num;
};
int main()
{
	int n,m;
	cin>>m>>n;
	int a[n],j=1,ans=0,ret[m+1],t=0;
	for(int i=1;i<=m;i++)ret[i]=0;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)ans+=abs(a[i]-a[i-1]);
	feng s[n];
	s[0].s=abs(a[1]-a[0]),s[0].num=a[0];
	for(int i=1;i<n-1;i++)
	{
		if(a[i+1]<a[i]&&a[i]>a[i-1])
			s[j].num=a[i],s[j++].s=min(a[i]-a[i-1],a[i]-a[i+1])*2;
		if(a[i+1]>a[i]&&a[i]<a[i-1])
			s[j].num=a[i],s[j++].s=min(a[i-1]-a[i],a[i+1]-a[i])*2;
	}
	//cout<<ans<<endl;
	s[j].s=abs(a[n-2]-a[n-1]),s[j++].num=a[n-1];
	for(int i=0;i<j;i++)
	{
		//cout<<s[i].s<<" "<<s[i].num<<endl;
		ret[s[i].num]+=s[i].s;
	}
	//for(int i=1;i<=m;i++)cout<<ret[i]<<endl;
	for(int i=1;i<=m;i++)t=max(t,ret[i]);
	cout<<ans-t<<endl;
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node{int x;int y;};
bool cmp(node a,node b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int n,m;
	cin>>m>>n;
	long long a[n],ans=0,num=0,res=0;
	node s[n*2];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=1;i<n;i++)if(a[i]!=a[i-1])
	{
		ans+=abs(a[i]-a[i-1]);
		s[num].x=a[i];s[num++].y=a[i-1];
		s[num].y=a[i];s[num++].x=a[i-1];
	}
	sort(s,s+num,cmp);
	//for(int i=0;i<num;i++)cout<<s[i].x<<" "<<s[i].y<<endl;
	//cout<<num;
	//cout<<ans<<endl;
	for(int i=0,j=1;i<num;i=j,j++)
	{
		long long ret1=0,ret2=0,ret;
		for(;s[j].x==s[j-1].x;j++);
		for(int k=i;k<j;k++)ret1+=abs(s[k].y-s[k].x);
		//cout<<ret1<<" ";
		for(int k=0;k<j-i;k++)ret2+=abs(s[i+k].y-s[j-k-1].y);
		//cout<<ret2<<" ";
		ret=ret1-ret2/2;
		res=max(res,ret);
		//cout<<res<<endl;
	}
	cout<<ans-res<<endl;
	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MAXN = 100010;
int A[MAXN];
ll llabs(ll a) {return a < 0 ? -a : a;}

vector<int> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<int> &P, int f, int t) {
	return P[t] - P[f - 1];
}

ll countSumFor(int number, int newNumber) {
	const int smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const int larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	int N, M;
	cin >> N >> M;
	for (int i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (int i = 0; i < M - 1; i += 1) best += llabs(A[i] - A[i + 1]);
	const ll totalSum = best;

	for (int i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (int i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (int i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (int i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (int j = 1; j < (int)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (int i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		const ll sum = p(G[i][1], 1, G[i][0].size());
		const ll lower = countSumFor(i, max(min((ll)N, sum / (ll)G[i][0].size()), 1ll));
		const ll upper = countSumFor(i, max(min((ll)N, ceilDiv(sum, (ll)G[i][0].size())), 1ll));
		const ll byUsingLower = totalSum - cur + lower;
		const ll byUsingUpper = totalSum - cur + upper;
		best = min(min(best, byUsingLower), byUsingUpper);
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber - 1) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, G[i][0][G[i][0].size() / 2]));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	if (t < f) return 0;
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) {
		sort(G[i][0].begin(), G[i][0].end());
		pair<vector<ll>::iterator, vector<ll>::iterator> eq = equal_range(G[i][0].begin(), G[i][0].end(), i);
		G[i][0].erase(eq.first, eq.second);
	}
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, findBest(i)));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	if (t < f) return 0;
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) {
		sort(G[i][0].begin(), G[i][0].end());
		pair<vector<ll>::iterator, vector<ll>::iterator> eq = equal_range(G[i][0].begin(), G[i][0].end(), i);
		G[i][0].erase(eq.first, eq.second);
	}
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, G[i][0][G[i][0].size() / 2]));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	if (f < t) return 0;
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, G[i][0][G[i][0].size() / 2]));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		best = min(best, totalSum + findBest(i));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, G[i][0][G[i][0].size() / 2]));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	if (f > t) return 0;
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		best = min(best, totalSum + check(cur, i, G[i][0][G[i][0].size() / 2]));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}
ll N, M;
ll totalSum;

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	if (t < f) return 0;
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

ll check(ll cur, ll number, ll got) {
	return countSumFor(number, got) - cur;
}

ll findBest(ll number) {
	const ll cur = countSumFor(number, number);

	ll l = 1, h = N, m1, m2;
	while (l < h) {
		m1 = l + (h - l) / 3;
		m2 = h - (h - l) / 3;
		const ll vm1 = check(cur, number, m1);
		const ll vm2 = check(cur, number, m2);
		if (vm1 > vm2) l = m1 + 1;
		else if (vm1 < vm2) h = m2 - 1;
		else {
			l = m1;
			h = m2 - 1;
		}
	}
	return check(cur, number, l);
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) {
		sort(G[i][0].begin(), G[i][0].end());
		pair<vector<ll>::iterator, vector<ll>::iterator> eq = equal_range(G[i][0].begin(), G[i][0].end(), i);
		G[i][0].erase(eq.first, eq.second);
	}
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		best = min(best, totalSum + findBest(i));
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MAXN = 100010;
int A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}

vector<int> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<int> &P, int f, int t) {
	return P[t] - P[f - 1];
}

ll countSumFor(int number, int newNumber) {
	const int smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const int larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	int N, M;
	cin >> N >> M;
	for (int i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (int i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	const ll totalSum = best;

	for (int i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (int i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (int i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (int i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (int j = 1; j < (int)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (int i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		const ll sum = p(G[i][1], 1, G[i][0].size());
		const ll lower = countSumFor(i, max(min((ll)N, sum / (ll)G[i][0].size()), 1ll));
		const ll upper = countSumFor(i, max(min((ll)N, ceilDiv(sum, (ll)G[i][0].size())), 1ll));
		const ll byUsingLower = totalSum - cur + lower;
		const ll byUsingUpper = totalSum - cur + upper;
		best = min(min(best, byUsingLower), byUsingUpper);
	}

	cout << best << endl;

	return 0;
}


#include      <algorithm>
#include      <cmath>
#include      <cstdarg>
#include      <cstdio>
#include      <cstdlib>
#include      <iomanip>
#include      <iostream>
#include      <iterator>
#include      <limits>
#include      <list>
#include      <map>
#include      <set>
#include      <queue>
#include      <vector>
#define endl '\n'
#define each(c, e) for (typeof(c.begin()) e = c.begin(); e != c.end(); ++e)
using namespace std;
typedef long long ll;
typedef long double ld;

const ll MAXN = 100010;
ll A[MAXN];
ll lllabs(ll a) {return a < 0 ? -a : a;}

vector<ll> G[MAXN][2];

template<typename T> inline T ceilDiv(T l, T r) {return (l / r) + ((l % r) != 0);}

ll p(const vector<ll> &P, ll f, ll t) {
	return P[t] - P[f - 1];
}

ll countSumFor(ll number, ll newNumber) {
	const ll smaller = lower_bound(G[number][0].begin(), G[number][0].end(), newNumber) - G[number][0].begin();
	const ll larger = G[number][0].size() - smaller;
	return newNumber * smaller - p(G[number][1], 1, smaller) + p(G[number][1], smaller + 1, G[number][0].size()) - newNumber * larger;
}

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false), cin.tie(0);
	ll N, M;
	cin >> N >> M;
	for (ll i = 0; i < M; i += 1) cin >> A[i];
	ll best = 0;
	for (ll i = 0; i < M - 1; i += 1) best += lllabs(A[i] - A[i + 1]);
	const ll totalSum = best;

	for (ll i = 0; i < M - 1; i += 1) G[A[i]][0].push_back(A[i + 1]);
	for (ll i = 1; i < M; i += 1) G[A[i]][0].push_back(A[i - 1]);

	for (ll i = 1; i <= N; i += 1) sort(G[i][0].begin(), G[i][0].end());
	for (ll i = 1; i <= N; i += 1) {
		G[i][1].resize(G[i][0].size() + 1);
		for (ll j = 1; j < (ll)G[i][1].size(); j += 1) {
			G[i][1][j] = G[i][1][j - 1] + G[i][0][j - 1];
		}
	}

	for (ll i = 1; i <= N; i += 1) {
		if (!G[i][0].size()) continue;
		const ll cur = countSumFor(i, i);
		const ll sum = p(G[i][1], 1, G[i][0].size());
		const ll lower = countSumFor(i, max(min((ll)N, sum / (ll)G[i][0].size()), 1ll));
		const ll upper = countSumFor(i, max(min((ll)N, ceilDiv(sum, (ll)G[i][0].size())), 1ll));
		const ll byUsingLower = totalSum - cur + lower;
		const ll byUsingUpper = totalSum - cur + upper;
		best = min(min(best, byUsingLower), byUsingUpper);
	}

	cout << best << endl;

	return 0;
}


#include<stdio.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
vector <int> v[222222];
int a[222222];
int main()
{
	int n,m;
	int maxx=0;
	__int64 sum=0;
	__int64 _max=0;
	scanf("%d%d",&n,&m);
	int i,j;
	for(i=0;i<m;i++)
	{
		scanf("%d",&a[i]);
		maxx=max(maxx,a[i]);
		if(i!=0&&a[i]!=a[i-1])
		{
		sum+=abs(a[i]-a[i-1]);
		v[a[i]].push_back(a[i-1]);
		v[a[i-1]].push_back(a[i]);
		}

	}

	for(i=1;i<=maxx;i++)
	{
		__int64 temp=0;
		int t;
		if(v[i].size()==0)continue;

		sort(v[i].begin(),v[i].end());
		t=v[i][v[i].size()/2];
	    for(j=0;j<v[i].size();j++)
	    temp+=(abs(v[i][j]-i)-abs(v[i][j]-t));
	   _max=max(temp,_max);

	}

  	printf("%I64d\n",sum-_max);

 }


#include<stdio.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
vector <int> v[222222];
int a[222222];
int main()
{
	int n,m;
	int maxx=0;
	int sum=0;
	int _max=0;
	scanf("%d%d",&n,&m);
	int i,j;
	for(i=0;i<m;i++)
	{
		scanf("%d",&a[i]);
		maxx=max(maxx,a[i]);
		if(i!=0&&a[i]!=a[i-1])
		{
		sum+=abs(a[i]-a[i-1]);
		v[a[i]].push_back(a[i-1]);
		v[a[i-1]].push_back(a[i]);
		}

	}

	for(i=1;i<=maxx;i++)
	{
		int temp=0;
		int t;
		if(v[i].size()==0)continue;
		t=v[i][v[i].size()/2];
		sort(v[i].begin(),v[i].end());
	    for(j=0;j<v[i].size();j++)
	    temp+=(abs(v[i][j]-i)-abs(v[i][j]-t));
	   _max=max(temp,_max);

	}

  	printf("%d\n",sum-_max);

 }


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string.h>
int visit[222222];
using namespace std;
vector <int> v[222222];
int  main()
{
	memset(visit,0,sizeof(visit));
	int n,m;
	int sum=0;
	int tmep=0;
	scanf("%d%d",&n,&m);
	int a[222222];
	int i,j;
	int maxx=0;
    scanf("%d",&a[0]),v[a[0]].push_back(0);
	for(i=1;i<m;i++)
	scanf("%d",&a[i]),sum+=abs(a[i]-a[i-1]),v[a[i]].push_back(i);
	tmep=abs(a[1]-a[0]);
	for(i=1;i<v[a[0]].size();i++)
	tmep+=abs(a[v[a[0]][i]]-a[v[a[0]][i]-1])+abs(a[v[a[0]][i]]-a[v[a[0]][i]+1])-abs(a[v[a[0]][i]+1]-a[v[a[0]][i]-1]);
	maxx=tmep;
	for(i=1;i<m-1;i++)
	{
	if(!visit[a[i]])
	  {
	  	visit[a[i]]=1;
    	tmep=0;
	    for(j=0;j<v[a[i]].size();j++)
    	if(v[a[i]][j]!=m-1)
        tmep+=abs(a[v[a[i]][j]]-a[v[a[i]][j]-1])+abs(a[v[a[i]][j]]-a[v[a[i]][j]+1])-abs(a[v[a[i]][j]+1]-a[v[a[i]][j]-1]);
       else tmep+=abs(a[m-1]-a[m-2]);
   	  }
   	  maxx=max(tmep,maxx);

	}

	if(!visit[a[m-1]])
	maxx=max(maxx,abs(a[m-1]-a[m-2]));

	printf("%d\n",sum-maxx);

}


#include<stdio.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
vector <int> v[222222];
int a[222222];
int main()
{
	int n,m;
	int maxx=0;
	int sum=0;
	int _max=0;
	scanf("%d%d",&n,&m);
	int i,j;
	for(i=0;i<m;i++)
	{
		scanf("%d",&a[i]);
		maxx=max(maxx,a[i]);
		if(i!=0&&a[i]!=a[i-1])
		{
		sum+=abs(a[i]-a[i-1]);
		v[a[i]].push_back(a[i-1]);
		v[a[i-1]].push_back(a[i]);
		}

	}

	for(i=1;i<=maxx;i++)
	{
		int temp=0;
		int t;
		if(v[i].size()==0)continue;

		sort(v[i].begin(),v[i].end());
			t=v[i][v[i].size()/2];
	    for(j=0;j<v[i].size();j++)
	    temp+=(abs(v[i][j]-i)-abs(v[i][j]-t));
	   _max=max(temp,_max);

	}

  	printf("%d\n",sum-_max);

 }


#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int  main()
{
	int n,m;
	int sum=0;
	scanf("%d%d",&n,&m);
	int a[222222];
	int i;
	int maxx=0;
    scanf("%d",&a[0]);
	for(i=1;i<m;i++)
	scanf("%d",&a[i]),sum+=abs(a[i]-a[i-1]);
	maxx=abs(a[1]-a[0]);
	for(i=1;i<m-1;i++)
	maxx=max(maxx,abs(a[i]-a[i-1])+abs(a[i]-a[i+1])-abs(a[i+1]-a[i-1]));
	maxx=max(maxx,abs(a[i-1]-a[i-2]));
	printf("%d\n",sum-maxx);

}


#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int  main()
{
	int n,m;
	int sum=0;
	scanf("%d%d",&n,&m);
	int a[222222];
	int i;
	int maxx=0;
    scanf("%d",&a[0]);
	for(i=1;i<m;i++)
	scanf("%d",&a[i]),sum+=abs(a[i]-a[i-1]);
	maxx=abs(a[1]-a[0]);
	for(i=1;i<m-1;i++)
	maxx=max(maxx,abs(a[i]-a[i-1])+abs(a[i]-a[i+1])-abs(a[i+1]-a[i-1]));
	maxx=max(maxx,abs(a[m-1]-a[m-2]));
	printf("%d\n",sum-maxx);

}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

struct node{
ll l,r;
};
node det;
vector<node>v[100010];
pair<ll,ll>pii;
map<pair<ll,ll>,ll>mpp;

ll arr[100010],store[600010],res,mi=1,res2;
ll func(ll to,ll from)
{
    pii.first=from;
    pii.second=to;
    //det.l=from;
    //det.r=to;
    if(mpp[pii]>0)
       return store[mpp[pii]];
    ll ret=0;
    for(ll i=0;i<v[from].size();i++)
    {
        if(v[from][i].l==-1)
        {
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
        else if(v[from][i].r==-1)
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
        }
        else
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
    }
    mpp[pii]=mi;
   return store[mi++]=(res2-ret);
}

main()
{
    ll i,n,k;
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
       scanf(" %I64d",&arr[i]);
       if(i!=1)
       res+=abs(arr[i]-arr[i-1]);
    }
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
           det.l=-1;
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
        else if(i==k&&i!=1)
        {
           det.l=arr[i-1];
           det.r=-1;
           v[arr[i]].push_back(det);
        }
        else if((i+1)<=k&&(i-1)>=1)
        {
            det.l=arr[i-1];
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
    }
    res2=res;
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
            res=min(res,func(arr[i+1],arr[i]));
        }
        else if(i==k&&i!=1)
        {
            res=min(res,func(arr[i-1],arr[i]));
        }
        else if((i+1)<=k&&(i-1)>=1)
        {
            res=min(res,func(abs(arr[i+1]-arr[i-1])/2LL,arr[i]));
            res=min(res,func(abs(arr[i+1]-arr[i-1])/2LL+1,arr[i]));
            res=min(res,func(arr[i-1],arr[i]));
            res=min(res,func(arr[i+1],arr[i]));
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

struct node{
ll l,r;
};
node det;
vector<node>v[100010];
pair<ll,ll>pii;
map<pair<ll,ll>,ll>mpp;

ll arr[100010],store[100010],res,mi=1,res2;
ll func(ll to,ll from)
{
    pii.first=from;
    pii.second=to;
    //det.l=from;
    //det.r=to;
    if(mpp[pii]>0)
       return store[mpp[pii]];
    ll ret=0;
    for(int i=0;i<v[from].size();i++)
    {
        if(v[from][i].l==-1)
        {
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
        else if(v[from][i].r==-1)
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
        }
        else
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
    }
    mpp[pii]=mi;
   return store[mi++]=res2-ret;
}

main()
{
    ll i,n,k;
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
       scanf(" %I64d",&arr[i]);
       if(i!=1)
       res+=abs(arr[i]-arr[i-1]);
    }
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
           det.l=-1;
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
        else if(i==k&&i!=1)
        {
           det.l=arr[i-1];
           det.r=-1;
           v[arr[i]].push_back(det);
        }
        else
        {
            det.l=arr[i-1];
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
    }
    res2=res;
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
            res=min(res,func(arr[i+1],arr[i]));
        }
        else if(i==k&&i!=1)
        {
            res=min(res,func(arr[i-1],arr[i]));
        }
        else
        {
            res=min(res,func(arr[i+1],arr[i]));
            res=min(res,func(arr[i-1],arr[i]));
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

ll i,k,n,add,arr[100010],j,res;
vector<ll>v[100010];
main()
{
    cin>>n>>k;
    for(i=0;i<k;i++)
    {
        scanf(" %I64d",&arr[i]);
        if(i!=0&&i!=(k-1))
        {
            v[arr[i]].push_back(arr[i-1]);
            v[arr[i-1]].push_back(arr[i]);
        }
        if(i!=0)
            add+=abs(arr[i]-arr[i-1]);
    }
    res=add;
    for(i=1;i<=n;i++)
    {
        vector<ll>vc=v[i];
        if(!vc.empty())
        {
            sort(vc.begin(),vc.end());
            ll mid=vc[vc.size()/2];
            if(mid>n)
                mid=n;
            ll tp=add;
            for (j = 0; j < vc.size(); j++)
            {
             tp+= abs(vc[j] - mid) - abs(vc[j] - i);
            }
            res=min(res,tp);
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

ll i,k,n,add,arr[100010],j,res;
vector<ll>v[100010];
main()
{
    cin>>n>>k;
    for(i=0;i<k;i++)
    {
        scanf(" %d",&arr[i]);
        if(i!=0&&i!=(k-1))
        {
            v[arr[i]].push_back(arr[i-1]);
            v[arr[i-1]].push_back(arr[i]);
        }
        if(i!=0)
            add+=abs(arr[i]-arr[i-1]);
    }
    res=add;
    for(i=1;i<=n;i++)
    {
        vector<ll>vc=v[i];
        if(!vc.empty())
        {
            sort(vc.begin(),vc.end());
            ll mid=vc[vc.size()/2];
            ll tp=add;
            for (j = 0; j < vc.size(); j++)
            {
             tp+= abs(vc[j] - mid) - abs(vc[j] - i);
            }
            res=min(add,tp);
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

int i,k,n,add,arr[100010],res;

main()
{
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
        scanf(" %d",&arr[i]);
        if(i!=1)
            add+=abs(arr[i]-arr[i-1]);
    }
    res=add;
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
            res=min(res,add-abs(arr[i]-arr[i+1]));
        }
        else if(i==k&&i!=1)
        {
            res=min(res,add-abs(arr[i]-arr[i-1]));
        }
        else
        {
            res=min(res,add-abs(arr[i]-arr[i-1])-abs(arr[i]-arr[i+1])+abs(arr[i-1]-arr[i+1]));
            //res=min(res,add-abs(arr[i]-arr[i-1])-abs(arr[i]-arr[i+1]));
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

struct node{
ll l,r;
};
node det;
vector<node>v[100010];
ll arr[100010],res,res2;
ll func(ll to,ll from)
{
    ll ret=0;
    for(int i=0;i<v[from].size();i++)
    {
        if(v[from][i].l==-1)
        {
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
        else if(v[from][i].r==-1)
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
        }
        else
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
    }
   return res2-ret;
}

main()
{
    ll i,n,k;
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
       scanf(" %I64d",&arr[i]);
       if(i!=1)
       res+=abs(arr[i]-arr[i-1]);
    }
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
           det.l=-1;
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
        else if(i==k&&i!=1)
        {
           det.l=arr[i-1];
           det.r=-1;
           v[arr[i]].push_back(det);
        }
        else
        {
            det.l=arr[i-1];
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
    }
    res2=res;
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
            res=min(res,func(arr[i+1],arr[i]));
        }
        else if(i==k&&i!=1)
        {
            res=min(res,func(arr[i-1],arr[i]));
        }
        else
        {
            res=min(res,func(arr[i+1],arr[i]));
            res=min(res,func(arr[i-1],arr[i]));
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

ll i,k,n,add,arr[100010],j,res;
vector<ll>v[100010];
main()
{
    cin>>n>>k;
    for(i=0;i<k;i++)
    {
        scanf(" %I64d",&arr[i]);
        if(i!=0&&arr[i]!=arr[i-1])
        {
            v[arr[i]].push_back(arr[i-1]);
            v[arr[i-1]].push_back(arr[i]);
        }
        if(i!=0)
            add+=abs(arr[i]-arr[i-1]);
    }
    res=add;
    for(i=1;i<=n;i++)
    {
        vector<ll>vc=v[i];
        if(!vc.empty())
        {
            sort(vc.begin(),vc.end());
            ll mid=vc[vc.size()/2];
            if(mid>n)
                mid=n;
            ll tp=add;
            for (j = 0; j < vc.size(); j++)
            {
             tp+= abs(vc[j] - mid) - abs(vc[j] - i);
            }
            res=min(res,tp);
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

struct node{
ll l,r;
};
node det;
vector<node>v[100010];
pair<ll,ll>pii;
map<pair<ll,ll>,ll>mpp;

ll arr[100010],store[100010],res,mi=1,res2;
ll func(ll to,ll from)
{
    pii.first=from;
    pii.second=to;
    //det.l=from;
    //det.r=to;
    if(mpp[pii]>0)
       return store[mpp[pii]];
    ll ret=0;
    for(ll i=0;i<v[from].size();i++)
    {
        if(v[from][i].l==-1)
        {
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
        else if(v[from][i].r==-1)
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
        }
        else
        {
            ret+=abs(from-v[from][i].l)-abs(to-v[from][i].l);
            ret+=abs(from-v[from][i].r)-abs(to-v[from][i].r);
        }
    }
    mpp[pii]=mi;
   return store[mi++]=(res2-ret);
}

main()
{
    ll i,n,k;
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
       scanf(" %I64d",&arr[i]);
       if(i!=1)
       res+=abs(arr[i]-arr[i-1]);
    }
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
           det.l=-1;
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
        else if(i==k&&i!=1)
        {
           det.l=arr[i-1];
           det.r=-1;
           v[arr[i]].push_back(det);
        }
        else if((i+1)<=k&&(i-1)>=1)
        {
            det.l=arr[i-1];
           det.r=arr[i+1];
           v[arr[i]].push_back(det);
        }
    }
    res2=res;
    for(i=1;i<=k;i++)
    {
        if(i==1&&i!=k)
        {
            res=min(res,func(arr[i+1],arr[i]));
        }
        else if(i==k&&i!=1)
        {
            res=min(res,func(arr[i-1],arr[i]));
        }
        else if((i+1)<=k&&(i-1)>=1)
        {
            res=min(res,func(arr[i+1],arr[i]));
            res=min(res,func(arr[i-1],arr[i]));
        }
    }
    cout<<res<<endl;
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define pi (2*acos(0))
#define SF scanf
#define SFd1(a) scanf("%d",&a)
#define SFd2(a,b) scanf("%d%d",&a,&b)
#define SFd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define PF printf
#define inf 99999999
#define eps 0.00000001
#define ll long long
#define ull long long unsigned
#define fr(i,n) for(i=0;i<n;i++)
#define dist(x1,y1,x2,y2) sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
using namespace std;
//int rr[]={1,2,-1,-2,1,2,-1,-2};
//int cc[]={2,1,2,1,-2,-1,-2,-1};

ll i,k,n,add,arr[100010],j,res;
vector<ll>v[100010];
main()
{
    cin>>n>>k;
    for(i=0;i<k;i++)
    {
        scanf(" %d",&arr[i]);
        if(i!=0&&i!=(k-1))
        {
            v[arr[i]].push_back(arr[i-1]);
            v[arr[i-1]].push_back(arr[i]);
        }
        if(i!=0)
            add+=abs(arr[i]-arr[i-1]);
    }
    res=add;
    for(i=1;i<=n;i++)
    {
        vector<ll>vc=v[i];
        if(!vc.empty())
        {
            sort(vc.begin(),vc.end());
            ll mid=vc[vc.size()/2];
            ll tp=add;
            for (j = 0; j < vc.size(); j++)
            {
             tp+= abs(vc[j] - mid) - abs(vc[j] - i);
            }
            res=min(res,tp);
        }
    }
    cout<<res<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

const int MX=100010;

struct node{
    LL l,h;
};

node ran[MX];
LL ln[MX],hn[MX];

bool cmpl(const node &x,const node &y){
    if (x.l==y.l){
        return x.h<y.h;
    }else{
        return x.l<y.l;
    }
}

bool cmph(const node &x,const node &y){
    if (x.h==y.h){
        return x.l>y.l;
    }else{
        return x.h>y.h;
    }
}

int main(){
    int n,m,x,y;
    LL sum=0;
    scanf("%d%d",&n,&m);
    scanf("%d",&x);
    for (int i=0;i<m-1;i++){
        scanf("%d",&y);
        ran[i].l=min(x,y);
        ran[i].h=max(x,y);
        ln[ran[i].l]++;
        hn[ran[i].h]++;
        sum+=ran[i].h-ran[i].l;
        x=y;
    }
    int mid;
    LL ans=0,tmp;
    sort(ran,ran+m-1,cmpl);
    for (int i=0;i<m-1;i+=ln[ran[i].l]){
        if (ln[ran[i].l]>hn[ran[i].l]){
            mid=i-1+((ln[ran[i].l]-hn[ran[i].l]+1)>>1);
            tmp=0;
            for (int j=i;j<mid;j++){
                tmp-=ran[mid].h-ran[j].h;
            }
            tmp+=(i+ln[ran[i].l]-hn[ran[i].l]-mid)*(ran[mid].h-ran[i].l);
            if (tmp>ans){
                ans=tmp;
            }
        }
    }
    sort(ran,ran+m-1,cmph);
    for (int i=0;i<m-1;i+=hn[ran[i].h]){
        if (ln[ran[i].h]<hn[ran[i].h]){
            mid=i-1+((hn[ran[i].h]-ln[ran[i].h]+1)>>1);
            tmp=0;
            for (int j=i;j<mid;j++){
                tmp+=ran[mid].l-ran[j].l;
            }
            tmp+=(i+hn[ran[i].h]-ln[ran[i].h]-mid)*(ran[i].h-ran[mid].l);
            if (tmp>ans){
                ans=tmp;
            }
        }
    }
    printf("%I64d\n",sum-ans);

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

const int MX=100010;

struct node{
    LL l,h;
};

node ran[MX];
LL ln[MX],hn[MX];

bool cmpl(const node &x,const node &y){
    if (x.l==y.l){
        return x.h<y.h;
    }else{
        return x.l<y.l;
    }
}

bool cmph(const node &x,const node &y){
    if (x.h==y.h){
        return x.l>y.l;
    }else{
        return x.h>y.h;
    }
}

int main(){
    int n,m,x,y;
    LL sum=0;
    scanf("%d%d",&n,&m);
    scanf("%d",&x);
    for (int i=0;i<m-1;i++){
        scanf("%d",&y);
        ran[i].l=min(x,y);
        ran[i].h=max(x,y);
        ln[ran[i].l]++;
        hn[ran[i].h]++;
        sum+=ran[i].h-ran[i].l;
        x=y;
    }
    int mid;
    LL ans=0,tmp;
    sort(ran,ran+m-1,cmpl);
    for (int i=0;i<m-1;i+=ln[ran[i].l]){
        if (ln[ran[i].l]>hn[ran[i].l]){
            mid=i-1+((ln[ran[i].l]-hn[ran[i].l]+1)>>1);
            tmp=0;
            for (int j=i;j<mid;j++){
                tmp-=ran[mid].h-ran[j].h;
            }
            tmp+=(i+ln[ran[i].l]-hn[ran[i].l]-mid)*(ran[mid].h-ran[i].l);
            if (tmp>ans){
                ans=tmp;
            }
        }
    }
    sort(ran,ran+m-1,cmph);
    for (int i=0;i<m-1;i+=hn[ran[i].h]){
        if (ln[ran[i].h]<hn[ran[i].h]){
            mid=i-1+((hn[ran[i].h]-ln[ran[i].h]+1)>>1);
            tmp=0;
            for (int j=i;j<mid;j++){
                tmp+=ran[mid].l-ran[j].l;
            }
            tmp+=(i+hn[ran[i].h]-ln[ran[i].h]-mid)*(ran[mid].h-ran[i].l);
            if (tmp>ans){
                ans=tmp;
            }
        }
    }
    printf("%I64d\n",sum-ans);

}