import Data.Bits
import Data.Bool
import Data.Char
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
main=print.solve.U.unfoldr(B.readInt.B.dropWhile isSpace)=<<B.getContents
solve nabcl=minimum$map f$U.replicateM n[0..2]where
  n=U.head nabcl
  abc=U.slice 1 3 nabcl
  ls=U.drop 4 nabcl
  f=U.sum.U.zipWith(\i b->dp V.! b U.! i)abc.U.accumulate(.|.)(U.replicate 3 0).(`U.zip`w)
  w=U.iterateN n(*2)1
  m=U.maximum abc+1
  dp=V.generate(shift 1 n)$ \b->case b of
    0->U.generate m$bool(maxBound`div`4)0.(==0)
    b->let(p,l)=(dp V.! (b.&.(b-1)),ls U.!countTrailingZeros b) in U.generate m$ \i->
      min(p U.! i)$if i<l then l-i else min(i-l)$p U.! (i-l)+10