import bisect
from collections import deque

N, K = map(int, input().split())
A = [int(input()) for _ in range(N)]

cnt = [[] for _ in range(max(A) + 1)]

graph = [[] for _ in range(N)]

for i in range(N):
    cnt[A[i]].append(i)

for i in range(N):
    for j in range(max(1, A[i] - 3), min(len(cnt), A[i] + 4)):
        if len(cnt[j]) > 0:
            ind = bisect.bisect_left(cnt[j], i)
            if ind < len(cnt[j]) and cnt[j][ind] > i:
                graph[i].append(cnt[j][ind])

start = 0
res = 0
dist = [-1]*N

for i in range(N):
    if dist[i] >= 0:
        continue

    q = deque([i])
    dist[i] = 1

    while q:
        v = q.pop()
        for n_v in graph[v]:
            dist[n_v] = max(dist[n_v], dist[v] + 1)
            q.append(n_v)

    res = max(res, max(dist))

print(max(dist))