A Grouping Problem#1525

A Grouping Problem

1 second
128 megabytes
Hard

You are given a set of integers. You may choose distinct elements from this set to form a group. Two groups are considered different if there exists at least one element that is present in one group but not in the other. For example, if the set contains elements a, b, c, d, and you are asked to choose two elements, then ab, ac, ad, bc, bd, and are all valid and distinct groups.

A grouping system is called complete if, for a given , the number of different groups is maximized. In this example, the set \{ab, ac, ad, bc, bd, cd\} forms a complete grouping system.

For a particular complete grouping system, the fitness is computed as follows:

In the example above, for k = 2, the fitness is

F_2 = (ab + ac + ad + bc + bd + cd) \bmod\ m

If k = 1, the fitness is

F_1 = (a + b + c + d) \bmod\ m

Your task is to determine the complete grouping system that yields the maximum possible fitness.

Input

Each test case starts with two positive integers n~(2 \le n \le 1000) and m~(1 \le m < 2^{31}). The next line contains positive integers, each at most . The last test case contains n = m = 0 and should not be processed.

Output

For each test case, print a single line containing the maximum possible fitness for a grouping system.

Examples

Example 1

Loading
Loading