<pre>import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Lunch {

	static String line;
	static String[] nums;
	private static ArrayList&lt;Integer&gt; SM;
	private static ArrayList&lt;Integer&gt; DB;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		while (true) {
			line = br.readLine();
			nums = line.split(&quot; &quot;);
			int L = Integer.parseInt(nums[0]);
			int S = Integer.parseInt(nums[1]);
			int M = Integer.parseInt(nums[2]);
			int D = Integer.parseInt(nums[3]);
			int B = Integer.parseInt(nums[4]);
			if (L == 0 &amp;&amp; S == 0 &amp;&amp; M == 0 &amp;&amp; D == 0 &amp;&amp; B == 0) {
				break;
			}
			SM = new ArrayList&lt;Integer&gt;();
			DB = new ArrayList&lt;Integer&gt;();
			//////////////////////////////
			List&lt;Integer&gt; first = new ArrayList&lt;Integer&gt;();
			line = br.readLine();
			nums = line.split(&quot; &quot;);
			for (int i = 0; i &lt; S; i++) {
				first.add(Integer.parseInt(nums[i]));
			}

			line = br.readLine();
			nums = line.split(&quot; &quot;);
			for (int i = 0; i &lt; M; i++) {
				int num = Integer.parseInt(nums[i]);
				for (int j = 0; j &lt; first.size(); j++) {
					SM.add(num + first.get(j));
				}
			}
			/////////////////////////////////////
			first = new ArrayList&lt;Integer&gt;();
			line = br.readLine();
			nums = line.split(&quot; &quot;);
			for (int i = 0; i &lt; D; i++) {
				first.add(Integer.parseInt(nums[i]));
			}

			line = br.readLine();
			nums = line.split(&quot; &quot;);
			for (int i = 0; i &lt; B; i++) {
				int num = Integer.parseInt(nums[i]);
				for (int j = 0; j &lt; first.size(); j++) {
					DB.add(num + first.get(j));
				}
			}

			Collections.sort(DB);
			System.out.println(spocitaj(L));
			br.readLine();
		}

	}

	private static long spocitaj(int L) {
		long moznosti = 0;
		for (Integer c1 : SM) {
			if (c1 &gt; L)
				continue;
			int dolnyIx = 0;
			int hornyIx = DB.size() - 1;
			while (hornyIx - dolnyIx &gt; 0) {
				int stred = (hornyIx + dolnyIx) / 2 + 1;
				if (c1 + DB.get(stred) &gt; L) {
					hornyIx = stred - 1;
				} else
					dolnyIx = stred;

			}
			moznosti += hornyIx;
			if (hornyIx == 0 &amp;&amp; (c1 + DB.get(0)) &lt;= L) {
				moznosti++;
			} else if (hornyIx &gt; 0) {
				moznosti++;
			}
		}
		return moznosti;

	}
}
</pre>
