#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  srand(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n;
  vector<long long int> arr(n);
  long long int sum1 = 0;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    cin >> arr[i];
    sum1 += arr[i];
  }
  long long int sum2 = 0;
  cin >> m;
  vector<long long int> brr(m);
  for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
       i += 1 - 2 * ((0) > (m))) {
    cin >> brr[i];
    sum2 += brr[i];
  }
  if (sum1 != sum2) {
    cout << -1;
  } else {
    sum1 = arr[0];
    sum2 = brr[0];
    long long int ind1 = 0, ind2 = 0;
    long long int ans = 0;
    while (ind1 < n && ind2 < m) {
      if (sum1 == sum2) {
        ans++;
        ind1++;
        ind2++;
        if (ind1 < n) sum1 = arr[ind1];
        if (ind2 < m) sum2 = brr[ind2];
      } else {
        if (sum1 < sum2) {
          ind1++;
          if (ind1 < n) sum1 += arr[ind1];
        } else {
          ind2++;
          if (ind2 < m) sum2 += brr[ind2];
        }
      }
    }
    cout << ans;
  }
  return 0;
}
