#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cstdio>
#include<cmath>
#include<deque>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<tuple>
#include<utility>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define REP(i, n)  for(int i=0; i<(int)(n); i++)
#define REPS(i, n)  for(int i=1; i<=(int)(n); i++)
#define PER(i, n) for(int i=(int)(n)-1; i>= 0; i--)
#define PERS(i, n)  for(int i=(int)(n); i>0; i--)
#define FOR(i, c) for(__typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
#define RFOR(i, c)  for(__typeof((c).rbegin()) i=(c).rbegin(); i!=(c).end(); i++)
#define ALL(container)  (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n)  setfill('0') << right << setw(n)
#define mp(a, b)  make_pair(a, b)
#define toLower(c)  c+0x20
#define toUpper(c)  c-0x20
#define pb  push_back
#define eb  emplace_back

const int INF = 1e9;
const long long LLINF = 1e18;
const ull MOD = 998244353;
const int MSIZE = 100007;
const double Pi = 3.14159265358979323146;

int main(){
  int match[10] = {6,2,5,5,4,5,6,3,7,6};
  int n, m; cin >> n >> m;
  vector<int> num(m), dp(n+100, -INF);
  REP(i, m){
    cin >> num[i];
  }
  dp[0]=0;
  sort(RALL(num));
  string ans = "";
  for(int i=1; i<=n; i++){
    REP(j, m){
      if(i-match[num[j]]<0) continue;
      dp[i] = max(dp[i], dp[i-match[num[j]]]+1);
    }
  }
  //cout << n << ":" << ans << endl;

  while(n>0){
    REP(j, m){
      if(n-match[num[j]]<0) continue;
      if(dp[n-match[num[j]]]==dp[n]-1){
        ans += ('0'+num[j]);
        n-=match[num[j]];
        //cout << n << ":" << ans << endl;
        break;
      }
    }
  }
  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
 
struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}


int dp[310][310][610];
signed main(){
    string s;cin>>s;
    int m;cin>>m;
    int n=(int)s.size();
    if(m>=n){
        cout<<n<<endl;
        return 0;
    }
    string t=s;
    reverse(ALL(t));

    rep(i,n)rep(j,n)rep(k,m+1){
        chmax(dp[i+1][j+1][k],dp[i][j][k]+(s[i]==t[j]));
        chmax(dp[i+1][j+1][k],dp[i][j+1][k]);
        chmax(dp[i+1][j+1][k],dp[i+1][j][k]);
        chmax(dp[i+1][j+1][k+1],dp[i][j][k]+1);
    }
    int res=0;
    // t=rev(s)
    // s = [a   ][b     ]
    // t = [rb    ][ra  ]
    // LCS(a,rb)=LCS(b,ra)
    // 文字同士をマッチングさせることを考えるとそれはそう
    rep(i,n+1)chmax(res,2*dp[i][n-i][m]);
    rep(i,n)  chmax(res,2*dp[i][n-1-i][m]+1);
    cout<<res<<endl;
    return 0;
}
