#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
const int N = 2e5 + 1;
const long long int mod = 1e9 + 7;
void solve() {
  long long int n, m, i, j, sum1, sum2;
  sum1 = sum2 = 0;
  cin >> n;
  long long int a[n];
  for (i = 0; i < n; i++) cin >> a[i], sum1 += a[i];
  cin >> m;
  long long int b[m];
  for (i = 0; i < m; i++) cin >> b[i], sum2 += b[i];
  if (sum1 != sum2)
    cout << -1;
  else {
    i = j = 0;
    long long int s1 = a[i], s2 = b[j], ans = 0;
    i++;
    j++;
    while (i < n || j < m) {
      if (s1 == s2) {
        ans++;
        s1 = a[i];
        s2 = b[j];
        i++;
        j++;
      } else if (s1 > s2) {
        while (s1 > s2 && j < m) {
          s2 += b[j];
          j++;
        }
      } else {
        while (s1 < s2 && i < n) {
          s1 += a[i];
          i++;
        }
      }
    }
    if (s1 == s2) ans++;
    cout << ans;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
