N,M=[int(x) for x in input().split()]
X=[0,2,5,5,4,5,6,3,7,6]
#    1,2,3,4,5,6,7,8,9

A=[int(x) for x in input().split()]



ANS=[0]
for i in range(1,N+1):
	ans=0
	for j in A:
		if i==X[j]:
			ans=max(j,ans)
		elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	ANS.append(ans)
print(ANS[-1])
	 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x N,M=[int(x) for x in input().split()] N [int(x) for x in input().split()] M X=[0,2,5,5,4,5,6,3,7,6] X [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x A=[int(x) for x in input().split()] A [int(x) for x in input().split()] ANS=[0] ANS [0] 0 for i in range(1,N+1):
	ans=0
	for j in A:
		if i==X[j]:
			ans=max(j,ans)
		elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	ANS.append(ans) i range(1,N+1) range 1 N+1 N 1 ans=0 ans 0 for j in A:
		if i==X[j]:
			ans=max(j,ans)
		elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	 j A if i==X[j]:
			ans=max(j,ans)
		elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	 i==X[j] i X[j] X j ans=max(j,ans) ans max(j,ans) max j ans elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	 i>=X[j] i X[j] X j temp=ANS[i-X[j]] temp ANS[i-X[j]] ANS i-X[j] i X[j] X j if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	 temp!=0 temp 0 ans=max(ans,10*temp+j) ans max(ans,10*temp+j) max ans 10*temp+j 10*temp 10 temp j ANS.append(ans) ANS.append ANS append ans print(ANS[-1]) print ANS[-1] ANS -1 N,M=[int(x) for x in input().split()] [int(x) for x in input().split()] N ans=0 0 ans A=[int(x) for x in input().split()] [int(x) for x in input().split()] A M=[int(x) for x in input().split()] [int(x) for x in input().split()] M ANS=[0] [0] ANS ans=max(ans,10*temp+j) max(ans,10*temp+j) ans X=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] X ans=max(j,ans) max(j,ans) ans temp=ANS[i-X[j]] ANS[i-X[j]] temp
N,M=[int(x) for x in input().split()]
X=[0,2,5,5,4,5,6,3,7,6]
#    1,2,3,4,5,6,7,8,9

A=[int(x) for x in input().split()]



ANS=[0]
for i in range(1,N+1):
	ans=0
	for j in A:
		if i==X[j]:
			ans=max(j,ans)
		elif i>=X[j]:
			temp=ANS[i-X[j]]
			if temp!=0:

				ans=max(ans,10*temp+j)
		
		#print(ans)
	ANS.append(ans)
print(ANS[-1])
	
