fun addMatch(n : Int, i : Int, lm : MutableList<Int>, la : MutableList<Boolean>){
    val remain = n - i
    if(lm[i] < 0) return
    if(la[1] && remain >= 2) lm[i + 2] = lm[i] + 1
    if(la[2] && remain >= 5) lm[i + 5] = lm[i] + 1
    if(la[3] && remain >= 5) lm[i + 5] = lm[i] + 1
    if(la[4] && remain >= 4) lm[i + 4] = lm[i] + 1
    if(la[5] && remain >= 5) lm[i + 5] = lm[i] + 1
    if(la[6] && remain >= 6) lm[i + 6] = lm[i] + 1
    if(la[7] && remain >= 3) lm[i + 3] = lm[i] + 1
    if(la[8] && remain >= 7) lm[i + 7] = lm[i] + 1
    if(la[9] && remain >= 6) lm[i + 6] = lm[i] + 1
}

fun makeNumber(n : Int, lx : MutableList<Int>, lm : MutableList<Int>, la : MutableList<Boolean>){
    var m = n
    for(i in 1..lm[n]){
        if(la[9] && m >= 6 && lm[m] == lm[m - 6] + 1){
            lx.add(9)
            m -= 6
            continue
        }
        if(la[8] && m >= 7 && lm[m] == lm[m - 7] + 1){
            lx.add(8)
            m -= 7
            continue
        }
        if(la[7] && m >= 3 && lm[m] == lm[m - 3] + 1){
            lx.add(7)
            m -= 3
            continue
        }
        if(la[6] && m >= 6 && lm[m] == lm[m - 6] + 1){
            lx.add(6)
            m -= 6
            continue
        }
        if(la[5] && m >= 5 && lm[m] == lm[m - 5] + 1){
            lx.add(5)
            m -= 5
            continue
        }
        if(la[4] && m >= 4 && lm[m] == lm[m - 4] + 1){
            lx.add(4)
            m -= 4
            continue
        }
        if(la[3] && m >= 5 && lm[m] == lm[m - 5] + 1){
            lx.add(3)
            m -= 5
            continue
        }
        if(la[2] && m >= 5 && lm[m] == lm[m - 5] + 1){
            lx.add(2)
            m -= 5
            continue
        }
        // 絶対通るはず
        if(la[1] && m >= 2 && lm[m] == lm[m - 2] + 1){
            lx.add(1)
            m -= 2
            continue
        }
    }
}

fun main(args: Array<String>){
    val (n, m) = readLine()!!.split(" ").map(String::toInt)
    val l = readLine()!!.split(" ").map(String::toInt)
    val la = mutableListOf<Boolean>()
    for(i in 0..9) {
        la.add(false)
    }
    l.forEach{
        la[it] = true
    }
    val lm = mutableListOf<Int>()
    lm.add(0)
    for(i in 1..n){
        lm.add(-1000000000)
    }
    for(i in 0..n){
        addMatch(n, i, lm, la)
    }
    val lx  = mutableListOf<Int>()
    makeNumber(n, lx, lm, la)
    for(i in 1..(lm[n])){
        print(lx[i - 1])
    }
    println()
}