import Control.Monad
import Data.Char
import Data.Maybe
import qualified Data.Vector as V
import qualified Data.Set as S
main=print.solve=<<(getLine>>getLine)
solve s=length.mapMaybe f$replicateM 3[0..9]where
  ns=V.accum(flip S.insert)(V.replicate 10 S.empty).(`zip`[0..]).map digitToInt$s
  f=foldM(\k i->S.lookupGT k$ns V.! i)(-1)