#include <stdio.h>
#include <set>
using namespace std;
int main(){
    int n,m,now,ans;
    set<int> s;
    unsigned int temp;
    while(true){
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)return 0;
        s.clear();
        s.insert(m);
        now=ans=0;
        for(int i=0;i<n;i++){
            scanf("%u",&temp);
            now=(now+static_cast<int>(temp%m))%m;
            ans=max(ans,(m+now-*s.upper_bound(now))%m);
            s.insert(now);
        }
        printf("%d\n",ans);
    }
}
