#include <bits/stdc++.h>
using namespace std;
long long a[1000000], n, b[1000000], m, i, j, l1, l2;
int main() {
  scanf("%lld", &n);
  i = 0;
  while (i < n) {
    scanf("%lld", &a[i]);
    i++;
  }
  scanf("%lld", &m);
  j = 0;
  while (j < m) {
    scanf("%lld", &b[j]);
    j++;
  }
  l1 = n;
  l2 = m;
  i = 0;
  j = 0;
  while ((i < n) && (j < m)) {
    if (a[i] > b[j]) {
      b[j + 1] += b[j];
      j++;
      l2--;
    }
    if (b[j] > a[i]) {
      a[i + 1] += a[i];
      i++;
      l1--;
    }
    if (a[i] == b[j]) {
      i++;
      j++;
    }
  }
  if ((l1 == l2) && (a[n - 1] == b[m - 1])) {
    cout << l1;
  } else {
    cout << -1;
  }
  return 0;
}
