#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:128777216")
using namespace std;
const long double eps = 1e-12;
mt19937 rndm;
void start() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  cout.setf(ios::fixed);
  string FILENAME = "navalny";
  rndm.seed(time(0));
}
int main() {
  start();
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int m;
  cin >> m;
  vector<long long> b(m);
  for (int i = 0; i < m; ++i) cin >> b[i];
  int lena = 0, lenb = 0;
  int posa = 0, posb = 0;
  int ans = 0;
  while (lena < n || lenb < m) {
    if (posa == lena || posb == lenb) {
      if (posa == lena) ++posa;
      if (posb == lenb) ++posb;
      continue;
    }
    if (lena < n && !a[lena]) {
      ++lena;
      continue;
    }
    if (lenb < m && !b[lenb]) {
      ++lenb;
      continue;
    }
    if (a[lena] == b[lenb])
      ++lena, ++lenb, ++ans;
    else if (a[lena] < b[lenb]) {
      if (posa == n) return !(cout << -1);
      a[lena] += a[posa], a[posa++] = 0;
    } else {
      if (posb == m) return !(cout << -1);
      b[lenb] += b[posb], b[posb++] = 0;
    }
  }
  cout << ans;
  return 0;
}
