fun main(args: Array<String>) {
    val t = arrayOf(
        0, 2
        ,
        5
        ,
        5
        ,
        4
        ,
        5
        ,
        6
        ,
        3
        ,
        7
        ,
        6
    )

    val (N, M) = readLine()!!.split(" ").map{it.toInt()}
    val A = readLine()!!.split(" ").map{it.toInt()}

    val dp = Array<String?>(N + 1) { null }
    dp[0] = ""

    for (i in 1..N) {
        for (a in A) {
            if (t[a] > i) continue

            val s = dp[i - t[a]] ?: continue
            val nl = s.length + 1
            val ts = s + a.toString()
            if (dp[i] == null) {
                dp[i] = ts
            } else if (nl > dp[i]!!.length ||
                    nl == dp[i]!!.length && ts > dp[i]!!) {
                dp[i] = ts
            }
        }
    }

    println(dp[N])
}