#include <bits/stdc++.h>
using namespace std;
int const N = 5000000;
int n, m;
long long v[N], vv[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%lld", v + i);
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) scanf("%lld", vv + i);
  int an = 0;
  int i = 0, j = 0;
  long long sm0 = v[0], sm1 = vv[0];
  int lsti = i, lstj = j;
  while (i < n || j < m) {
    if (i == lsti && j == lstj && lsti && lstj) break;
    if (sm0 == sm1) {
      ++i, ++j, ++an;
      sm0 = sm1 = 0;
      sm0 = (long long)v[i], sm1 = (long long)vv[j];
      continue;
    }
    if (sm0 < sm1) {
      ++i;
      sm0 = (long long)(sm0 + v[i]);
      continue;
    }
    if (sm1 < sm0) {
      ++j;
      sm1 = (long long)(sm1 + vv[j]);
      continue;
    }
  }
  if (i != n || j != m) an = -1;
  printf("%d\n", an);
}
