#include <bits/stdc++.h>
using namespace std;

int main(){

  int N,M;
    cin>>N>>M;
    vector<bool> A(10),B(8);
    for(int i=0;i<M;i++){
        int a;cin>>a;
        A[a]=true;
    }
    if(A[1]) B[2]=true;
    if(A[7]) B[3]=true;
    if(A[4]) B[4]=true;
    if(A[2]||A[3]||A[5]) B[5]=true;
    if(A[6]||A[9]) B[6]=true;
    if(A[8]) B[7]=true;
    
    if(B[2]){
        if(N%2==0){
            for(int i=0;i<N/2;i++){
                cout<<1;
            }
            cout<<endl;
        }else{
            if(B[3]){
                cout<<7;
                for(int i=0;i<(N-3)/2;i++){
                    cout<<1;
                }
                cout<<endl;
                }
            else if(B[5]){
                if(A[5]){
                    cout<<5;
                    for(int i=0;i<(N-5)/2;i++){
                        cout<<1;
                    }
                    cout<<endl;
                }
                else if(A[3]){
                    cout<<3;
                    for(int i=0;i<(N-5)/2;i++){
                        cout<<1;
                    }
                    cout<<endl;
                }else{
                    cout<<2;
                    for(int i=0;i<(N-5)/2;i++){
                        cout<<1;
                    }
                    cout<<endl;
                }
            }else{
                cout<<8;
                for(int i=0;i<(N-7)/2;i++){
                    cout<<1;
                }
                cout<<endl;
            }
        }
    }
    else if(B[3]){
        if(N%3==0){
            for(int i=0;i<N/3;i++){
                cout<<7;
            }
            cout<<endl;
        }else if(N%3==1){
            if(B[4]){
                for(int i=0;i<(N-4)/3;i++){
                    cout<<7;
                }
                cout<<4<<endl;
            }else if(B[7]){
                cout<<8;
                for(int i=0;i<(N-7)/3;i++){
                    cout<<7;
                }
                cout<<endl;
            }else if(A[5]){
                for(int i=0;i<(N-10)/3;i++){
                    cout<<7;
                }
                cout<<55<<endl;
            }else if(A[3]){
                for(int i=0;i<(N-10)/3;i++){
                    cout<<7;
                }
                cout<<33<<endl;
            }else{
                for(int i=0;i<(N-10)/3;i++){
                    cout<<7;
                }
                cout<<22<<endl;
            }
        }else{
                if(A[5]){
                    for(int i=0;i<(N-5)/3;i++){
                        cout<<7;
                    }
                    cout<<5<<endl;
                }else if(A[3]){
                    for(int i=0;i<(N-5)/3;i++){
                        cout<<7;
                    }
                    cout<<3<<endl;
                }else if(A[2]){
                    for(int i=0;i<(N-5)/3;i++){
                        cout<<7;
                    }
                    cout<<2<<endl;
                }else if(A[4]){
                    for(int i=0;i<(N-8)/3;i++){
                        cout<<7;
                    }
                    cout<<44<<endl;
                }else{
                    cout<<88;
                    for(int i=0;i<(N-14)/3;i++){
                        cout<<7;
                    }
                    cout<<endl;
                }
            }
    }
    else if(B[4]){
        if(N%4==0){
            for(int i=0;i<N/4;i++){
                cout<<4;
            }
            cout<<endl;
        }else if(N%4==1){
            if(A[5]){
                cout<<5;
                for(int i=0;i<(N-5)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }
            else if(A[3]){
                for(int i=0;i<(N-5)/4;i++){
                    cout<<4;
                }
                cout<<3<<endl;
            }else if(A[2]){
                for(int i=0;i<(N-5)/4;i++){
                    cout<<4;
                }
                cout<<2<<endl;
            }else if(A[9]){
                cout<<98;
                for(int i=0;i<(N-13)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }else if(A[6]){
                cout<<86;
                for(int i=0;i<(N-13)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }else{
                cout<<888;
                for(int i=0;i<(N-21)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }
        }else if(N%4==2){
            if(A[9]){
                cout<<9;
                for(int i=0;i<(N-6)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }else if(A[6]){
                cout<<6;
                for(int i=0;i<(N-6)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }else if(A[5]){
                cout<<55;
                for(int i=0;i<(N-10)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }else if(A[3]){
                for(int i=0;i<(N-10)/4;i++){
                    cout<<4;
                }
                cout<<33<<endl;
            }else if(A[2]){
                for(int i=0;i<(N-10)/4;i++){
                    cout<<4;
                }
                cout<<22<<endl;
            }else{
                cout<<88;
                for(int i=0;i<(N-14)/4;i++){
                    cout<<4;
                }
                cout<<endl;
            }
        }else{
            if(A[9]&&B[5]&&N>=11){
                if(A[5]){
                    cout<<95;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<endl;
                }else if(A[3]){
                    cout<<9;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<3<<endl;
                }else{
                    cout<<9;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<2<<endl;
                }
            }else if(A[8]){
                cout<<8;
            for(int i=0;i<(N-7)/4;i++){
                cout<<4;
            }
            cout<<endl;
            }else if(A[6]&&B[5]){
                if(A[5]){
                    cout<<65;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<endl;
                }else if(A[3]){
                    cout<<6;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<3<<endl;
                }else{
                    cout<<6;
                    for(int i=0;i<(N-11)/4;i++){
                        cout<<4;
                    }
                    cout<<2<<endl;
                }
            }else{
                if(A[5]){
                    cout<<555;
                    for(int i=0;i<(N-15)/4;i++){
                        cout<<4;
                    }
                    cout<<endl;
                }else if(A[3]){
                    for(int i=0;i<(N-15)/4;i++){
                        cout<<4;
                    }
                    cout<<333<<endl;
                }else if(A[2]){
                    for(int i=0;i<(N-15)/4;i++){
                        cout<<4;
                    }
                    cout<<222<<endl;
                }
            }
        }
    }
    else if(A[5]){
        if(N%5==0){
            for(int i=0;i<N/5;i++){
                cout<<5;
            }
            cout<<endl;
        }else if(A[9]){
            if(N==6) cout<<9<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<98<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
            for(int i=0;i<N%5;i++){
                cout<<9;
            }
            for(int i=0;i<(N-N%5*6)/5;i++){
                cout<<5;
            }
                cout<<endl;
            }
        }else if(A[6]){
            if(N==6) cout<<6<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<86<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
                if(A[8]){
                    if(N%5==1){
                        cout<<6;
                        for(int i=0;i<(N-6)/5;i++){
                            cout<<5;
                        }
                        cout<<endl;
                    }else if(N%5==2){
                        cout<<8;
                        for(int i=0;i<(N-7)/5;i++){
                            cout<<5;
                        }
                        cout<<endl;
                    }else if(N%5==3){
                        cout<<86;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<5;
                        }
                        cout<<endl;
                    }else{
                        cout<<88;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<5;
                        }
                        cout<<endl;
                    }
                }else{
                    for(int i=0;i<N%5;i++){
                    cout<<6;
                }
                    for(int i=0;i<(N-N%5*6)/5;i++){
                        cout<<5;
                    }
                    cout<<endl;
                }
            }
        }else{
            if(N%5==1){
                cout<<888;
                for(int i=0;i<(N-21)/5;i++){
                    cout<<5;
                }
                cout<<endl;
            }else if(N%5==2){
                cout<<8;
                for(int i=0;i<(N-7)/5;i++){
                    cout<<5;
                }
                cout<<endl;
            }else if(N%5==3){
                cout<<8888;
                for(int i=0;i<(N-28)/5;i++){
                    cout<<5;
                }
                cout<<endl;
            }else if(N%5==4){
                cout<<88;
                for(int i=0;i<(N-14)/5;i++){
                    cout<<5;
                }
                cout<<endl;
            }
        }
    }else if(A[3]){
        if(N%5==0){
            for(int i=0;i<N/5;i++){
                cout<<3;
            }
            cout<<endl;
        }else if(A[9]){
            if(N==6) cout<<9<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<98<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
                for(int i=0;i<N%5;i++){
                    cout<<9;
                }
                for(int i=0;i<(N-N%5*6)/5;i++){
                    cout<<3;
                }
                cout<<endl;
            }
        }else if(A[6]){
            if(N==6) cout<<6<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<86<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
                if(A[8]){
                    if(N%5==1){
                        cout<<6;
                        for(int i=0;i<(N-6)/5;i++){
                            cout<<3;
                        }
                        cout<<endl;
                    }else if(N%5==2){
                        cout<<8;
                        for(int i=0;i<(N-7)/5;i++){
                            cout<<3;
                        }
                        cout<<endl;
                    }else if(N%5==3){
                        cout<<86;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<3;
                        }
                        cout<<endl;
                    }else{
                        cout<<88;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<3;
                        }
                        cout<<endl;
                    }
                }else{
                    for(int i=0;i<N%5;i++){
                        cout<<6;
                    }
                    for(int i=0;i<(N-N%5*6)/5;i++){
                        cout<<3;
                    }
                    cout<<endl;
                }
            }
        }else{
            if(N%5==1){
                cout<<888;
                for(int i=0;i<(N-21)/5;i++){
                    cout<<3;
                }
                cout<<endl;
            }else if(N%5==2){
                cout<<8;
                for(int i=0;i<(N-7)/5;i++){
                    cout<<3;
                }
                cout<<endl;
            }else if(N%5==3){
                cout<<8888;
                for(int i=0;i<(N-28)/5;i++){
                    cout<<3;
                }
                cout<<endl;
            }else if(N%5==4){
                cout<<88;
                for(int i=0;i<(N-14)/5;i++){
                    cout<<3;
                }
                cout<<endl;
            }
        }
    }else if(A[2]){
        if(N%5==0){
            for(int i=0;i<N/5;i++){
                cout<<2;
            }
            cout<<endl;
        }else if(A[9]){
            if(N==6) cout<<9<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<98<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
                for(int i=0;i<N%5;i++){
                    cout<<9;
                }
                for(int i=0;i<(N-N%5*6)/5;i++){
                    cout<<2;
                }
                cout<<endl;
            }
        }else if(A[6]){
            if(N==6) cout<<6<<endl;
            else if(N==7) cout<<8<<endl;
            else if(N==13) cout<<86<<endl;
            else if(N==14) cout<<88<<endl;
            else if(N==19) cout<<885<<endl;
            else{
                if(A[8]){
                    if(N%5==1){
                        cout<<6;
                        for(int i=0;i<(N-6)/5;i++){
                            cout<<2;
                        }
                        cout<<endl;
                    }else if(N%5==2){
                        cout<<8;
                        for(int i=0;i<(N-7)/5;i++){
                            cout<<2;
                        }
                        cout<<endl;
                    }else if(N%5==3){
                        cout<<86;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<2;
                        }
                        cout<<endl;
                    }else{
                        cout<<88;
                        for(int i=0;i<(N-13)/5;i++){
                            cout<<2;
                        }
                        cout<<endl;
                    }
                }else{
                    for(int i=0;i<N%5;i++){
                        cout<<6;
                    }
                    for(int i=0;i<(N-N%5*6)/5;i++){
                        cout<<2;
                    }
                    cout<<endl;
                }
            }
        }else{
            if(N%5==1){
                cout<<888;
                for(int i=0;i<(N-21)/5;i++){
                    cout<<2;
                }
                cout<<endl;
            }else if(N%5==2){
                cout<<8;
                for(int i=0;i<(N-7)/5;i++){
                    cout<<2;
                }
                cout<<endl;
            }else if(N%5==3){
                cout<<8888;
                for(int i=0;i<(N-28)/5;i++){
                    cout<<2;
                }
                cout<<endl;
            }else if(N%5==4){
                cout<<88;
                for(int i=0;i<(N-14)/5;i++){
                    cout<<2;
                }
                cout<<endl;
            }
        }
    }else if(A[9]){
        for(int i=0;i<(N-N%6*7)/6;i++){
            cout<<9;
        }
        for(int i=0;i<N%6;i++){
            cout<<8;
        }
        cout<<endl;
    }else if(A[6]){
        for(int i=0;i<N%6;i++){
            cout<<8;
        }
        for(int i=0;i<(N-N%6*7)/6;i++){
            cout<<6;
        }
        cout<<endl;
    }else{
        for(int i=0;i<N/7;i++){
            cout<<8;
        }
        cout<<endl;
    }
}


Measured memory_bytes: 256