#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, m;
long long a[N], b[N];
long long suma[N], sumb[N];
int main() {
  scanf("%d", &n);
  suma[0] = 0LL;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    suma[i] = suma[i - 1] + a[i];
  }
  scanf("%d", &m);
  sumb[0] = 0LL;
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &b[i]);
    sumb[i] = sumb[i - 1] + b[i];
  }
  int ans = 0;
  int pa = 0, pb = 0, na = 1, nb;
  while (na <= n) {
    long long tmpa = suma[na] - suma[pa];
    int l = pb + 1, r = m, mid;
    nb = -1;
    while (l <= r) {
      mid = (l + r) / 2;
      if (sumb[mid] - sumb[pb] == tmpa) {
        nb = mid;
        break;
      } else if (sumb[mid] - sumb[pb] < tmpa)
        l = mid + 1;
      else
        r = mid - 1;
    }
    if (nb == -1) {
      na++;
    } else {
      pa = na;
      na++;
      pb = nb;
      ans++;
    }
  }
  if (pa != n || pb != m)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
