#include <bits/stdc++.h>
using namespace std;
template<typename T> void setmax(T& x, T y) {x = max(x, y);}
vector<int> poses[1 << 20];
int main()
{
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
poses[a[i]].push_back(i);
}
vector<long long> dp(n);
long long ans = 0;
//dp[i] = maxj max(dp[j] + j - i, dp[j] + i - j)
for(int cur_el = 2; cur_el <= k; cur_el++)
{
long long mx_dp_minus_j = -1e9;
long long mx_dp_plus_j = -1e9;
for(int pos_prev : poses[cur_el - 1])
{
setmax(mx_dp_minus_j, dp[pos_prev] - pos_prev);
setmax(mx_dp_plus_j, dp[pos_prev] + pos_prev);
}
for(int pos_el : poses[cur_el])
{
dp[pos_el] = max(pos_el + mx_dp_minus_j, mx_dp_plus_j - pos_el);
setmax(ans, dp[pos_el]);
}
}
cout << ans << endl;
return 0;
}