#include <bits/stdc++.h>
using namespace std;
int N, C;
int seq[3010]; // 입력 수열
int countVal[3010]; // 값 개수 카운트
int prefixSum[3010]; // 누적합
int matchLen[3010][3010];
int ans;
void extendPalindrome() {
// matchLen[i][j] 계산: i~j 범위에서 양쪽으로 확장 가능한 길이
for (int i = 1; i <= N; i++) {
for (int j = N; j >= i; j--) {
matchLen[i][j] = 0;
if (seq[i-1] == seq[j+1] && i > 1 && j < N)
matchLen[i][j] = matchLen[i-1][j+1] + 1;
}
}
// 각 위치를 중심으로 확장
for (int center = 1; center < N; center++) {
int left = center;
set<int> S;
fill(countVal, countVal+C+1, 0);
countVal[seq[left]]++;
S.insert(seq[left]);
// 왼쪽 확장
while (left > 1 && seq[left-1] >= seq[left]) {
left--;
countVal[seq[left]]++;
S.insert(seq[left]);
}
// 오른쪽 확장
int smallVal = -1, smallCount = 0;
for (int right = center+1; right <= N; right++) {
if (seq[center] > seq[right]) {
if (smallVal != -1) {
if (smallVal == seq[right]) smallCount++;
else break;
} else {
smallVal = seq[right];
smallCount = 1;
}
} else {
if (!countVal[seq[right]]) S.insert(seq[right]);
countVal[seq[right]]--;
if (!countVal[seq[right]]) S.erase(seq[right]);
}
int last = C;
if (!S.empty()) last = *S.begin();
else ans = max(ans, right - left + 1 + matchLen[left][right]*2);
ans = max(ans,
prefixSum[last-1]*2 +
(prefixSum[last] - prefixSum[last-1] + min(0, -countVal[last]))*2 +
smallCount);
}
}
// 중심 기준으로 홀수/짝수 길이 확장
for (int sum = 2; sum <= N+N; sum++) {
int b = sum/2 + 1, e = sum - sum/2 - 1;
while (b > 1 && e < N && seq[b-1] == seq[e+1]) b--, e++;
int bb = b-1;
fill(countVal, countVal+C+1, 0);
set<int> S;
ans = max(ans, e-b+1);
if (b == 1) continue;
int be = bb;
for (int j = bb; j >= 1; j--) {
if (j != bb && seq[j] < seq[j+1]) break;
be = j;
countVal[seq[j]]++;
S.insert(seq[j]);
}
for (int j = 1; j <= C; j++) {
prefixSum[j] = countVal[j];
prefixSum[j] += prefixSum[j-1];
}
for (int j = e+1; j <= N; j++) {
if (seq[bb] > seq[j]) break;
if (!countVal[seq[j]]) S.insert(seq[j]);
countVal[seq[j]]--;
if (!countVal[seq[j]]) S.erase(seq[j]);
int last = C;
if (!S.empty()) last = *S.begin();
else ans = max(ans, j-be+1 + matchLen[be][j]*2);
ans = max(ans,
prefixSum[last-1]*2 +
(prefixSum[last]-prefixSum[last-1]+min(0, -countVal[last]))*2 +
e-b+1);
}
}
}
int solve() {
ans = 0;
fill(countVal, countVal+C+1, 0);
for (int i = 1; i <= N; i++) {
countVal[seq[i]]++;
ans = max(ans, countVal[seq[i]]);
}
extendPalindrome();
reverse(seq+1, seq+N+1);
for (int i = 1; i <= N; i++) seq[i] = C + 1 - seq[i];
extendPalindrome();
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> C;
for (int i = 1; i <= N; i++) cin >> seq[i];
cout << solve() << "\n";
}