#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long __gcd(long long a, long long b) { return b ? __gcd(b, a % b) : a; }
struct pair_hash {
  inline std::size_t operator()(
      const std::pair<long long, long long>& v) const {
    return v.first * 31 + v.second;
  }
};
const long long mod = 1e9 + 7;
vector<long long> binarycut(long long x) {
  vector<long long> v;
  while (x) {
    if (x & 1)
      v.emplace_back(1);
    else
      v.emplace_back(0);
    x >>= 1;
  }
  reverse(v.begin(), v.end());
  return v;
}
long long takemod(long long a) { return ((a % mod) + mod) % mod; }
long long fast_exp(long long base, long long expo) {
  long long res = 1;
  while (expo > 0) {
    if (expo & 1) res = (res * base) % mod;
    base = (base * base) % mod;
    expo >>= 1;
  }
  return res;
}
long long modinv(long long a) { return takemod(fast_exp(takemod(a), mod - 2)); }
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  time_t t1, t2;
  t1 = clock();
  long long n;
  cin >> n;
  deque<long long> a;
  long long sa = 0, sb = 0;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    sa += x;
    a.emplace_back(x);
  }
  long long m;
  cin >> m;
  deque<long long> b;
  for (long long i = 0; i < m; i++) {
    long long x;
    cin >> x;
    sb += x;
    b.emplace_back(x);
  }
  if (sa != sb) {
    cout << -1;
    return 0;
  }
  long long res = 0;
  while (!a.empty() && !b.empty() && a.front() == b.front()) {
    res++;
    a.pop_front();
    b.pop_front();
  }
  while (!a.empty() && !b.empty()) {
    if (a.front() == b.front()) {
      res++;
      a.pop_front();
      b.pop_front();
    } else if (a.front() > b.front()) {
      long long sum = b.front();
      b.pop_front();
      while (!b.empty() && sum < a.front()) {
        sum += b.front();
        b.pop_front();
      }
      b.push_front(sum);
    } else {
      long long sum = a.front();
      a.pop_front();
      while (!a.empty() && sum < b.front()) {
        sum += a.front();
        a.pop_front();
      }
      a.push_front(sum);
    }
  }
  cout << res << '\n';
  t2 = clock();
  cerr << "time taken: " << t2 - t1;
  return 0;
}
