import Data.List

main = do
  n <- readLn :: IO Int
  as <- getLine >>= return . map (\x -> (head x, length x)) . group . sort . zipWith (flip (-)) [1..] . map read . words :: IO [(Int, Int)]

  let
    solve list@(x:xs) = t : solve' (scanl1 (\(x1,y1) (x2,y2) -> (x2, y1+y2)) list) t
      where t =  sum $ map (\y -> abs ((fst y - fst x) * (snd y))) xs
    solve' (_:[]) _ = []
    solve' ((x1,y1):(x2,y2):rest) s = t : (solve' ((x2,y2):rest) t)
      where t = s + (x2-x1) * (y1-(n-y1))

  print $ minimum $ solve as
