#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
ostream& operator<<(ostream& o,P p){
  return o<<"("<<p.first<<","<<p.second<<")";
}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(auto &t:a)cin>>t;return i;}
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T> void prArr(T a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}
 

signed main(){
  while(1){
    int n,mod;
    cin>>n>>mod;
    if(n == 0 && mod==0) return 0;

    vector<int> A(n);
    cin>>A;

    multiset<int> S;
    vector<int> Mod(n);
    for(int i=0,s=0;i<n;i++){
      s += A[i];
      S.insert(s%mod);
      S.insert(s%mod+mod);
      Mod[i] = s%mod;
    }
    
    int ofset = 0;
    int ans = 0;
    for(int i=0;i<n;i++){
      int s = *(--S.upper_bound(ofset + mod-1));
      Max(ans,s - ofset);
      S.erase(S.find(Mod[i]));
      S.erase(S.find(Mod[i]+mod));
      ofset = (ofset + A[i]) %mod;
    }
    cout<<ans<<endl;
    
  }
   
  return 0;
}


