#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define DEBUG(x) cout << ">" << #x << ": " << x << endl

int prices[4][2000];
int sizes[4];
long long n;
int L;

struct X {
	int avg;
	int * prices;
	int size;
};

X lines[4];

void f(int i, int j, int cash) {
	if ( i > 3 )  {
		if ( cash >= 0 )
			n++;
		return;
	}
	if ( cash < 0 ) {
		return;
	}
	for ( int k = 0 ; k < lines[i].size ; k++ )
	{
		int curcash=cash-lines[i].prices[k];
		if ( curcash < 0 )
			break;
		f(i+1, 0, curcash);

	}
}

int intcmp(const void * a, const void * b) {
	int arg1 = *(const int *)a;
	int arg2 = *(const int *)b;
	if ( arg1 < arg2 ) return -1;
	if ( arg1 > arg2 ) return 1;
	return 0;
}

int xcmp(const void * a, const void * b) {
	const X* arg1 = (const X *)a;
	const X* arg2 = (const X *)b;
	if ( arg1->avg < arg2->avg ) return 1;
	if ( arg1->avg > arg2->avg ) return -1;
	return 0;
}

int main() {
	for ( ;; ) {
		n = 0;
		cin >> L;
		for ( int i = 0 ; i < 4 ; i++ ) 
			cin >> sizes[i];
		if ( !L && !sizes[0] && !sizes[1] && !sizes[2] && !sizes[3] )
			break;
		for ( int i = 0 ; i < 4 ; i++ ) 
		{
			int sum = 0;
			for ( int j = 0 ; j < sizes[i] ; j++ ) 
			{
				cin >> prices[i][j];
				sum += prices[i][j];
			}
			lines[i].avg = sum/sizes[i];
			lines[i].size = sizes[i];
			lines[i].prices = prices[i];
		}
		for ( int i = 0 ; i < 4 ; i++ ) 
			qsort(prices[i], sizes[i], sizeof(int), intcmp);
		qsort(lines, 4, sizeof(X), xcmp);

		f(0,0,L);
		cout << n << endl;
	}

return 0;
}