import Data.List

f3 ::Integer->Integer
f3 0=0
f3 _=10

f::Integer->Integer->Integer->Integer->Integer->Integer->Integer->Integer
f a b c a1 b1 c1 mp
    |a>0 && b>0 && c>0=let (a2:b2:c2:[])=sort (a:b:c:[])
                           (a3:b3:c3:[])=sort (a1:b1:c1:[])
                       in abs(a2-a3)+abs(b2-b3)+abs(c2-c3)+mp
    |otherwise=10^20

f2::[Integer]->Integer->Integer->Integer->Integer->Integer->Integer->Integer->Integer
f2 [] a b c a2 b2 c2 mp=f a b c a2 b2 c2 mp
f2 (x:xs) a b c a2 b2 c2 mp=minimum [f2 xs (a+x) b c a2 b2 c2 (mp+(f3 a)),f2 xs a (b+x) c a2 b2 c2 (mp+(f3 b)),
                                     f2 xs a b (c+x) a2 b2 c2 (mp+(f3 c)),f2 xs a b c a2 b2 c2 mp]

main = do
       l1<-getLine
       ls<-getContents
       let xs=map read (lines ls)::[Integer]
           (_:a2:b2:c2:[])=map read (words l1)::[Integer]
       putStrLn $ show $ f2 xs 0 0 0 a2 b2 c2 0