#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 10;
const long long mod = 2e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
long long a[maxn], b[maxn];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, m, i = 0, j = 0, t1 = 0, t2 = 0, cnt = 0, sum1 = 0, sum2 = 0;
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum1 += a[i];
  }
  scanf("%lld", &m);
  for (long long i = 0; i < m; i++) {
    scanf("%lld", &b[i]);
    sum2 += b[i];
  }
  i = 0, j = 0;
  if (sum1 != sum2) {
    printf("-1\n");
  } else {
    while (1) {
      if (t1 == t2) {
        if (t1 == 0) {
          t1 += a[i], t2 += b[j];
          i++, j++;
        } else {
          cnt++;
          t1 = t2 = 0;
        }
      } else if (t1 < t2) {
        t1 += a[i];
        i++;
      } else {
        t2 += b[j];
        j++;
      }
      if (i > n || j > m) {
        break;
      }
    }
    printf("%lld\n", cnt);
  }
  return 0;
}
