import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

from functools import lru_cache

N,M = II()
A = III()
num = [0]*10
num[1],num[2],num[3],num[4],num[5] = 2,5,5,4,5
num[6],num[7],num[8],num[9] = 6,3,7,6

@lru_cache(maxsize=10**5)
def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1

print(rec(N)) import sys sys import math math from collections import defaultdict sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def input():
    return sys.stdin.readline()[:-1] input return sys.stdin.readline()[:-1] sys.stdin.readline()[:-1] sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline :-1 -1 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 def I(): return int(input()) I def II(): return map(int, input().split()) II def III(): return list(map(int, input().split())) III def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

################# Line tuple(map(int, input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split read_all = [tuple(map(int, input().split())) for _ in range(N)] read_all [tuple(map(int, input().split())) for _ in range(N)] return map(list,zip(*read_all)) map(list,zip(*read_all)) map list zip(*read_all) zip *read_all read_all N N from functools import lru_cache N,M = II() N II() II M A = III() A III() III num = [0]*10 num [0]*10 [0] 0 10 num[1],num[2],num[3],num[4],num[5] = 2,5,5,4,5 num[1] num 1 2 num[2] num 2 5 num[3] num 3 5 num[4] num 4 4 num[5] num 5 5 num[6],num[7],num[8],num[9] = 6,3,7,6 num[6] num 6 6 num[7] num 7 3 num[8] num 8 7 num[9] num 9 6 def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1 rec val = [] val [] for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
     i range(M) range M if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
     n-num[A[i]] < 0 n-num[A[i]] n num[A[i]] num A[i] A i 0 continue elif n-num[A[i]] == 0:
            val.append(A[i])
         n-num[A[i]] == 0 n-num[A[i]] n num[A[i]] num A[i] A i 0 val.append(A[i]) val.append val append A[i] A i if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
     rec(n-num[A[i]])>0 rec(n-num[A[i]]) rec n-num[A[i]] n num[A[i]] num A[i] A i 0 val.append(10*rec(n-num[A[i]])+A[i]) val.append val append 10*rec(n-num[A[i]])+A[i] 10*rec(n-num[A[i]]) 10 rec(n-num[A[i]]) rec n-num[A[i]] n num[A[i]] num A[i] A i A[i] A i if val:
        return max(val)
    else:
        return -1 val return max(val) max(val) max val return -1 -1 n n @lru_cache(maxsize=10**5) rec maxsize=10**5 @lru_cache(maxsize=10**5) lru_cache maxsize=10**5 maxsize 10**5 10 5 rec print(rec(N)) print rec(N) rec N mod = 10**9 + 7 10**9 + 7 mod def II(): return map(int, input().split()) def II(): return map(int, input().split()) II def input():
    return sys.stdin.readline()[:-1] def input():
    return sys.stdin.readline()[:-1] input def III(): return list(map(int, input().split())) def III(): return list(map(int, input().split())) III M = II() II() M def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

################# def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

################# Line def I(): return int(input()) def I(): return int(input()) I num = [0]*10 [0]*10 num def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1 def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1 rec N,M = II() II() N A = III() III() A
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

from functools import lru_cache

N,M = II()
A = III()
num = [0]*10
num[1],num[2],num[3],num[4],num[5] = 2,5,5,4,5
num[6],num[7],num[8],num[9] = 6,3,7,6

@lru_cache(maxsize=10**5)
def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1

print(rec(N))