#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  cin >> m;
  vector<int> b(m);
  for (int i = 0; i < m; ++i) scanf("%d", &b[i]);
  if (std::accumulate(a.begin(), a.end(), (long long)0) !=
      std::accumulate(b.begin(), b.end(), (long long)0)) {
    cout << -1 << endl;
    return 0;
  }
  int length = 0;
  long long i = 0, j = 0, sumA = a[0], sumB = b[0];
  while (i < n && j < m) {
    if (sumA == sumB) {
      length++;
      if (i + 1 < n) sumA = a[i + 1];
      i++;
      if (j + 1 < m) sumB = b[j + 1];
      j++;
      continue;
    }
    if (sumA < sumB) {
      if (i + 1 < n) sumA += (long long)a[i + 1];
      i++;
    } else {
      if (j + 1 < m) sumB += (long long)b[j + 1];
      j++;
    }
  }
  cout << length << endl;
  return 0;
}
