#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>
using namespace std;
typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);
ll n,p;
ll a[maxn];
ll bin_search(int left,int right,ll key)
{
while(left<=right){
int mid=(left+right)/2;
//cout<<left<<" "<<right<<" "<<mid<<endl;
if(a[mid]>key) right=mid-1;
else if((mid==n&&a[mid]<=key)||(a[mid+1]>key&&a[mid]<=key)) return a[mid];
else left=mid+1;
}
return -INF;
}
int main()
{
while(cin>>n>>p){
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
a[i]%=p;
}
sort(a+1,a+n+1);
ll ans=0;
for(int i=1;i<=n-1;i++){
ll x=bin_search(i+1,n,p-a[i]);
ll y=bin_search(i+1,n,2*p-a[i]);
//cout<<"i+1="<<i+1<<" p-a[i]="<<p-a[i]<<endl;
//cout<<"x="<<x<<" y="<<y<<endl;
ll t=max((x+a[i])%p,(y+a[i])%p);
if(t>ans) ans=t;
}
cout<<ans<<endl;
}
return 0;
}