#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)

int n,k;
int card[16];
int check[16];
set<string> ss;

string tos(int a){
	char str[100];
	int i=0;
	while( a>0 ){
		str[i++] = a%10 + '0';
		a /= 10;
	}
	str[i] = 0;
	string s = string(str);
	return string(s.rbegin(), s.rend());
}
void solve(int cnt, string str){
	//printf("%d , %s\n",cnt,str.c_str());
	if( cnt==0 ){
		ss.insert(str);
		return;
	}
	for(int i=0; i<n; i++)if( !check[i] ){
		check[i] = 1;
		solve(cnt-1, str + tos(card[i]));
		check[i] = 0;
	}
	return;
}
int main(){
	while(scanf("%d%d",&n,&k),n|k){
		ss.clear();
		rep(i,n)scanf("%d",&card[i]);
		solve(k,"");
		cout << ss.size() << endl;
	}
	
	return 0;
}
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#pragma warning (disable: 4996)
using namespace std;

int dat[10] = { 0,2,5,5,4,5,6,3,7,6 };

int N, M, A[1 << 18];
int dp[1 << 18];

int main() {
	cin >> N >> M;
	for (int i = 1; i <= M; i++) cin >> A[i];

	for (int i = 0; i <= N; i++) dp[i] = -(1 << 30);
	dp[0] = 0;
	for (int i = 0; i <= N; i++) {
		for (int k = 1; k <= M; k++) dp[i + dat[A[k]]] = max(dp[i + dat[A[k]]], dp[i] + 1);
	}

	int val = N; string S = "";
	while (val > 0) {
		int r = 0;
		for (int i = 1; i <= M; i++) {
			if (val >= dat[A[i]] && dp[val] - 1 == dp[val - dat[A[i]]]) r = max(r, A[i]);
		}
		val -= dat[r]; S += ('0' + r);
	}
	//reverse(S.begin(), S.end());
	cout << S << endl;
	return 0;
}