def main():
    N = int(input())
    a = list(map(int, input().split()))
    num_dict = {}
    a.sort()
    a_max = a[N-1]
    temp = a[0]
    sum = 1

    for i in range(1,N):
        if temp == a[i]:
            sum += 1
        else:
            num_dict[temp] = sum
            sum = 1
            temp = a[i]
    num_dict[a[N-1]] = sum
    
    ans = 0
    judge = 0
    for j in range(1, a_max):
        #j=1のときは特別
        if j == 1 and j in num_dict:
            if j+1 in num_dict:
                judge = num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                judge = num_dict[j]
                ans = judge
                continue

        if (j in num_dict) and (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j] + num_dict[j+1]):
                judge = num_dict[j-1] + num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                continue

        elif (j in num_dict) and (j-1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j]):
                judge = num_dict[j-1] + num_dict[j]
                ans = judug
                cintinue
            else:
                continue

        elif (j in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j]):
                judge = num_dict[j+1] + num_dict[j]
                ans = judug
                cintinue
            else:
                continue

        elif (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j-1]):
                judge = num_dict[j+1] + num_dict[j-1]
                ans = judug
                cintinue
            else:
                continue

        elif j in num_dict:
            if judge <= (num_dict[j]):
                judge = num_dict[j]
                ans = judug
                cintinue
            else:
                continue

        else:
            continue

    if a_max-1 in num_dict:
        if judge <= num_dict[a_max-1] + num_dict[a_max]:
            ans = num_dict[a_max-1] + num_dict[a_max]
    else:
        if judge <= num_dict[a_max]:
            ans = num_dict[a_max]
    return ans

print(main())


### FIXED CODE ###

def main():
    N = int(input())
    a = list(map(int, input().split()))
    num_dict = {}
    a.sort()
    a_max = a[N-1]
    temp = a[0]
    sum = 1

    for i in range(1,N):
        if temp == a[i]:
            sum += 1
        else:
            num_dict[temp] = sum
            sum = 1
            temp = a[i]
    num_dict[a[N-1]] = sum

    ans = 0
    judge = 0
    for j in range(1, a_max):
        #j=1のときは特別
        if j == 1 and j in num_dict:
            if j+1 in num_dict:
                judge = num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                judge = num_dict[j]
                ans = judge
                continue

        if (j in num_dict) and (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j] + num_dict[j+1]):
                judge = num_dict[j-1] + num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                continue

        elif (j in num_dict) and (j-1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j]):
                judge = num_dict[j-1] + num_dict[j]
                ans = judge
                continue
            else:
                continue

        elif (j in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j]):
                judge = num_dict[j+1] + num_dict[j]
                ans = judge
                continue
            else:
                continue

        elif (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j-1]):
                judge = num_dict[j+1] + num_dict[j-1]
                ans = judge
                continue
            else:
                continue

        elif j in num_dict:
            if judge <= (num_dict[j]):
                judge = num_dict[j]
                ans = judge
                continue
            else:
                continue

        else:
            continue

    if a_max-1 in num_dict:
        if judge <= num_dict[a_max-1] + num_dict[a_max]:
            ans = num_dict[a_max-1] + num_dict[a_max]
    else:
        if judge <= num_dict[a_max]:
            ans = num_dict[a_max]
    return ans

print(main())