#include <iostream>
#include <algorithm>
using namespace std;
const int M = 11;
int B[5010][5010];
int c[5010],maxNum[100010],maxMod[M];

int main() {
    int n, ans{ };
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> c[i];
    }
    for (int i=0; i<=n; i++) {
        memset(maxNum,0,sizeof(maxNum));
        memset(maxMod,0,sizeof(maxMod));
        for (int j=1; j<i; j++) {
            pre[c[j]] = max(pre[c[j]], B[i][j]);
            mod[c[j]%M] = max(mod[c[j]%M], B[i][j]);
        }
        for (int j=i+1; j<=n; j++) {
            int tmp = 1+max({ pre[a[j]+1], pre[a[j]-1], 
                mod[a[j]%M], B[i][0] });
            B[j][i] = B[i][j] = tmp;
            pre[a[j]] = max(pre[a[j]], tmp);
            mod[a[j]%M] = max(mod[a[j]%M], tmp);
            ans = max(ans, tmp);
        }
    }
    cout << ans;
}
