import Control.Applicative( (<$>))
import Data.Map 
import qualified Data.Map as M
import Data.IntMap
import qualified Data.IntMap as IM
import Data.List
import qualified Data.List as L
import Data.ByteString.Char8
import qualified Data.ByteString.Char8 as C8
import Data.Maybe( fromJust)


main = do
    let
	readBS2Int = fst . fromJust . C8.readInt
	readLine2Ints = Prelude.map readBS2Int . C8.words <$> C8.getLine

    n_ <- readLn::IO Int
    as_ <- readLine2Ints
    let
	intCounts::[Int] -> IntMap Int
	intCounts = L.foldl' (\m k->IM.insertWith (+) k 1 m)
							IM.empty
	counter = IM.toAscList $ intCounts as_

	proc mx [(i,c)]		    = c
	proc mx [(i1,c1),(i2,c2)]
	  | i1==i2-1		    = c1+c2
	  | otherwise		    = max c1 c2
	proc mx [(i1,c1),(i2,c2),(i3,c3)]
	  | i1==i2-1 && i2+1==i3    = max mx (c1+c2+c3)
	  | i1==i2-1		    = max mx (c1+c2)
	  |	i2+1==i3	    = max mx (c2+c3)
	  | otherwise		    = max mx c2
	proc mx ((i1,c1):tl@((i2,c2):(i3,c3):as))
	  | i1==i2-1 && i2+1==i3    = proc (max mx (c1+c2+c3)) tl
	  | i1==i2-1		    = proc (max mx (c1+c2)) tl
	  |	i2+1==i3	    = proc (max mx (c2+c3)) tl
	  | otherwise		    = proc (max mx c2) tl
	proc' [(i1,c1),(i2,c2),(i3,c3)]
	  | i1==i2-1 && i2+1==i3    = c1+c2+c3
	  | i1==i2-1		    = c1+c2
	  |	i2+1==i3	    = c2+c3
	  | otherwise		    = L.maximum [c1,c2,c3]
    
    print $ if 3 == L.length counter
	    then proc' counter
	    else proc 0 counter
