// Arup Guha
// 12/11/2025
// Solution to 2025 UCF HS Online D1 Problem B: Cookie Consumption

import java.util.*;

public class cookie {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int k = stdin.nextInt();
		
		// Just care mod k, so read that way.
		int[] vals = new int[n+1];
		for (int i=1; i<=n; i++)
			vals[i] = stdin.nextInt()%k;
			
		// Now cumulative frequency.
		for (int i=2; i<=n; i++)
			vals[i] = (vals[i-1]+vals[i])%k;
		
		int res = vals[1];
		TreeSet<Integer> used = new TreeSet<Integer>();
		used.add(res);
		
		// Go through rest.
		for (int i=2; i<=n; i++) {
		
			Integer low = used.first();
			Integer high = used.higher(vals[i]);
			
			// Best we can do going to a lower mod.
			if (low != null && low < vals[i])
				res = Math.max(res, vals[i]-low);
				
			// Best we can do getting a mod above.
			if (high != null)
				res = Math.max(res,  vals[i]+k-high);
				
			// Now a mod we can build off of.
			used.add(vals[i]);
		}
		
		// Ta da!
		System.out.println(res);
	}
}