#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double f = 2.32349;
long long a[N], b[N];
void solve() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, m;
  cin >> n;
  long long suma = 0, sumb = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    suma += a[i];
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sumb += b[i];
  }
  if (suma != sumb) {
    cout << -1 << '\n';
  } else {
    int ansn = n, ansm = m;
    int la = 1, lb = 1;
    while (la != n + 1 && lb != m + 1) {
      if (a[la] == b[lb]) {
        la++;
        lb++;
      } else if (a[la] < b[lb]) {
        a[la + 1] += a[la];
        la++;
        ansn--;
      } else {
        b[lb + 1] += b[lb];
        lb++;
        ansm--;
      }
    }
    cout << ansn << '\n';
  }
}
signed main() {
  solve();
  return 0;
}
