import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n])

resolve() import sys sys sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 INF=float("inf") INF float("inf") float "inf" MOD=10**9+7 MOD 10**9+7 10**9 10 9 7 input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n]) resolve W=[0,2,5,5,4,5,6,3,7,6] W [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 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 dp=[-1]*(n+1) dp [-1]*(n+1) [-1] -1 n+1 n 1 dp[0]=0 dp[0] dp 0 0 from itertools import product for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
     i a product(range(n+1),A) product range(n+1) range n+1 n 1 A if i-W[a]<0: continue
         i-W[a]<0 i-W[a] i W[a] W a 0 dp[i]=max(dp[i],dp[i-W[a]]*10+a) dp[i] dp i max(dp[i],dp[i-W[a]]*10+a) max dp[i] dp i dp[i-W[a]]*10+a dp[i-W[a]]*10 dp[i-W[a]] dp i-W[a] i W[a] W a 10 a print(dp[n]) print dp[n] dp n resolve() resolve def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n]) def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n]) resolve INF=float("inf") float("inf") INF MOD=10**9+7 10**9+7 MOD input=sys.stdin.readline sys.stdin.readline input
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n])

resolve()