#include <iostream>
#include <stdlib.h>

using namespace std;

int my_cmp(const void* a, const void* b)
{
	int v1 = *((const int*)a);
	int v2 = *((const int*)b);
	if(v1 < v2) return -1;
	if(v1 > v2) return 1;
	return 0;
}

int bin_search(int array[], int search, int s, int e){
	//cout << search << s << e << array[s] << array[e] << endl;
	if(s == e){
		if (array[s] <= search){
			return s;
		} else {
			return s-1;
		}	
	}

	int m = (s + e)/2;


	if (array[m] > search){
		return bin_search(array, search, s, m);
	} else if (array[m] < search){
		return bin_search(array, search, m + 1, e);
	} else {
		return m;
	}

}

int main()
{
	int max_price, i, j, combs, cur_price = 0, i1, i2, i3, i4;
	int nums[4];
	int *lunches[4];
		int input;

	int f1;
	int f2;
	int f3;
	int cur_m;

	while(1) {
		cin >> max_price >> nums[0] >> nums[1] >> nums[2] >> nums[3];

		if(max_price == 0 && nums[0] == 0)
			break;

		if(cin.eof())
			break;

		lunches[0] = new int[nums[0]];
		lunches[1] = new int[nums[1]];
		lunches[2] = new int[nums[2]];
		lunches[3] = new int[nums[3]];

		combs = 0;
		
		//cout << "kokot" << endl;
		for(j = 0; j < 4; j++) {
			for(i = 0; i < nums[j]; i++){
				//cin >> lunches[j][i];
				input = 0;
				cin >> input;
				if (input <= max_price){
					lunches[j][i] = input;
				}

				//cout << lunches[j][i];
			}
			//cout << endl;
			qsort(lunches[j], nums[j], sizeof(int), my_cmp);
		}
		
		
		for(i4 = 0; i4 < nums[3]; i4++) {
			f1 = lunches[3][i4];
			
			if ( f1 > max_price){
				break;
			}

			for(i3 = 0; i3 < nums[2]; i3++) {

				f2 = f1 + lunches[2][i3];

				if ( f2 > max_price) {
					break;
				}

				for(i2 = 0; i2 < nums[1]; i2++) {

					f3 = f2 + lunches[1][i2];

					if ( f3 >= max_price) {
						break;
					}

					cur_m = bin_search(lunches[0], max_price - f3, 0, nums[0]);

					
					//cout << "cur_m" << cur_m << "searched for:" << max_price-f3 <<endl;
					if ( cur_m >= 0 ){
						combs += cur_m + 1;
					}

					/*if(cur_price > max_price) {
						nums[1] = i2;
						break;
					}*/
				}
				/*if(cur_price > max_price) {
					nums[2] = i3;
					break;
				}*/
			}
			/*if(cur_price > max_price) {
				nums[3] = i4;
				break;
			}*/
		}

		cout << combs << endl;

		delete [] lunches[0];
		delete [] lunches[1];
		delete [] lunches[2];
		delete [] lunches[3];
	}
	return 0;
}