#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <string>

#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

int N, M;
int A[10];

std::string out_num(const std::array<int, 10> pattern) {
    std::string str = "";
    for (int i = 9; i >= 1; i--) {
        for (int j = 0; j < pattern[i]; j++) {
            str = str + std::to_string(i);
        }
    }
    return str;
}

std::array<int, 10> add(const std::array<int, 10> pat1, const std::array<int, 10> pat2) {
    std::array<int, 10> ret = {};
    for (int i = 1; i <= 9; i++) {
        ret[i] = pat1[i] + pat2[i];
    }
    return ret;
}

bool is_large(const std::array<int, 10> pat1, const std::array<int, 10> pat2) {
    int keta[2] = {};
    for (int i = 1; i <= 9; i++) {
        keta[0] += pat1[i];
        keta[1] += pat2[i];
    }
    if (keta[0] > keta[1]) { return true; }
    else if (keta[1] > keta[0]) { return false; }

    for (int i = 9; i >= 1; i--) {
        if (pat1[i] > pat2[i]) { return true; }
        else if (pat2[i] > pat1[i]) { return false; }
    }

    return false; // same
}

main(int argc, char **argv) {
    std::cin >> N >> M;
    for (int i = 1; i <= M; i++) {
        std::cin >> A[i];
    }
    bool can_make[10] = {};
    for (int i = 1; i <= M; i++) {
        can_make[A[i]] = true;
    }
    int cost[10] = {};
    cost[1] = 2;
    cost[2] = 5;
    cost[3] = 5;
    cost[4] = 4;
    cost[5] = 5;
    cost[6] = 6;
    cost[7] = 3;
    cost[8] = 7;
    cost[9] = 6;

    int greedy = 0;
    int greedy_cost = 100;

    for (int i = 1; i <= 9; i++) {
        if (can_make[i] && cost[i] <= greedy_cost) {
            greedy_cost = cost[i];
            greedy = i;
        }
    }
    assert(greedy != 0);


    const int Nmod420 = N % 420;

    std::array<int, 10> memo[500];
    for (int i = 0; i < 500; i++) {
        for (int j = 0; j < 10; j++) {
            memo[i][j] = 0;
        }
    }

    memo[0][0] = 1;
    for (int i = 1; i <= 9; i++) {
        memo[0][i] = 0;
    }

    memo[1][0] = 0;

    for (int i = 1; i <= 9; i++) {
        if (can_make[i]) {
            memo[cost[i]][0] = 1;
            for (int j = 1; j <= 9; j++) {
                memo[cost[i]][j] = 0;
            }
            memo[cost[i]][i] = 1;
        }
    }

    for (int i = 4; i <= Nmod420; i++) {
        for (int j1 = 0; j1 <= i; j1++) {
            int j2 = i - j1;
            if (j1 > j2) { break; }

            std::array<int, 10> tmp = {};
            if (memo[j1][0] == 1 && memo[j2][0] == 1) {
                tmp = add(memo[j1], memo[j2]);
                tmp[0] = 1;
            }

            if (tmp[0] == 1) {
                if (memo[i][0] == 0) {
                    memo[i] = tmp;
                } else if (is_large(tmp, memo[i])) {
                    memo[i] = tmp;
                }
            }
        }
    }

    std::array<int, 10> ret = memo[Nmod420];

    ret[greedy] += (420 / cost[greedy]) * int(N / 420);
    
    std::cout << out_num(ret) << std::endl;
}


Measured memory_bytes: 256