using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("9 7");
WillReturn.Add("3 0 2 5 5 3 0 6 3");
//11
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 10");
WillReturn.Add("4");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("20 20");
WillReturn.Add("18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12");
//99
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long M = wkArr[1];
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
var CntDict = new Dictionary<long, long>();
foreach (long EachA in AArr) {
if (CntDict.ContainsKey(EachA) == false) {
CntDict[EachA] = 0;
}
CntDict[EachA]++;
}
var InsUnionFindSizeInfo = new UnionFindSizeInfo();
foreach (var EachPair in CntDict) {
InsUnionFindSizeInfo.MakeSet(EachPair.Key, EachPair.Key * EachPair.Value);
}
foreach (var EachPair in CntDict) {
long CurrNode = EachPair.Key;
long ConnNode = (CurrNode + 1) % M;
if (CntDict.ContainsKey(ConnNode)) {
InsUnionFindSizeInfo.Unite(CurrNode, ConnNode);
}
}
long BaseSum = AArr.Sum();
var AnswerKouho = new List<long>();
foreach (var EachPair in CntDict) {
AnswerKouho.Add(BaseSum - InsUnionFindSizeInfo.GetScoreSum(EachPair.Key));
}
Console.WriteLine(AnswerKouho.Min());
}
}
// UnionFindSizeInfoクラス
internal class UnionFindSizeInfo
{
private class NodeInfoDef
{
internal long ParentNode;
internal long Rank;
internal long Size;
internal long ScoreSum;
}
private Dictionary<long, NodeInfoDef> mNodeInfoDict =
new Dictionary<long, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(long pNode, long pScoreSum)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
WillAdd.Size = 1;
WillAdd.ScoreSum = pScoreSum;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(long pX, long pY)
{
long XNode = FindSet(pX);
long YNode = FindSet(pY);
// 既に同じ木の場合
if (XNode == YNode) return;
long XRank = mNodeInfoDict[XNode].Rank;
long YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
mNodeInfoDict[XNode].Size += mNodeInfoDict[YNode].Size;
mNodeInfoDict[XNode].ScoreSum += mNodeInfoDict[YNode].ScoreSum;
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
mNodeInfoDict[YNode].Size += mNodeInfoDict[XNode].Size;
mNodeInfoDict[YNode].ScoreSum += mNodeInfoDict[XNode].ScoreSum;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
}
}
// ノードを引数として、木の根を取得
internal long FindSet(long pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<long>();
long CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (long EachPathNode in PathNodeList) {
mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
}
return CurrNode;
}
// ノードを引数として、木のサイズを取得
internal long GetSize(long pNode)
{
long RootNode = FindSet(pNode);
return mNodeInfoDict[RootNode].Size;
}
// ノードを引数として、木のスコアを取得
internal long GetScoreSum(long pNode)
{
long RootNode = FindSet(pNode);
return mNodeInfoDict[RootNode].ScoreSum;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}