import resource
import sys
sys.setrecursionlimit(20000)
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
n, m = map(int, input().split())
a = list(map(int, input().split()))

memo = [-1]*10100


def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return - 10 ** 100000
    if not (0 <= i and i < 10100):
        print(-1)
        exit()
    if memo[i] != -1:
        return memo[i]
    if i == 0:
        return 0
    ret = -10**100000
    for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
    memo[i] = ret
    return ret


print(dp(n)) import resource resource import sys sys sys.setrecursionlimit(20000) sys.setrecursionlimit sys setrecursionlimit 20000 f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] f [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m a = list(map(int, input().split())) a list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split memo = [-1]*10100 memo [-1]*10100 [-1] -1 10100 def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return - 10 ** 100000
    if not (0 <= i and i < 10100):
        print(-1)
        exit()
    if memo[i] != -1:
        return memo[i]
    if i == 0:
        return 0
    ret = -10**100000
    for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
    memo[i] = ret
    return ret dp if i < 0:
        return - 10 ** 100000
     i < 0 i 0 return - 10 ** 100000 - 10 ** 100000 - 10 100000 if not (0 <= i and i < 10100):
        print(-1)
        exit()
     not (0 <= i and i < 10100) 0 <= i and i < 10100 0 <= i 0 i i < 10100 i 10100 print(-1) print -1 exit() exit if memo[i] != -1:
        return memo[i]
     memo[i] != -1 memo[i] memo i -1 return memo[i] memo[i] memo i if i == 0:
        return 0
     i == 0 i 0 return 0 0 ret = -10**100000 ret -10**100000 -10 100000 for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
     x a ret = max(ret, 10 * dp(i - f[x]) + x) ret max(ret, 10 * dp(i - f[x]) + x) max ret 10 * dp(i - f[x]) + x 10 * dp(i - f[x]) 10 dp(i - f[x]) dp i - f[x] i f[x] f x x memo[i] = ret memo[i] memo i ret return ret ret i i print(dp(n)) print dp(n) dp n m = map(int, input().split()) map(int, input().split()) m memo = [-1]*10100 [-1]*10100 memo f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] f a = list(map(int, input().split())) list(map(int, input().split())) a n, m = map(int, input().split()) map(int, input().split()) n def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return - 10 ** 100000
    if not (0 <= i and i < 10100):
        print(-1)
        exit()
    if memo[i] != -1:
        return memo[i]
    if i == 0:
        return 0
    ret = -10**100000
    for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
    memo[i] = ret
    return ret def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return - 10 ** 100000
    if not (0 <= i and i < 10100):
        print(-1)
        exit()
    if memo[i] != -1:
        return memo[i]
    if i == 0:
        return 0
    ret = -10**100000
    for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
    memo[i] = ret
    return ret dp
import resource
import sys
sys.setrecursionlimit(20000)
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
n, m = map(int, input().split())
a = list(map(int, input().split()))

memo = [-1]*10100


def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return - 10 ** 100000
    if not (0 <= i and i < 10100):
        print(-1)
        exit()
    if memo[i] != -1:
        return memo[i]
    if i == 0:
        return 0
    ret = -10**100000
    for x in a:
        ret = max(ret, 10 * dp(i - f[x]) + x)
    memo[i] = ret
    return ret


print(dp(n))
