#include #include using namespace std; using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, m; cin >> n >> m; vector a(m); rep(i,m) cin >> a[i]; rep(i,m)--a[i]; const int INF = 1e9; vector> dp(m+2, vector(n, INF)); dp[0][0] = 0; reverse(a.begin(),a.end()); rep(i,m) { rep(j,n) { rep(k,n) { dp[i+1][k] = min(dp[i+1][k], dp[i][j]+abs(k-j)); } } int a1 = dp[i+1][a[i]]; int a2 = dp[i+1][a[i]+1]; dp[i+1][a[i]+1] = min(dp[i+1][a[i]+1], a1); dp[i+1][a[i]] = min(dp[i+1][a[i]], a2); } rep(j,n)rep(k,n){ dp[m+1][j] = min(dp[m+1][j], dp[m][k]+abs(k-j)); } for(int i=1;i