#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair pi; typedef pair pl; typedef pair pls; int main() { ll B; cin >> B; int N; cin >> N; ll C[12]; ll sum = 0; for (int i = 0; i < N; i++) { cin >> C[i]; sum += C[i]; } ll ave = (sum+B) / N,sa = 0; sort(C,C+N); reverse(C, C + N); ll ans = 10000000000; for (int i = 0; i < N; i++) { ll res = 0; ll b = B; ll num = C[i]; for (int j = 0; j < N; j++) { if (C[j] > num) { b += C[j] - num; res += C[j] - num; }else if(C[j] < num){ if (abs(C[j] - num) > b) { res = 1000000000000; break; } b -= num - C[j]; res += num - C[j]; } } ans = min(ans,res); } ll res = 0; for (int i = 0; i < N; i++) { if (C[i] > ave) { B += C[i] - ave; res += C[i] - ave; } else if (C[i] < ave) { if (abs(C[i] - ave) > B) { res = 1000000000000; break; } B -= ave - C[i]; res += ave - C[i]; } } ans = min(ans,res); cout << ans << endl; return 0; }