N, M = list(map(int, input().split()))

A = [int(i) for i in input().split()]



# dp[i] = i本使ってできる最大の数



# xを作るために使うマッチの本数

h = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}



dp = [-1] * (N + 1)

dp[0] = 0



for i in range(N + 1):

    for a in A:

        if i - h[a] < 0: continue

        dp[i] = max(10*dp[i-h[a]] + a, dp[i])



print((dp[N]))
N, M = list(map(int, input().split()))

A = [i for i in input().split()]



# dp[i] = i本使ってできる最大の数



def mx(a, b):

    if a is not None and b is None:

        return a

    elif a is None and b is not None:

        return b

    if len(a) != len(b):

        if len(a) > len(b):

            return a

        else:

            return b

    return max(a, b)



# xを作るために使うマッチの本数

h = {'1':2, '2':5, '3':5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6}



dp = [None] * (N + 1)

dp[0] = ''



for i in range(N + 1):

    for a in A:

        if i - h[a] < 0: continue

        if dp[i-h[a]] is None and dp[i] is None:

            continue

        try:

            dp[i] = mx(dp[i-h[a]] + a, dp[i])

        except:

            continue



print((dp[N]))
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
# dp[i] = i本使ってできる最大の数
# xを作るために使うマッチの本数
h = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    for a in A:
        if i - h[a] < 0:
            continue
        dp[i] = max(10 * dp[i - h[a]] + a, dp[i])
print((dp[N]))

N, M = list(map(int, input().split()))
A = [i for i in input().split()]
# dp[i] = i本使ってできる最大の数
def mx(a, b):
    if a is not None and b is None:
        return a
    elif a is None and b is not None:
        return b
    if len(a) != len(b):
        if len(a) > len(b):
            return a
        else:
            return b
    return max(a, b)


# xを作るために使うマッチの本数
h = {"1": 2, "2": 5, "3": 5, "4": 4, "5": 5, "6": 6, "7": 3, "8": 7, "9": 6}
dp = [None] * (N + 1)
dp[0] = ""
for i in range(N + 1):
    for a in A:
        if i - h[a] < 0:
            continue
        if dp[i - h[a]] is None and dp[i] is None:
            continue
        try:
            dp[i] = mx(dp[i - h[a]] + a, dp[i])
        except:
            continue
print((dp[N]))

-A = [int(i) for i in input().split()] +A = [i for i in input().split()] +def mx(a, b): +    if a is not None and b is None: +        return a +    elif a is None and b is not None: +        return b +    if len(a) != len(b): +        if len(a) > len(b): +            return a +        else: +            return b +    return max(a, b) + + -h = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} -dp = [-1] * (N + 1) -dp[0] = 0 +h = {"1": 2, "2": 5, "3": 5, "4": 4, "5": 5, "6": 6, "7": 3, "8": 7, "9": 6} +dp = [None] * (N + 1) +dp[0] = "" -        dp[i] = max(10 * dp[i - h[a]] + a, dp[i]) +        if dp[i - h[a]] is None and dp[i] is None: +            continue +        try: +            dp[i] = mx(dp[i - h[a]] + a, dp[i]) +        except: +            continue