#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef long long ll;
const int MAX_N = 31000;
const int MAX_M = 100010; 
int n,m,arr[MAX_N],mod_sum[MAX_N];
int bit[MAX_M];

int sum(int i) {
  ++i;
  int s = 0;
  while( i > 0 ){
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i,int x){
  ++i;
  while( i <= m ) {
    bit[i] += x;
    i += i & -i;
  }
}

void print_arr(int limit=m){
  rep(i,limit+1) {
    cout << "[" << i << ":" << sum(i) << "] ";
  } puts("");
}

bool DEBUG = 0;

int getMax(int L,int R){
  int target = sum(R);
  /*
  while( L + 1 < R ){
    int M = ( L + R ) / 2;
    if( sum(M) >= target ) R = M;
    else                   L = M;
  }
  */
  rep(_,65){
    int M = ( L + R ) / 2;
    if( sum(M) >= target ) R = M;
    else                   L = M;
  }
  if( L != R ) {
    int res = -1;
    if( sum(R) >= target ) res = R;
    if( sum(L) >= target ) res = L;
    assert( res != -1 );
    R = res;
  }
  if(DEBUG)cout << "result = " << R << " | " << sum(R) << " - " << sum(R-1) << endl;
  if( R == 0 && sum(R) ) return 0;
  else if( R == 0 )      return -1;
  if( sum(R) - sum(R-1) ) return R;
  return -1;
}

inline int convert(int value,int sub){ return ( value - sub + m ) % m; }

void compute(){
  int maxi = 0;
  /*
  rep(i,n) cout << setw(3) << arr[i] << " "; puts("");
  rep(i,n) cout << setw(3) << mod_sum[i] << " "; puts("");
  */
  rep(i,n) if( arr[i] ) add(mod_sum[i],1); //maxi = max(maxi,arr[i]);
  //print_arr(20);

  if( maxi == m-1 ) { printf("%d\n",maxi); return; }
  int sub = 0;
  
  rep(i,n) if( arr[i] ) {
    
    if( DEBUG ) { puts(""); cout << arr[i] << " -th" << endl; }
    if( DEBUG ) { print_arr(10); }
    int low  = sub;
    int high = ( m - 1 + sub ) % m;
    assert( convert(low,sub) == 0 );
    assert( convert(high,sub) == m-1 );
    if( DEBUG ) cout << "sub = " << sub << endl;

    if( DEBUG ) cout << low << "," << high << " [" << convert(low,sub) << "," << convert(high,sub) << "]" << endl;

    assert( low != high );

    if( low < high ) {
      int temp = getMax(low,high);
      if( DEBUG)cout << "temp = " << temp << " " << convert(temp,sub) << endl;
      if( temp != -1 ) maxi = max(maxi,convert(temp,sub));
    } else {
      int temp = getMax(low,m-1);
      if( DEBUG){
        cout << "search[" << low << "," << m-1 << "]" << endl;
        cout << "temp = " << temp << " " << convert(temp,sub) << endl;
      }
      if( temp != -1 ) maxi = max(maxi,convert(temp,sub));
      temp = getMax(0,high);
      if( DEBUG ) {
        cout << "search[" << 0 << "," << high << "]" << endl;
        cout << "temp = " << temp << " " << convert(temp,sub) << endl;
      }
      if( temp != -1 ) maxi = max(maxi,convert(temp,sub));
    }
    if( DEBUG)cout << "maxi = " << maxi << endl;
    ( sub += arr[i] ) %= m;
    add(mod_sum[i],-1);
    if( maxi == m-1 ) break;
  }
  printf("%d\n",maxi);
}

int main(){
  while( scanf("%d %d",&n,&m), n|m ){
    memset(bit,0,sizeof(bit));
    rep(i,n) {
      scanf("%d",arr+i);
      arr[i] %= m;
      if( i ) mod_sum[i] = ( mod_sum[i-1] + arr[i] ) % m;
      else    mod_sum[i] = arr[i];
    }
    compute();
  }
  return 0;
}

Measured memory_bytes: 1832