import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int b = sc.nextInt(); int n = sc.nextInt(); int[] c = new int[n]; int ave = 0; for (int i = 0; i < c.length; i++) { c[i] = sc.nextInt(); ave+=c[i]; } ave /= n; long ans = Long.MAX_VALUE; for(int i = Math.max(0, ave-100000); i< ave+100000;i++){ int d = i; int bb = b; long cnt = 0; for (int j = 0; j < c.length; j++) { cnt += Math.abs(c[j]-d); bb -= d-c[j]; } // System.out.println(i+" "+cnt +" "+ ans); if(bb < 0)continue; ans = Math.min(ans, cnt); } System.out.println(ans); } }