<pre>import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author kuric10
 */
public class Lunch {
    
    private static long l;
    private static long count[] = new long [4];
    private static long meals[][] = new long [4][2001];
   
    
    public static void main(String args[]) throws IOException{
        
        
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        
        String line;
        String poleS[];
        states[0] = new TreeMap&lt;Long,Long&gt;();
        states[1] = new TreeMap&lt;Long,Long&gt;();
        states[2] = new TreeMap&lt;Long,Long&gt;();
        states[3] = new TreeMap&lt;Long,Long&gt;();
        Long result;
        while(!(line = input.readLine()).startsWith(&quot;0 0 0 0 0&quot;)){
            poleS = line.split(&quot; &quot;);
            l = Long.parseLong(poleS[0]);
            count[0] = Long.parseLong(poleS[1]);
            count[1] = Long.parseLong(poleS[2]);
            count[2] = Long.parseLong(poleS[3]);
            count[3] = Long.parseLong(poleS[4]);
            
            result = 0l;
            
            for (int i = 0; i &lt; 4; i++) {
                states[i].clear();
                line = input.readLine();
                poleS = line.split(&quot; &quot;);
                for (int j = 0; j &lt; poleS.length; j++) {
                    meals[i][j]= Long.parseLong(poleS[j]);
                }
            }
            input.readLine();
            
            spracuj();
            
            
            
            for (Map.Entry&lt;Long, Long&gt; entry : states[3].entrySet()) {
                result+=entry.getValue();
            }
            System.out.println(result);
        }
    }
    
    private static TreeMap&lt;Long,Long&gt;[] states = new TreeMap[4];

    private static void spracuj() {
        Long temp;
        //S
        for (int i = 0; i &lt; count[0]; i++) {
            //System.out.println(i);
            temp = states[0].get(meals[0][i]);
            if(temp == null){
                states[0].put(meals[0][i],1l);
            }else{
                states[0].put(meals[0][i], temp+1);
            }

        }
        
        
        
        //
        for (int i = 1; i &lt;= 3; i++) {
            
            
            for (Map.Entry&lt;Long, Long&gt; entry : states[i-1].entrySet()) {
                
                for (int j = 0; j &lt; count[i]; j++) {
                    
                    if(meals[i][j]+entry.getKey()&gt;l)continue;
                    
                    temp = states[i].get(meals[i][j]+entry.getKey());
                    if(temp == null){
                        states[i].put(meals[i][j]+entry.getKey(), entry.getValue());
                    }else{
                        
                        states[i].put(meals[i][j]+entry.getKey(), temp+entry.getValue());
                    }
                    
                }
                
            }
            
        }
        
       /* for (int i = 0; i &lt; 4; i++) {
            for (Map.Entry&lt;Long, Long&gt; entry : states[i].entrySet()) {
                System.out.println(entry.getKey()+&quot; &quot;+entry.getValue()  );
            }
            System.out.println(&quot;&quot;);
        }*/
        
        
        
    }
}
</pre>
