import           Control.Monad
import qualified Control.Monad.ST            as ST
import qualified Data.Array.IO               as IO
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Maybe
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM

readInt = fst . fromJust . BS.readInt
getInt = readInt <$> BS.getLine

main = do
  n <- getInt
  s <- BS.getLine

  dp <- IO.newArray ((0,0),(n+1,n+1)) 0 :: IO (IO.IOUArray (Int,Int) Int)
  v <- VM.new 1
  VM.write v 0 

  forM_ [n-1,(n-2)..0] $ \i ->
    forM_ [n-1,(n-2)..(i+1)] $ \j ->
      when (BS.index s i == BS.index s j) $ do
        dpij <- IO.readArray dp (i,j)
        dpi1j1 <- IO.readArray dp (i+1, j+1)
        let nv = max dpij (dpi1j1 + 1)
        IO.writeArray dp (i,j) nv
        res <- VM.read v 0
        VM.write v 0 $ max res $ min nv (j-i)

  ans <- VM.read v 0
  print ans