program match_matching
  implicit none
  integer :: n, m, i, j, dp(0:10000) = -1
  integer, dimension(9) :: a = 0, x = 0, p = (/2,5,5,4,5,6,3,7,6/)
  read(*,*) n, m
  read(*,*) a(1:m)
  do i = 1, m
    x(a(i)) = x(a(i))+1
  end do
  m = 0
  do i = 9, 1, -1
    if (x(i) == 0) cycle
    m = m+1
    a(m) = i
  end do
  dp(0) = 0
  do i = 1, n
    do j = 1, m
      if (i < p(a(j))) cycle
      dp(i) = max(dp(i),dp(i-p(a(j))))
    end do
    if (dp(i) >= 0) dp(i) = dp(i)+1
  end do
  i = n
  x = 0
  do while (i > 0)
    do j = 1, m
      if (i-p(a(j)) < 0 .or. dp(i-p(a(j))) /= dp(i)-1) cycle
      i = i-p(a(j))
      x(a(j)) = x(a(j))+1
      exit
    end do
  end do
  do i = 9, 1, -1
    do j = 1, x(i)
      write(*,'(i0)',advance='no') i
    end do
  end do
  write(*,*)
end program match_matching