#include using namespace std; #define int long long typedef pair P; int INF = 1e9+7; int mod = 1e9+7; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; signed main() { int N; cin >> N; vectorA(N); vectorB(N); int sum = 0; for(int i = 0; i < N; i++) { cin >> A[i]; sum+=A[i]; } int sum2 = 0; for(int i = 0; i < N; i++) { cin >> B[i]; sum2+=B[i]; } if(N == 2) { cout << abs(A[0]-B[0]) << endl; return 0; } // 190 185 if(abs(sum-sum2)%(N-2) == 0 && sum >= sum2) { int MA = (sum-sum2)/(N-2); int ans = 0; for(int i = 0; i < N; i++) { if(A[i]-MA < B[i]) { if((B[i]-(A[i]-MA))/2 > MA) { cout << -1 << endl; return 0; } ans+=(B[i]-(A[i]-MA))/2; } } cout << ans << endl; } else { cout << -1 << endl; } }