#include <iostream> #include <math.h> #include <stdlib.h> #include <algorithm> using namespace std; bool myfunction(int l,int r)    {        return l>r;    } int main() {     while(1)     {          int n;         cin>>n;         if(n==-1)break;         int sum=0;         int a[n+1];         for(int i=1;i<=n;i++)         {              cin>>a[i];             sum+=a[i];         }         if(sum%n!=0){                 cout<<"-1"<<endl;                 continue;         }         int ave=sum/n;         sort(a,a+n,myfunction);         int res=0;         int round=0;         int current=0;         for(int i=1;i<=n;i++)         {             current=res+a[i]-ave;             for(int j=i+1;j<=n;j++)             {                 if(a[j]<ave)                 {                     if(a[j]+current>=ave){                          res=current-(ave-a[j]);                         round+=ave-a[j];                         a[j]=ave;                     }                     else{                         a[j]+=current;                         round+=current;                         res=0;                     }                 }             }         }         cout<<round<<endl;       }  }