#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 ); sort( C.begin(), C.end() ); ll minv1 = 1LL << 62; int minid1; for( int i = 0; i < N; ++i ){ ll v = ok( B, C, C[ i ] ); if( minv1 > v ) minv1 = v, minid1 = i; } int minid2; if( minid1 - 1 >= 0 ) minid2 = minid1 - 1; if( minid1 + 1 < N ) minid2 = minid1 + 1; if( minid1 - 1 >= 0 and minid1 + 1 < N ){ if( ok( B, C, C[ minid1 - 1 ] ) < ok( B, C, C[ minid1 + 1 ] ) ) minid2 = minid1 - 1; else minid2 = minid1 + 1; } int g = C[ minid1 ]; for( int lb = C[ minid1 ], rb = C[ minid2 ]; lb + 1 <= rb; ){ if( lb + 1 == rb ){ ll vl = ok( B, C, lb ); ll vr = ok( B, C, rb ); if( vl < vr ) g = lb; else g = rb; break; } int x = lb + ( rb - lb + 2 ) / 3; int y = rb - ( rb - lb + 2 ) / 3; ll vl = ok( B, C, lb ); ll vx = ok( B, C, x ); ll vy = ok( B, C, y ); ll vr = ok( B, C, rb ); if( vl <= vx and vx <= vy and vy <= vr ) g = rb = y; else if( vl >= vx and vx <= vy and vy <= vr ) g = rb = y; else if( vl >= vx and vx >= vy and vy <= vr ) g = lb = x; else if( vl >= vx and vx >= vy and vy >= vr ) g = lb = x; } cout << ok( B, C, g ) << endl; return 0; }