Codeforces Round 248 (Div. 1)
A
Ryouko's Memory Note
Codeforces
未评级
原题
←B →
翻译中…

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 image.

翻译中…

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.

翻译中…

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.

翻译中…

样例

Input #1
4 6
1 2 3 4 3 2
Output #1
3
InputCopy OutputCopy
Input #2
10 5
9 4 3 8 8
Output #2
6
InputCopy OutputCopy
题目信息
题号
434A
时间限制
1 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。