import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    a = list(map(int,input().split()))
    d = [-1,2,5,5,4,5,6,3,7,6]
    
    keta = [-1]*(N+10)
    keta[0] = 0
    
    for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

    use = N
    
    ans = [-1]*(N+10)
    ans[0] = 0
    for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
    print(ans[N])
    
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())
    a = list(map(int,input().split()))
    d = [-1,2,5,5,4,5,6,3,7,6]
    
    keta = [-1]*(N+10)
    keta[0] = 0
    
    for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

    use = N
    
    ans = [-1]*(N+10)
    ans[0] = 0
    for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
    print(ans[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 d = [-1,2,5,5,4,5,6,3,7,6] d [-1,2,5,5,4,5,6,3,7,6] -1 2 5 5 4 5 6 3 7 6 keta = [-1]*(N+10) keta [-1]*(N+10) [-1] -1 N+10 N 10 keta[0] = 0 keta[0] keta 0 0 for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

     i range(N) range N for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

     num a if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

     keta[i] != -1 keta[i] keta i -1 keta[i+d[num]] = keta[i]+1 keta[i+d[num]] keta i+d[num] i d[num] d num keta[i]+1 keta[i] keta i 1 use = N use N ans = [-1]*(N+10) ans [-1]*(N+10) [-1] -1 N+10 N 10 ans[0] = 0 ans[0] ans 0 0 for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
     i range(N) range N for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
     num a if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
     keta[i+d[num]] == keta[i]+1 keta[i+d[num]] keta i+d[num] i d[num] d num keta[i]+1 keta[i] keta i 1 ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num) ans[i+d[num]] ans i+d[num] i d[num] d num max(ans[i+d[num]],ans[i]*10+num) max ans[i+d[num]] ans i+d[num] i d[num] d num ans[i]*10+num ans[i]*10 ans[i] ans i 10 num print(ans[N]) print ans[N] ans N if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main input = sys.stdin.buffer.readline sys.stdin.buffer.readline input def main():
    N,M = map(int,input().split())
    a = list(map(int,input().split()))
    d = [-1,2,5,5,4,5,6,3,7,6]
    
    keta = [-1]*(N+10)
    keta[0] = 0
    
    for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

    use = N
    
    ans = [-1]*(N+10)
    ans[0] = 0
    for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
    print(ans[N])
     def main():
    N,M = map(int,input().split())
    a = list(map(int,input().split()))
    d = [-1,2,5,5,4,5,6,3,7,6]
    
    keta = [-1]*(N+10)
    keta[0] = 0
    
    for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

    use = N
    
    ans = [-1]*(N+10)
    ans[0] = 0
    for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
    print(ans[N])
     main
import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    a = list(map(int,input().split()))
    d = [-1,2,5,5,4,5,6,3,7,6]
    
    keta = [-1]*(N+10)
    keta[0] = 0
    
    for i in range(N):
        for num in a:
            if keta[i] != -1:
                keta[i+d[num]] = keta[i]+1

    use = N
    
    ans = [-1]*(N+10)
    ans[0] = 0
    for i in range(N):
        for num in a:
            if keta[i+d[num]] == keta[i]+1:
                ans[i+d[num]] = max(ans[i+d[num]],ans[i]*10+num)
    
    print(ans[N])
    
if __name__ == "__main__":
    main()
