import sys
def input(): return sys.stdin.readline().strip()


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [(0, 0)] * (N + 1) # dp[i] = (i本のマッチで作れる最大の整数, その整数の桁数)
    for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
    print(dp[N][0])

if __name__ == "__main__":
    main() import sys sys def input(): return sys.stdin.readline().strip() input def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [(0, 0)] * (N + 1) # dp[i] = (i本のマッチで作れる最大の整数, その整数の桁数)
    for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
    print(dp[N][0]) main 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 Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] Match [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 dp = [(0, 0)] * (N + 1) dp [(0, 0)] * (N + 1) [(0, 0)] (0, 0) 0 0 N + 1 N 1 for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
     n range(N + 1) range N + 1 N 1 for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
     a A if n - Match[a] < 0: continue
             n - Match[a] < 0 n - Match[a] n Match[a] Match a 0 if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
             n - Match[a] == 0 n - Match[a] n Match[a] Match a 0 dp[n] = max(dp[n], (a, 1)) dp[n] dp n max(dp[n], (a, 1)) max dp[n] dp n (a, 1) a 1 continue number, l = dp[n - Match[a]] number dp[n - Match[a]] dp n - Match[a] n Match[a] Match a l if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
     number != 0 number 0 print(dp[N][0]) print dp[N][0] [N] dp N 0 if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [(0, 0)] * (N + 1) # dp[i] = (i本のマッチで作れる最大の整数, その整数の桁数)
    for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
    print(dp[N][0]) def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [(0, 0)] * (N + 1) # dp[i] = (i本のマッチで作れる最大の整数, その整数の桁数)
    for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
    print(dp[N][0]) main
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [(0, 0)] * (N + 1) # dp[i] = (i本のマッチで作れる最大の整数, その整数の桁数)
    for n in range(N + 1):
        for a in A:
            if n - Match[a] < 0: continue
            if n - Match[a] == 0:
                dp[n] = max(dp[n], (a, 1))
                continue
            number, l = dp[n - Match[a]]
            if number != 0: dp[n] = max(dp[n], (number + a * 10**l, l + 1))
    print(dp[N][0])

if __name__ == "__main__":
    main()
