local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function tbl_clone(tbl)
    return {table.unpack(tbl)}
end

function parse_problem()
    local N = io.read("n")
    local goals = {}
    table.insert(goals, io.read("n"))
    table.insert(goals, io.read("n"))
    table.insert(goals, io.read("n"))
    local ingredients = {}
    for i=1,N do
        local l, _ = io.read("n", "l")
        table.insert(ingredients, l)
    end
    return goals, ingredients
end

function main()
    local goals, ingredients = parse_problem()
    local INF = 999999

    local function find_min()
        local function update(cur_lengths, k, x)
            -- Update cur_lengths.
            -- Ingredients k is used for x.
            local t = tbl_clone(cur_lengths)
            t[x] = t[x] + ingredients[k]
            return t
        end
        local function synthesis_cost(cur_lengths, x)
            -- Ingredients is used for x.
            if cur_lengths[x] == 0 then
                return 0
            else
                return 10
            end
        end
        local function find_min_rec(i, cur_lengths, cur_cost)
            -- i (0 <= i <= #ingredients): Number for decided ingredients.
            -- cur_lengths[k] (1 <= k <= 3): Current length for goal k.
            dbgpr(i, cur_cost)
            if i == #ingredients then
                for k=1,#goals do
                    if cur_lengths[k] == 0 then
                        return INF
                    end
                    cur_cost = cur_cost + math.abs(cur_lengths[k] - goals[k])
                end
                return cur_cost
            end
            local next_i = i + 1
            local visit_costs = {
                find_min_rec(next_i, update(cur_lengths, next_i, 1), cur_cost + synthesis_cost(cur_lengths, 1)),
                find_min_rec(next_i, update(cur_lengths, next_i, 2), cur_cost + synthesis_cost(cur_lengths, 2)),
                find_min_rec(next_i, update(cur_lengths, next_i, 3), cur_cost + synthesis_cost(cur_lengths, 3)),
                find_min_rec(next_i, tbl_clone(cur_lengths), cur_cost)
            }
            table.sort(visit_costs)
            return visit_costs[1]
        end
        return find_min_rec(0, {0, 0, 0}, 0)
    end
    local min_used_mp = find_min()
    print(min_used_mp)
end
main()
