#include <bits/stdc++.h>
using namespace std;
void READ(vector<long long> &v, long long n) {
  long long a;
  for (long long i = 0; i < n; i = i + 1) {
    cin >> a;
    v.push_back(a);
  }
}
void PRINT(vector<long long> &v, long long a = 0) {
  for (long long i = a; i < v.size(); i = i + 1) {
    cout << v[i] << " ";
  }
  cout << "\n";
}
double logy(long long n, long long b) {
  if (b == 0) {
    return (-1);
  }
  if (n == 1) {
    return (0);
  }
  return ((double)log10(n) / log10(b));
}
long long power(long long k, long long n, long long m = 1000000007) {
  long long res = 1;
  while (n) {
    if (n % 2 != 0) {
      res = (res * k) % m;
    }
    k = (k * k) % m;
    n = n / 2;
  }
  return (res);
}
double power(double k, long long n) {
  double res = 1;
  while (n) {
    if (n % 2 != 0) {
      res = (res * k);
    }
    k = (k * k);
    n = n / 2;
  }
  return (res);
}
double distance(pair<long long, long long> a, pair<long long, long long> b) {
  return ((double)(sqrt((a.first - b.first) * (a.first - b.first) +
                        (a.second - b.second) * (a.second - b.second))));
}
const long long dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const long long dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vector<pair<long long, long long> > dr;
long long n, m, k, h;
bool ok = false;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(std::ios::floatfield);
  std::cout.precision((long long)10);
  long long a, b, c, d, i, j, t, x, y, z;
  char ch, chr;
  string str, st, str1, str2, str3, str4;
  cin >> n;
  vector<long long> v;
  READ(v, n);
  vector<long long> v1;
  cin >> m;
  READ(v1, m);
  a = accumulate((v).begin(), (v).end(), (long long)0);
  b = accumulate((v1).begin(), (v1).end(), (long long)0);
  if (a != b) {
    cout << "-1";
    exit(0);
  }
  vector<long long> pref(n, 0), pref1(m, 0);
  pref[0] = v[0];
  pref1[0] = v1[0];
  for (i = 1; i < n; i++) {
    pref[i] = pref[i - 1] + v[i];
  }
  for (int(i) = (1); (i) < (m); ++(i)) {
    pref1[i] = pref1[i - 1] + v1[i];
  }
  x = 0;
  i = 0;
  j = 0;
  while (i < n && j < m) {
    if (pref[i] == pref1[j]) {
      x++;
      i++;
      j++;
    } else if (pref[i] > pref1[j]) {
      j++;
    } else {
      i++;
    }
  }
  cout << x;
}
