#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 300005;
int n, m;
long long a[N], b[N];
int main() {
  scanf("%d", &n);
  long long sum1 = 0, sum2 = 0;
  for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), sum1 += a[i];
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) scanf("%lld", &b[i]), sum2 += b[i];
  if (sum1 != sum2)
    puts("-1");
  else {
    sum1 = a[1], sum2 = b[1];
    int ans = 0;
    for (int i = 1, j = 1; i <= n && j <= m;) {
      if (sum1 == sum2) {
        ++i, ++j;
        sum1 = a[i], sum2 = b[j];
        ++ans;
      } else {
        if (sum1 < sum2) {
          ++i;
          sum1 += a[i];
        } else {
          ++j;
          sum2 += b[j];
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
