{"problem":{"name":"Takahashi's Solitaire","description":{"content":"Takahashi has $N$ cards in his hand. For $i = 1, 2, \\ldots, N$, the $i$\\-th card has an non-negative integer $A_i$ written on it. First, Takahashi will freely choose a card from his hand and put it on","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc277_d"},"statements":[{"statement_type":"Markdown","content":"Takahashi has $N$ cards in his hand. For $i = 1, 2, \\ldots, N$, the $i$\\-th card has an non-negative integer $A_i$ written on it.\nFirst, Takahashi will freely choose a card from his hand and put it on a table. Then, he will repeat the following operation as many times as he wants (possibly zero).\n\n*   Let $X$ be the integer written on the last card put on the table. If his hand contains cards with the integer $X$ or the integer $(X+1)\\bmod M$ written on them, freely choose one of those cards and put it on the table. Here, $(X+1)\\bmod M$ denotes the remainder when $(X+1)$ is divided by $M$.\n\nPrint the smallest possible sum of the integers written on the cards that end up remaining in his hand.\n\n## Constraints\n\n*   $1 \\leq N \\leq 2 \\times 10^5$\n*   $2 \\leq M \\leq 10^9$\n*   $0 \\leq A_i \\lt M$\n*   All values in the input are integers.\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$N$ $M$\n$A_1$ $A_2$ $\\ldots$ $A_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc277_d","tags":[],"sample_group":[["9 7\n3 0 2 5 5 3 0 6 3","11\n\nAssume that he first puts the fourth card ($5$ is written) on the table and then performs the following.\n\n*   Put the fifth card ($5$ is written) on the table.\n*   Put the eighth card ($6$ is written) on the table.\n*   Put the second card ($0$ is written) on the table.\n*   Put the seventh card ($0$ is written) on the table.\n\nThen, the first, third, sixth, and ninth cards will end up remaining in his hand, and the sum of the integers on those cards is $3 + 2 + 3 +3 = 11$. This is the minimum possible sum of the integers written on the cards that end up remaining in his hand."],["1 10\n4","0"],["20 20\n18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12","99"]],"created_at":"2026-03-03 11:01:13"}}