n, m = map(int, input().split())
A = list(map(int, input().split()))
digit = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [(-1, -1)]*(n+1)
dp[0] = (0, -1)
for k in range(1, n+1):
    max_value = -1
    max_d = -1
    for num in A:
        if k >= digit[num]:
            suf, d = dp[k - digit[num]]
            if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

    dp[k] = (max_value, max_d)
print(dp[n][0]) 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 digit = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} digit {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 dp = [(-1, -1)]*(n+1) dp [(-1, -1)]*(n+1) [(-1, -1)] (-1, -1) -1 -1 n+1 n 1 dp[0] = (0, -1) dp[0] dp 0 (0, -1) 0 -1 for k in range(1, n+1):
    max_value = -1
    max_d = -1
    for num in A:
        if k >= digit[num]:
            suf, d = dp[k - digit[num]]
            if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

    dp[k] = (max_value, max_d) k range(1, n+1) range 1 n+1 n 1 max_value = -1 max_value -1 max_d = -1 max_d -1 for num in A:
        if k >= digit[num]:
            suf, d = dp[k - digit[num]]
            if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

     num A if k >= digit[num]:
            suf, d = dp[k - digit[num]]
            if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

     k >= digit[num] k digit[num] digit num suf, d = dp[k - digit[num]] suf dp[k - digit[num]] dp k - digit[num] k digit[num] digit num d if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

     suf >= 0 suf 0 value = num * (10 ** (d + 1)) + suf value num * (10 ** (d + 1)) + suf num * (10 ** (d + 1)) num 10 ** (d + 1) 10 d + 1 d 1 suf if value > max_value:
                    max_value = value
                    max_d = d + 1

     value > max_value value max_value max_value = value max_value value max_d = d + 1 max_d d + 1 d 1 dp[k] = (max_value, max_d) dp[k] dp k (max_value, max_d) max_value max_d print(dp[n][0]) print dp[n][0] [n] dp n 0 max_value = -1 -1 max_value dp = [(-1, -1)]*(n+1) [(-1, -1)]*(n+1) dp suf, d = dp[k - digit[num]] dp[k - digit[num]] suf A = list(map(int, input().split())) list(map(int, input().split())) A max_d = -1 -1 max_d n, m = map(int, input().split()) map(int, input().split()) n digit = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} digit value = num * (10 ** (d + 1)) + suf num * (10 ** (d + 1)) + suf value max_value = value value max_value d = dp[k - digit[num]] dp[k - digit[num]] d m = map(int, input().split()) map(int, input().split()) m max_d = d + 1 d + 1 max_d
n, m = map(int, input().split())
A = list(map(int, input().split()))
digit = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [(-1, -1)]*(n+1)
dp[0] = (0, -1)
for k in range(1, n+1):
    max_value = -1
    max_d = -1
    for num in A:
        if k >= digit[num]:
            suf, d = dp[k - digit[num]]
            if suf >= 0:
                value = num * (10 ** (d + 1)) + suf
                if value > max_value:
                    max_value = value
                    max_d = d + 1

    dp[k] = (max_value, max_d)
print(dp[n][0])