import           Control.Applicative
import           Data.List

main :: IO ()
main = do
  [_, w] <- map read.words <$> getLine
  if w <= 3
  then do
    ss <- map reverse.transpose.lines <$> getContents
    print $ solve ss
  else undefined

solve :: [String] -> Int
solve ss = f ss
  where
    f :: [String] -> Int
    f [xs] = 0
    f [xs, ys] = g xs ys `min` g ys xs
    f [xs, ys, zs] = minimum
        [ g xs ys + min (g ys zs) (g zs ys)
        , g ys xs + g ys zs
        , g zs ys + min (g xs ys) (g ys xs)
        ]
    g :: String -> String -> Int
    g xs ys = sum [length.filter id$zipWith(==)ws ys|ws<-tails xs]