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 gentbl(n, f)
    local t = {}
    for i=1,n do
        t[i] = f(i)
    end
    return t
end
function parse_table(N)
    local tbl = {}
    for i=1,N do
        table.insert(tbl, io.read("n"))
    end
    return tbl
end
function parse_table_callback(N, f)
    for i=1,N do
        f(i, io.read("n"))
    end
end
function main()
    local cost = {2,5,5,4,5,6,3,7,6}
    local N, M = io.read("n", "n")
    io.read("l")
    local allowed = gentbl(9, function(i)
        return false
    end)
    parse_table_callback(M, function(i, v)
        assert(v >= 1 and v <= 9)
        allowed[v] = true
    end)
    --dbgpr_t(allowed)
    local function generate_max_ndigits(resource)
        local tbl = {}
        tbl[0] = 0
        for n=1,resource do
            local max_ndigit = -1
            --local i_at_max_ndigit = nil
            for i=9,1,-1 do
                -- try "i" as a digit
                local remain_n = n - cost[i]
                if allowed[i] and
                    cost[i] <= n and
                    tbl[remain_n] >= 0 then
                    local new_ndigit = tbl[remain_n] + 1
                    --dbgpr(n,i,cost[i],tbl[remain_n])
                    if max_ndigit < new_ndigit then
                        --i_at_max_ndigit = i
                        max_ndigit = new_ndigit
                    end
                end
            end
            tbl[n] = max_ndigit
        end
        return tbl
    end
    local max_ndigits = generate_max_ndigits(N)
    --dbgpr_t(max_ndigits)
    dbgpr("max_ndigits[N]", max_ndigits[N])
    local n = N
    local ans = {}
    for i=1,max_ndigits[N] do
        local selected = false
        for i=9,1,-1 do
            -- try "i" as a digit
            local remain_n = n - cost[i]
            if allowed[i] and max_ndigits[remain_n] == max_ndigits[n] - 1 then
                table.insert(ans, i)
                n = remain_n
                selected = true
                break
            end
        end
        assert(selected)
    end
    dbgpr("ans")
    print(table.concat(ans,""))
end
main()
