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

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

call reverse_sort(m,a)

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

dp = -1 !初期値

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 = ''
z = n
do j = 1, maxval(dp)
	do i = 1, m
		if(dp(z)-1 == dp(z-b(i)) .and. z-b(i) >= 0)then
    		ans(j:j) = achar(48+a(i))
			z = z - b(i)
            exit
    	end if
	end do
end do

deallocate(a,b,dp)
write(*,'(a)') trim(ans)

contains

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

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

subroutine num(r,s)
implicit none
integer :: 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

subroutine reverse_sort(m,a)
    implicit none
    integer, intent(in)    :: m
    integer, intent(inout) :: a(m)
    integer                :: i, j
    do i = m-1, 1, -1
      do j = 1, i
        if (a(j) .lt. a(j+1)) call swap(a(j),a(j+1))
      end do
    end do
    return
end subroutine reverse_sort
 
subroutine swap(a,b)
    implicit none
    integer, intent(inout) :: a, b
    integer                :: c
    c = a
    a = b
	b = c
	return
end subroutine swap

end program atcoder
