#include using namespace std; typedef long long ll; ll ok( int b, const vector< int > &c, int gol ){ ll res = 0; ll x = b; for( int i = 0; i < c.size(); ++i ) if( c[ i ] > gol ) x += c[ i ] - gol, res += c[ i ] - gol; for( int i = 0; i < c.size(); ++i ) if( c[ i ] < gol ) x -= gol - c[ i ], res += gol - c[ i ]; if( x < 0 ) return 1LL << 62; return res; } signed main(){ int B; cin >> B; int N; cin >> N; vector< int > C( N ); for( int i = 0; i < N; ++i ) cin >> C[ i ]; if( N == 1 ) cout << 0 << endl, exit( 0 ); ll csum = 0; for( int i = 0; i < N; ++i ) csum += C[ i ]; int lb = 0, rb = ( csum + B ) / N + 1; while( rb - lb > 3 ){ int x = ( 2LL * lb + rb ) / 3; int y = ( 2LL * rb + lb ) / 3; if( ok( B, C, x ) <= ok( B, C, y ) ) rb = y; else lb = x; } ll ans = 1LL << 62; for( int i = lb; i < rb; ++i ) ans = min( ans, ok( B, C, i ) ); cout << ans << endl; return 0; }