import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7}
    a = list(map(int,input().split()))
    INF = float("inf")
    dp = [-INF for _ in range(N+1)]
    dp[0] = 0
    for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
    dp.reverse()
    use = []
    for i in a:
        use.append((i,p[i]))
        
    use.sort(key=lambda x:(-x[0],-x[1]))
    ans = []
    now = 0
    while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
    print("".join(ans))
    
if __name__ == "__main__":
    main() import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline def main():
    N,M = map(int,input().split())
    p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7}
    a = list(map(int,input().split()))
    INF = float("inf")
    dp = [-INF for _ in range(N+1)]
    dp[0] = 0
    for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
    dp.reverse()
    use = []
    for i in a:
        use.append((i,p[i]))
        
    use.sort(key=lambda x:(-x[0],-x[1]))
    ans = []
    now = 0
    while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
    print("".join(ans))
     main N,M = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split M p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7} p {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7} 1 1 2 7 7 3 4 4 4 5 5 5 3 3 5 2 2 5 9 9 6 6 6 6 8 8 7 a = list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split INF = float("inf") INF float("inf") float "inf" -INF for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) -INF INF dp = [-INF for _ in range(N+1)] dp [-INF for _ in range(N+1)] dp[0] = 0 dp[0] dp 0 0 for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
     i range(N) range N for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
     j a if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
     i+p[j] <= N i+p[j] i p[j] p j N dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1) dp[i+p[j]] dp i+p[j] i p[j] p j max(dp[i+p[j]],dp[i]+1) max dp[i+p[j]] dp i+p[j] i p[j] p j dp[i]+1 dp[i] dp i 1 dp.reverse() dp.reverse dp reverse use = [] use [] for i in a:
        use.append((i,p[i]))
        
     i a use.append((i,p[i])) use.append use append (i,p[i]) i p[i] p i use.sort(key=lambda x:(-x[0],-x[1])) use.sort use sort key=lambda x:(-x[0],-x[1]) key lambda x:(-x[0],-x[1]) (-x[0],-x[1]) -x[0] x[0] x 0 -x[1] x[1] x 1 x ans = [] ans [] now = 0 now 0 while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
     now < N now N for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
     num i use if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
     now+i <= N and dp[now+i] == dp[now]-1 now+i <= N now+i now i N dp[now+i] == dp[now]-1 dp[now+i] dp now+i now i dp[now]-1 dp[now] dp now 1 ans.append(str(num)) ans.append ans append str(num) str num now += i now i break print("".join(ans)) print "".join(ans) "".join "" join ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N,M = map(int,input().split())
    p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7}
    a = list(map(int,input().split()))
    INF = float("inf")
    dp = [-INF for _ in range(N+1)]
    dp[0] = 0
    for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
    dp.reverse()
    use = []
    for i in a:
        use.append((i,p[i]))
        
    use.sort(key=lambda x:(-x[0],-x[1]))
    ans = []
    now = 0
    while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
    print("".join(ans))
     def main():
    N,M = map(int,input().split())
    p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7}
    a = list(map(int,input().split()))
    INF = float("inf")
    dp = [-INF for _ in range(N+1)]
    dp[0] = 0
    for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
    dp.reverse()
    use = []
    for i in a:
        use.append((i,p[i]))
        
    use.sort(key=lambda x:(-x[0],-x[1]))
    ans = []
    now = 0
    while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
    print("".join(ans))
     main input = sys.stdin.buffer.readline sys.stdin.buffer.readline input
import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    p = {1:2,7:3,4:4,5:5,3:5,2:5,9:6,6:6,8:7}
    a = list(map(int,input().split()))
    INF = float("inf")
    dp = [-INF for _ in range(N+1)]
    dp[0] = 0
    for i in range(N):
        for j in a:
            if i+p[j] <= N:
                dp[i+p[j]] = max(dp[i+p[j]],dp[i]+1)
    
    dp.reverse()
    use = []
    for i in a:
        use.append((i,p[i]))
        
    use.sort(key=lambda x:(-x[0],-x[1]))
    ans = []
    now = 0
    while now < N:
        for num,i in use:
            if (now+i <= N and dp[now+i] == dp[now]-1):
                ans.append(str(num))
                now += i
                break
    print("".join(ans))
    
if __name__ == "__main__":
    main()
