long long n,m;a[1000],s,t;main(p){for(scanf("%d",&s),t=s;s--;a[s]+=p*4,m=a[s]>m?a[s]:m)scanf("%d%d",a+s,&p);for(;t--&&~n%2;n+=m-a[t]);s=!printf("%lld",n&1?-1:n/2);}