#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e6 + 10;
const long long inf = 1e15;
void makeFaster() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long a[mxn], b[mxn];
int main() {
  makeFaster();
  int n;
  long long sa = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sa += a[i];
  }
  long long sb = 0;
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    sb += b[i];
  }
  long long ans = 0;
  if (sa != sb)
    cout << -1 << endl;
  else {
    int i = 0, j = 0;
    while (i < n || j < m) {
      if (a[i] == b[j]) {
        ans += 1;
        i++;
        j++;
      } else if (a[i] > b[j]) {
        a[i] -= b[j];
        j++;
      } else if (b[j] > a[i]) {
        b[j] -= a[i];
        i++;
      }
    }
    cout << ans << endl;
  }
}
