#include <bits/stdc++.h>
using namespace std;
long long ar[4000000], br[4000000];
string s;
int main() {
  long long int i, j, n, k = 0, ans = 0, l = 0, x = 0, a = 0, b = 0, len = 0;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) scanf("%lld", &ar[i]);
  scanf("%lld", &k);
  for (i = 0; i < k; i++) scanf("%lld", &br[i]);
  i = 0, j = 0;
  while (i < n || j < k) {
    a = ar[i];
    b = br[j];
    if (a == b) {
      ans++;
      i++;
      j++;
    } else {
      while (a != b && i < n && j < k) {
        if (a < b) {
          i++;
          a += ar[i];
        } else {
          j++;
          b += br[j];
        }
      }
      if (a == b) {
        ans++;
        i++;
        j++;
      } else {
        printf("-1\n");
        return 0;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
