#include <bits/stdc++.h>
using namespace std;
int n, m;
long long a[300002], b[300002];
int main() {
  ios_base::sync_with_stdio(false);
  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];
  int pa = 0, pb = 0;
  long long sa = 0, sb = 0;
  int ans = 0;
  while (pa < n || pb < m) {
    if (sa == sb) {
      pa++;
      sa += a[pa];
    }
    while (sa != sb && (pa < n || pb < m)) {
      if (pb == m || (sa < sb && pa < n)) {
        pa++;
        sa += a[pa];
      } else {
        pb++;
        sb += b[pb];
      }
    }
    if (sa != sb) {
      cout << "-1\n";
      return 0;
    }
    ans++;
    sa = sb = 0;
  }
  cout << ans << "\n";
  return 0;
}
