san = 300000
N,K = [int(a) for a in input().split()]
l = [0]*300000
for _ in range(N):
    a = int(input())
    M = 0
    for i in range(-3,4):
        if a+i>=0 and a+i<san: M= max(M,l[a+i-1])
    l[a-1]= M+1
print(max(l))