n, M, ans = int(input()), 11, 0
c = [0] + [int(i) for i in input().split()]
B = [[0]*(n + 1) for i in range(n + 1)]
maxNum = [0] * (10**5 + 2)
maxMod = [0] * M
for y in range(n + 1):
    maxMod, By = [0] * M, B[y]
    for i in c:
        maxNum[i] = 0
    for i in range(y):
        w, wm = c[i], c[i]%M
        maxMod[wm] = max(maxMod[wm], By[i])
        maxNum[w] = max(maxNum[w], By[i])
    for i in range(y + 1, n + 1):
        w, wm = c[i], c[i]%M
        tmp = 1 + max(maxMod[wm], maxNum[w+1],
            maxNum[w-1], By[0])
        B[y][x] = B[x][y] = tmp
        maxMod[wm] = max(maxMod[wm], tmp)
        maxNum[w] = max(maxNum[w], tmp)
        ans = max(ans, w)
print(ans)