import Control.Monad
import Data.List

main :: IO ()
main = do
    n <- read <$> getLine
    ps <- replicateM n
        ((\[x , y] -> (x, y)) . map read . words <$> getLine)
    let dss = map (\p -> tail $
                         sortBy (\(_, a) (_, b) -> compare a b) $
                         zip ([0..] :: [Int]) $
                         map (cost p) ps) ps
    -- print dss
    print $ f 0 dss

    where
        f :: Int -> [[(Int, Int)]] -> Int
        f _ [] = 0
        f _ [_] = 0
        f i (xs:yss) = case xs of
            [] -> 0
            (j, c):_ -> c + f (i + 1) (mapAt (deleteI i) j yss)


cost :: (Int, Int) -> (Int, Int) -> Int
cost (ax, ay) (bx, by) = min (abs $ ax - bx) (abs $ ay - by)

mapAt :: (a -> a) -> Int -> [a] -> [a]
mapAt _ _ []     = []
mapAt f 0 (x:xs) = f x : xs
mapAt f i (x:xs) = x : mapAt f (i - 1) xs

deleteI :: Eq a => a -> [(a, b)] -> [(a, b)]
deleteI _ [] = []
deleteI a ((a', b):xs) =
    if a == a' then xs
    else (a', b): deleteI a xs
