#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<stack>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

vector<int> card[10];
map<stack<int> ,int> check;
int visit[10];

stack<int> ub;
int cont;

void saiki(int dep,int k,int n){
	if(dep==k){
		if(check[ub]==0){
			check[ub]=1;
			cont++;
		}
		return;
	}
	
	rep(i,n){
		if(visit[i]==0){
			visit[i]=1;
			rep(j,card[i].size()){
				ub.push(card[i][j]);
			}
			
			saiki(dep+1,k,n);
			
			rep(j,card[i].size()){
				ub.pop();
			}
			visit[i]=0;
		}
	}
}

int main(){
	while(1){
		int n;
		int k;
		
		scanf("%d%d",&n,&k);
		if(n==0)break;
		
		rep(i,10){
			card[i].clear();
			visit[i]=0;
		}
		
		check.clear();
		cont=0;
		
		rep(i,n){
			int a;
			scanf("%d",&a);
			
			if(a<10){
				card[i].push_back(a);
			}else{
				card[i].push_back(a/10);
				card[i].push_back(a%10);
			}
		}
		
		saiki(0,k,n);
		
		printf("%d\n",cont);
	}
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) cin >> a[i];
  sort(all(a), greater<int>());
  vi v = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vi dp(n + 10, -1);
  dp[0] = 0;
  rep(i, n) rep(j, m) {
    if(dp[i] == -1) continue;
    dp[i + v[a[j]]] = max(dp[i + v[a[j]]], dp[i] + 1);
  }
  string ans;
  int i = n;
  while(i) {
    for(int j : a) {
      if(i - v[j] >= 0 && dp[i - v[j]] == dp[i] - 1) {
        i -= v[j];
        ans += char('0' + j);
        break;
      }
    }
  }
  cout << ans << endl;
}