fun main(args: Array<String>) {
    val (N, M) = listOfInt()
    val A = listOfInt().sortedDescending()
    val D = arrayListOf(0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
    val B = (0 until M).map { D[A[it]] }
    val dp = Array(N + 1) { -1 }; dp[0] = 0
    for (m in 0 until M) {
        for (d in B[m] .. N) {
            dp[d] = Math.max(dp[d], dp[d - B[m]] + 1)
        }
    }
    var ans = ""
    var n = N
    while (n > 0) {
        for (a in A) {
            if (n >= D[a] && dp[n - D[a]] == dp[n] - 1) {
                ans += a
                n -= D[a]
                break;
            }
        }
    }
    println(ans)
}
fun next() = readLine()!!
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)