program atcoder
implicit none
integer(8) :: n, m, i, j, ans, r, z, dz
integer(8), allocatable :: a(:), b(:), dp(:)

read(*,*) n,m
allocate(a(m),b(m),dp(0:n)) ! i本のマッチをちょうど使って作れる最大の桁数
read(*,*) (a(i), i=1,m)

do i = 1, m
	call num(a(i), b(i))
end do

dp = -10 !初期値

dp(0) = 0

do i = 1, n
	do j = 1, m
    	if(i-b(j) >= 0)then
			call chmax(dp(i),dp(i-b(j))+1)
        end if
    end do
end do

ans = 0
z = n
do j = 1, maxval(dp)
r = 0
	do i = 1, m
		if(dp(z)-1 == dp(z-b(i)))then
    		call chmax(r,a(i))
    	end if
	end do
    ans = ans + r*10**(dp(z)-1)
    call num(r,dz)
    z = z - dz
end do

write(*,'(i0)') ans

contains

subroutine chmax(dp,x)
implicit none
integer(8) :: dp, x

if (dp < x) then
	dp = x
	return
end if
end subroutine

subroutine num(r,s)
implicit none
integer(8) :: r, s
if(r == 1)then
    s = 2
end if
if(r == 2 .or. r == 3 .or. r == 5)then
	s = 5
end if
if(r == 4)then
    s = 4
end if
if(r == 6 .or. r == 9)then
    s = 6
end if
if(r == 7)then
	s = 3
end if
if(r == 8)then
	s = 7
end if
end subroutine

end program atcoder
