#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const double EPS = 1e-9;
const double PI = 3.14159265359;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> m;
  vector<long long> b(m);
  for (int i = 0; i < m; ++i) cin >> b[i];
  long long ans = 0;
  while (a.size() && b.size()) {
    while (a.size() && b.size() && a.back() == b.back()) {
      a.pop_back();
      b.pop_back();
      ++ans;
    }
    bool in = 0;
    while (a.size() && b.size() > 1 && a.back() > b.back()) {
      b[b.size() - 2] += b.back();
      b.pop_back();
      in = 1;
    }
    while (a.size() > 1 && b.size() && a.back() < b.back()) {
      a[a.size() - 2] += a.back();
      a.pop_back();
      in = 1;
    }
    if (!in) break;
  }
  if (a.size() || b.size())
    cout << -1;
  else
    cout << ans;
  return 0;
}
