import Control.Monad
import Control.Applicative
import Data.List




getBysplit = map read . words <$> getLine

main :: IO()
main = do
  [n] <- getBysplit :: IO [Int]
  als <- getBysplit :: IO [Int]
  print $ solve als

countBySame :: (Ord a) => [a] -> [(a, Int)]
countBySame [] = []
countBySame lst = let
                    x:xs = sort lst
                    ((e, c), lst') = sameElem x xs 0
                    in (e, c):countBySame lst'
    where
      sameElem :: (Ord a) => a -> [a] -> Int -> ((a, Int), [a])
      sameElem x [] count = ((x, count+1), [])
      sameElem x (y:ys) count
                    | x ==  y = sameElem x ys (count+1)
                    | x /= y = ((x, count+1), y:ys)

solve :: [Int] -> Int
solve lst = f 0 (-1, 0) lst'
    where
      lst' = countBySame lst
      f :: Int -> (Int, Int) -> [(Int, Int)] -> Int
      f ans (x, xc) [] =  if xc > ans
                            then xc
                            else ans
      f ans (x, xc) [(y, yc)]
                | x == y-1 = max (xc+yc) ans
                | x /= y-1 = f (max xc ans) (y, yc) []
      f ans (x, xc) ((y, yc):(z, zc):rest) = let
                                            a = if x == y-1
                                                  then xc
                                                  else 0
                                            b = if y == z-1
                                                  then zc
                                                  else 0
                                            in f (max ans (a+yc+b)) (y, yc) ((z, zc):rest)


