Lunch Menu

Count quadruples of one soup, one main, one dessert and one drink whose prices sum to at most L.

Medium6SortingTwo pointersBinary searchNo attempts yetTime limit3sMemory limit256 MB

Problem

Willy is the youngest zookeeper at the zoo. His pay is modest, so he plans his daily spending carefully, and lunch at the zoo canteen is no exception. From his first day of work he decided that a single lunch must never cost more than a fixed limit LL. His budget is tight, but he still wants a full lunch: a soup, a main dish, a dessert, and a beverage. To keep lunch interesting he also picks a combination that differs from every lunch he has eaten before. Willy wonders after how many days he will be forced to repeat a lunch he has already had.

You are given the price limit LL and the prices of all soups, main dishes, desserts, and beverages in the canteen. Determine how many different lunches cost at most LL. Two lunches are different if they differ in at least one of the four parts.

Input

The input holds several test cases. Each case starts with a line of five integers LL, SS, MM, DD, BB (1L1081 \le L \le 10^8, 1S,M,D,B50001 \le S, M, D, B \le 5000): the lunch price limit, the number of soups, the number of main dishes, the number of desserts, and the number of beverages, in that order. The next four lines hold one price list each. The first line lists the soup prices, the second the main dish prices, the third the dessert prices, and the fourth the beverage prices. Every price is a positive integer not larger than 10810^8. One empty line follows each test case. The last line of the input holds five zeros and is not a test case.

Output

For each test case print, on its own line, the number of different lunches whose total price is at most LL. The answer may exceed the range of a 32-bit integer.