def p_d():
    n, m = map(int, input().split())
    a = map(int, input().split())
    c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    inf = 10 ** 9
    costs = {m: c[m - 1] for m in a}
    # dp(i) := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
    dp = [0]

    for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

    digits = list(costs.keys())
    digits.sort(reverse=True)
    num = n
    result = []
    for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
    print("".join(result))


if __name__ == '__main__':
    p_d() def p_d():
    n, m = map(int, input().split())
    a = map(int, input().split())
    c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    inf = 10 ** 9
    costs = {m: c[m - 1] for m in a}
    # dp(i) := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
    dp = [0]

    for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

    digits = list(costs.keys())
    digits.sort(reverse=True)
    num = n
    result = []
    for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
    print("".join(result)) p_d n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m a = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split c = [2, 5, 5, 4, 5, 6, 3, 7, 6] c [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 inf = 10 ** 9 inf 10 ** 9 10 9 m: c[m - 1] for m in a for m in a m a for m in a m c[m - 1] c m - 1 m 1 costs = {m: c[m - 1] for m in a} costs {m: c[m - 1] for m in a} dp = [0] dp [0] 0 for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

     i range(1, n + 1) range 1 n + 1 n 1 dp[i - c] if i - c >= 0 else -inf for c in costs.values() for c in costs.values() c costs.values() costs.values costs values for c in costs.values() dp[i - c] if i - c >= 0 else -inf i - c >= 0 i - c i c 0 dp[i - c] dp i - c i c -inf inf dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1) dp.append dp append max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1 max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) max [dp[i - c] if i - c >= 0 else -inf for c in costs.values()] 1 digits = list(costs.keys()) digits list(costs.keys()) list costs.keys() costs.keys costs keys digits.sort(reverse=True) digits.sort digits sort reverse=True reverse True num = n num n result = [] result [] for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
     _ range(dp[n]) range dp[n] dp n for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
     digit digits if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
     num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1 num >= costs[digit] and dp[num - costs[digit]] != -inf num >= costs[digit] num costs[digit] costs digit dp[num - costs[digit]] != -inf dp[num - costs[digit]] dp num - costs[digit] num costs[digit] costs digit -inf inf dp[num - costs[digit]] == dp[num] - 1 dp[num - costs[digit]] dp num - costs[digit] num costs[digit] costs digit dp[num] - 1 dp[num] dp num 1 result.append(str(digit)) result.append result append str(digit) str digit num -= costs[digit] num costs[digit] costs digit break print("".join(result)) print "".join(result) "".join "" join result if __name__ == '__main__':
    p_d() __name__ == '__main__' __name__ '__main__' p_d() p_d def p_d():
    n, m = map(int, input().split())
    a = map(int, input().split())
    c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    inf = 10 ** 9
    costs = {m: c[m - 1] for m in a}
    # dp(i) := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
    dp = [0]

    for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

    digits = list(costs.keys())
    digits.sort(reverse=True)
    num = n
    result = []
    for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
    print("".join(result)) def p_d():
    n, m = map(int, input().split())
    a = map(int, input().split())
    c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    inf = 10 ** 9
    costs = {m: c[m - 1] for m in a}
    # dp(i) := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
    dp = [0]

    for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

    digits = list(costs.keys())
    digits.sort(reverse=True)
    num = n
    result = []
    for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
    print("".join(result)) p_d
def p_d():
    n, m = map(int, input().split())
    a = map(int, input().split())
    c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    inf = 10 ** 9
    costs = {m: c[m - 1] for m in a}
    # dp(i) := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
    dp = [0]

    for i in range(1, n + 1):
        dp.append(max(
            [dp[i - c] if i - c >= 0 else -inf for c in costs.values()]
        ) + 1)

    digits = list(costs.keys())
    digits.sort(reverse=True)
    num = n
    result = []
    for _ in range(dp[n]):
        for digit in digits:
            if num >= costs[digit] and dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
                result.append(str(digit))
                num -= costs[digit]
                break
    print("".join(result))


if __name__ == '__main__':
    p_d()
