#include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; namespace mp = boost::multiprecision; int main() { ll N; cin >> N; ll a_sum = 0; ll b_sum = 0; vector A(N); vector B(N); for (size_t i = 0; i < N; i++) { cin >> A[i]; a_sum += A[i]; } for (size_t i = 0; i < N; i++) { cin >> B[i]; b_sum += B[i]; } ll total = 0; vector diffs(N); for (size_t i = 0; i < N; i++) { diffs[i] = B[i] - A[i]; total += B[i] - A[i]; } if (N == 2){ if (total == 0){ ll changes = 0; ll current = 0; for(int i = 0; i < N;i++){ if (current > 0 && diffs[i] < 0){ changes += min(abs(current),abs(diffs[i])); current += diffs[i]; }else if (current < 0 && diffs[i] > 0){ changes += min(abs(current),abs(diffs[i])); current += diffs[i]; }else{ current += diffs[i]; } } cout << changes << endl; }else{ cout << -1 << endl; } }else{ // cout << total % (N-2) << endl; if (total % (N-2) != 0){ cout << -1 << endl; }else{ bool can_make = true; for(int i = 0; i < N;i++){ diffs[i] = diffs[i] + total/(N-2); if (diffs[i] > 0 || abs(diffs[i]) % 2 != 0){ can_make = false; } } if (can_make){ cout << - total/(N-2) << endl;; }else{ cout << -1 << endl; } } } return 0; }