#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef std::int_fast64_t i64;
typedef std::uint_fast64_t u64;
typedef std::int_fast32_t i32;
typedef std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(std::vector<T>& vector, const typename std::vector<T>::size_type size) {
    vector.reserve(size);
    std::copy_n(std::istream_iterator<T>(std::cin), size, std::back_inserter(vector));
  }

  template <typename T, std::size_t N>
  void read(std::array<T, N>& array) {
    std::copy_n(std::istream_iterator<T>(std::cin), N, array.begin());
  }
}

#endif

// main.cpp

class persistent_numeric {
private:
  class node {
  public:
    std::shared_ptr<node> next;
    int digit;
    node(const std::shared_ptr<node>& next, const char digit) :
      next(next), digit(digit) {
    }
  };

  std::shared_ptr<node> root;
  std::size_t size;

public:
  persistent_numeric() :
    persistent_numeric(std::shared_ptr<node>(), 0) {
  }
  persistent_numeric(const std::shared_ptr<node>& root, const std::size_t size) :
    root(root), size(size) {
  }

  persistent_numeric push_front(const int digit) const {
    return persistent_numeric(std::make_shared<node>(this->root, digit), this->size + 1);
  }

  bool operator<(const persistent_numeric& other) const {
    if (this->size < other.size) return true;
    if (this->size > other.size) return false;
    for (std::shared_ptr<node> this_ptr = this->root, other_ptr = other.root; this_ptr && other_ptr; this_ptr = this_ptr->next, other_ptr = other_ptr->next) {
      if (this_ptr->digit < other_ptr->digit) return true;
      if (this_ptr->digit > other_ptr->digit) return false;
    }
    return false;
  }

  friend std::ostream& operator<<(std::ostream& os, const persistent_numeric& self) {
    if (self.size > 0) {
      for (std::shared_ptr<node> ptr = self.root; ptr; ptr = ptr->next) {
        os << ptr->digit;
      }
    } else {
      os << 0;
    }
    return os;
  }
};

class optional_persistent_numeric {
private:
  bool m_available;
  persistent_numeric m_value;

public:
  optional_persistent_numeric() :
    m_available(false) {
  }
  optional_persistent_numeric(const persistent_numeric& value) :
    m_available(true), m_value(value) {
  }

  bool available() const {
    return this->m_available;
  }
  persistent_numeric& value() {
    return this->m_value;
  }
  const persistent_numeric& value() const {
    return this->m_value;
  }

  bool operator<(const optional_persistent_numeric& other) const {
    if (!this->available() && other.available()) return true;
    return this->available() && other.available() && this->value() < other.value();
  }

  friend std::ostream& operator<<(std::ostream& os, const optional_persistent_numeric& self) {
    if (self.available()) {
      os << self.value();
    } else {
      os << "N/A";
    }
    return os;
  }
};

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> A;
  tools::read(A, M);

  constexpr std::array<int, 10> required_matches = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  std::vector<optional_persistent_numeric> dp(N + 1);
  dp[0] = optional_persistent_numeric(persistent_numeric());
  for (int n = 0; n < N; ++n) {
    if (!dp[n].available()) continue;

    for (const int& A_i : A) {
      const int required_match = required_matches[A_i];
      if (n + required_match <= N) {
        dp[n + required_match] = std::max(dp[n + required_match], optional_persistent_numeric(dp[n].value().push_front(A_i)));
      }
    }
  }

  std::cout << dp[N] << std::endl;
  return 0;
}

Measured memory_bytes: 1152