import sys
input = sys.stdin.readline



def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print(dp[n])
        
   
    
if __name__ == "__main__":
    main() import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print(dp[n])
        
   
     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 key = dict() key dict() dict if 1 in a:
        key[2] = 1
     1 in a 1 a key[2] = 1 key[2] key 2 1 for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
     v [5, 3, 2] 5 3 2 if v in a:
            key[5] = v
            break
     v in a v a key[5] = v key[5] key 5 v break if 4 in a:
        key[4] = 4
     4 in a 4 a key[4] = 4 key[4] key 4 4 for v in [9, 6]:
        if v in a:
            key[6] = v
            break
     v [9, 6] 9 6 if v in a:
            key[6] = v
            break
     v in a v a key[6] = v key[6] key 6 v break if 7 in a:
        key[3] = 7
     7 in a 7 a key[3] = 7 key[3] key 3 7 if 8 in a:
        key[7] = 8
 
    
     8 in a 8 a key[7] = 8 key[7] key 7 8 dp = [0]*(n+1) dp [0]*(n+1) [0] 0 n+1 n 1 for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
     i range(n) range n if i != 0 and dp[i] == 0:
            continue
         i != 0 and dp[i] == 0 i != 0 i 0 dp[i] == 0 dp[i] dp i 0 continue for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
     k v key.items() key.items key items if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
     i+k <= n i+k i k n dp[i+k] = max(dp[i+k], 10*dp[i]+v) dp[i+k] dp i+k i k max(dp[i+k], 10*dp[i]+v) max dp[i+k] dp i+k i k 10*dp[i]+v 10*dp[i] 10 dp[i] dp i v print(dp[n]) print dp[n] dp n if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main input = sys.stdin.readline sys.stdin.readline input def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print(dp[n])
        
   
     def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print(dp[n])
        
   
     main
import sys
input = sys.stdin.readline



def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in key.items():
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print(dp[n])
        
   
    
if __name__ == "__main__":
    main()
