#include <bits/stdc++.h>
using namespace std;
int n, m, a[300005], b[300005], nextA, nextB, res;
long long SumA, SumB;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  cin >> m;
  for (int i = 1; i <= m; ++i) cin >> b[i];
  nextA = 1, nextB = 1;
  SumA = a[nextA++];
  SumB = b[nextB++];
  while (1) {
    if (nextA == n + 1 && nextB == m + 1) {
      if (SumA == SumB) {
        ++res;
        cout << res;
        return 0;
      }
      cout << -1;
      return 0;
    }
    if (nextA > n + 1 || nextB > m + 1) break;
    if (SumA == 0 && SumA == SumB) {
      SumA = a[nextA++];
      continue;
    }
    if (SumA < SumB) {
      SumA += a[nextA++];
    } else if (SumA > SumB) {
      SumB += b[nextB++];
    } else {
      ++res;
      SumA = 0;
      SumB = 0;
    }
  }
  if (nextA == n + 1 && nextB == m + 1)
    cout << res;
  else
    cout << -1;
}
