import qualified Data.ByteString.Char8 as C
import qualified Data.IntSet as S
import Data.Ord
import Prelude hiding (head, tail, null, filter, zipWith, minimum, (++))
import Data.Vector.Unboxed
import Data.Vector.Algorithms.Intro

main = C.interact $ put . sol . get

get = unfoldr (C.readInt . C.dropWhile (<'0'))

put = C.pack . show

sol v = prim (ex ++ ey)
  where
  n = head v
  u = tail v
  vs = generate n (\i -> 2*i)
  ex = es x $ modify (sortBy (comparing x)) vs
  ey = es y $ modify (sortBy (comparing y)) vs
  x i = u!i
  y i = u!(i+1)
  es f is = zipWith (\i j -> (f i-f j,i,j)) (tail is) is

prim es = f (S.singleton i0) 0
  where
  (_,i0,_) = head es
  f s a = if null r then a else f (S.insert i (S.insert j s)) (c+a)
    where
    r = filter (\(_,i,j) -> S.member i s /= S.member j s) es
    (c,i,j) = minimum r