import java.util.ArrayList
fun main(args: Array<String>) {
    val need = intArrayOf(0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
    var (n,m) = readLine()!!.split(" ").map { s -> s.toInt() }
    val a = readLine()!!.split(" ").map { s -> s.toInt() }.sortedBy { need[it]*10 - it }
    var prev =-1
    a.filter { (prev!=need[it]).apply { prev = need[it] } }

    class Data(var maxDig:Int=-1,var num:Int=0)

    val dp = DP<Data>(n+1){ index,dp ->
        if(index<0)Data()
        else{
            val amax = a.filter { index-need[it]>=0 }
                .maxBy { dp[index-need[it]]!!.maxDig*10 +  it }
            if(amax==null||dp[index-need[amax]]!!.maxDig==-1)Data()
            else Data(dp[index-need[amax]]!!.maxDig+1,amax)
        }
    }
    dp[0] = Data(0,0)
    while(n>0) {
        print(dp[n]!!.num)
        n -= need[dp[n]!!.num]
    }
}

class DP<T>(size:Int,private val recurrenceRelation: (Int,ArrayList<T?>) -> T ):ArrayList<T?>(size){
    init {
        for (i in 0 until size)add(null)
    }

    override fun get(index: Int): T? {
        return super.get(index)?:run{
             recurrenceRelation(index,this).apply { set(index,this) }
        }
    }
}
