#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, A[3 * 100005], B[3 * 100005], la, lb, suma, sumb, c = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> B[i];
  }
  la = 0;
  lb = 0;
  while (la < n && lb < m) {
    suma = A[la++];
    sumb = B[lb++];
    while ((la != n || lb != m) && suma != sumb) {
      if (suma < sumb) {
        if (la < n)
          suma += A[la++];
        else {
          cout << -1;
          return 0;
        }
      }
      if (sumb < suma) {
        if (lb < m)
          sumb += B[lb++];
        else {
          cout << -1;
          return 0;
        }
      }
    }
    if (suma != sumb) {
      cout << -1;
      return 0;
    }
    if ((la == n && lb != m) || (la != n && lb == m)) {
      cout << -1;
      return 0;
    }
    c++;
  }
  cout << c;
}
