Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.

Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.

Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page a_{i}. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is $\sum_{i = 1}^{m - 1}|a_{i + 1} - a_{i}|$.

Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place.

Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.


-----Input-----

The first line of input contains two integers n and m (1 ≤ n, m ≤ 10^5).

The next line contains m integers separated by spaces: a_1, a_2, ..., a_{m} (1 ≤ a_{i} ≤ n).


-----Output-----

Print a single integer — the minimum number of pages Ryouko needs to turn.


-----Examples-----
Input
4 6
1 2 3 4 3 2

Output
3

Input
10 5
9 4 3 8 8

Output
6



-----Note-----

In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.

In the second sample, optimal solution is achieved by merging page 9 to 4.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Sorting

Implementation
Skill types: Sorting

Starter code:

Solutions:
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = {}
for ai in a:
	b[ai] = []
amt = 0
for i in range(1, len(a)):
	amt += abs(a[i] - a[i - 1])
	if a[i] - a[i - 1]:
		b[a[i]] += [a[i - 1]]
		b[a[i - 1]] += [a[i]]
diff = 0
for bi in b:
	if b[bi]:
		center = sorted(b[bi])[len(b[bi]) // 2]
		sum = 0
		for page in b[bi]:
			sum += abs(page - center) - abs(page - bi)
		diff = min(diff, sum)
print(amt + diff)


(n, m) = map(int, input().split())
(s, p) = (0, [[] for i in range(n + 1)])
t = list(map(int, input().split()))
for i in range(m - 1):
	if t[i] != t[i + 1]:
		p[t[i + 1]].append(t[i])
		p[t[i]].append(t[i + 1])
for (i, q) in enumerate(p):
	if q:
		q.sort()
		k = q[len(q) // 2]
		d = sum((abs(i - j) - abs(k - j) for j in q))
		if d > s:
			s = d
print(sum((abs(t[i + 1] - t[i]) for i in range(m - 1))) - s)


def median(a):
	if len(a) == 0:
		return 0
	if len(a) % 2 == 1:
		return a[len(a) // 2]
	else:
		return (a[len(a) // 2] + a[len(a) // 2 - 1]) // 2

def profit(a, old_val):
	a.sort()
	med = median(a)
	sum_old = 0
	sum_new = 0
	for i in a:
		sum_old += abs(i - old_val)
		sum_new += abs(i - med)
	return sum_old - sum_new
(n, m) = [int(c) for c in input().split()]
pages = [int(c) for c in input().split()]
count = {pages[0]: []}
current_page_switches = 0
for i in range(1, len(pages)):
	cur_i = pages[i]
	prev_i = pages[i - 1]
	if not cur_i in count:
		count[cur_i] = []
	if cur_i != prev_i:
		count[cur_i].append(prev_i)
		count[prev_i].append(cur_i)
		current_page_switches += abs(cur_i - prev_i)
max_profit = 0
for i in count:
	if len(count[i]) > 0:
		tmp = profit(count[i], i)
		if tmp > max_profit:
			max_profit = tmp
print(current_page_switches - max_profit)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
price = sum((abs(a - b) for (a, b) in zip(arr, arr[1:])))
from collections import defaultdict
adj = defaultdict(list)
for (a, b) in zip(arr, arr[1:]):
	if a != b:
		adj[a].append(b)
		adj[b].append(a)

def median(xs):
	return sorted(xs)[len(xs) // 2]
res = 0
for (i, xs) in adj.items():
	m = median(xs)
	a = sum((abs(i - x) for x in xs))
	b = sum((abs(m - x) for x in xs))
	res = max(res, a - b)
print(price - res)


from collections import defaultdict
LIM = 10 ** 5 + 123
(n, m) = list(map(int, input().split()))
occ = [[] for i in range(LIM)]
add = [0 for i in range(LIM)]
a = list(map(int, input().split()))
base_ans = 0
for i in range(1, m):
	if a[i] != a[i - 1]:
		occ[a[i]].append(a[i - 1])
		occ[a[i - 1]].append(a[i])
	t = abs(a[i] - a[i - 1])
	base_ans += t
	add[a[i]] += t
	add[a[i - 1]] += t
ans = base_ans
for i in range(LIM):
	if len(occ[i]) == 0:
		continue
	occ[i].sort()
	k = len(occ[i])
	sum_before = 0
	sum_after = sum(occ[i])
	for (idx, c) in enumerate(occ[i], 1):
		sum_before += c
		sum_after -= c
		cur = idx * c - sum_before
		cur += sum_after - (k - idx) * c
		ans = min(ans, base_ans - add[i] + cur)
print(ans)


import base64, zlib
exec(zlib.decompress(base64.b85decode(b'c${@mOKt-p4Bc~z*PT)6B<(U%dJ)8eYDh&g5Poz2noK66M5-4c+wWsLDg+!6Y$6|<f{$r#B!`+N&%YE^Au(XDptZs2THqOkV&ku)h<AKSBN`(nUmN|aBgG3f;Df0L0S<pc)863i*#E9+mPU`H*P0n@N-mDn!SWgK)V!hZjtaS(x;O_{Y;%-0)4aLlp<W9Q`Y2p@rK~3xDNF2j_%e^y$gMgDvOkqPuW^?A^yIQ6-S^y0^Crh`Bfe1&sLUQkDtsndRo0oAf7|W#&dag7O_hH&R&{3')))