fun main(args: Array<String>) {
    val (N, M) = readLine()!!.split(" ").map{it.toInt()}
    val A = readLine()!!.split(" ").map{it.toInt()}
    val num = arrayOf(
        0,
        2
        ,
        5
        ,
        5
        ,
        4
        ,
        5
        ,
        6
        ,
        3
        ,
        7
        ,
        6
    )

    val dp = arrayOfNulls<String>(N + 1)
    dp[0] = ""

    for (i in 1..N) {
        for (j in A) {
            if (num[j] > i) continue
            val m = i - num[j]
            if (dp[m] == null) continue
            if (dp[i] == null) {
                dp[i] = j.toString() + dp[m]
            } else {
                val t = j.toString() + dp[m]
                if (t.length != dp[i]!!.length) {
                    if (t.length > dp[i]!!.length) {
                        dp[i] = t
                    }
                } else {
                    if (t > dp[i]!!) {
                        dp[i] = t
                    }
                }
            }
        }
    }

    println(dp[N])

//    val H = ArrayList<Pair<Int, List<Int>>>()
//    for (s in 1 until (1 shl 9)) {
//        val l = (1..9).filter { (s shr (it - 1)) and s == 1 }
//
//        if (!A.containsAll(l)) {
//            continue
//        }
//
//        for (i in )
//    }
}
