#include <bits/stdc++.h>
using namespace std;
long long a[300000 + 1];
long long sum_a[300000 + 1];
long long b[300000 + 1];
long long sum_b[300000 + 1];
long long query_a(int l, int r) { return sum_a[r] - sum_a[l - 1]; }
long long query_b(int l, int r) { return sum_b[r] - sum_b[l - 1]; }
int main() {
  int n, m, ans, la, ra, lb, rb, i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", a + i);
    sum_a[i] = sum_a[i - 1] + a[i];
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%lld", b + i);
    sum_b[i] = sum_b[i - 1] + b[i];
  }
  la = ra = 1;
  lb = rb = 1;
  ans = 0;
  while (ra <= n and rb <= m) {
    if (query_a(la, ra) < query_b(lb, rb)) {
      ra++;
    } else if (query_a(la, ra) > query_b(lb, rb)) {
      rb++;
    } else {
      la = ra + 1;
      ra = la;
      lb = rb + 1;
      rb = lb;
      ans++;
    }
  }
  if (la == n + 1 and lb == m + 1) {
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
  return 0;
}
