#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0) {
      a[i] += a[i - 1];
    }
  }
  cin >> m;
  vector<long long> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i > 0) {
      b[i] += b[i - 1];
    }
  }
  if (a[n - 1] != b[m - 1]) {
    cout << -1 << endl;
    return 0;
  }
  int it1 = 0, it2 = 0, ans = 0;
  while (it1 < n && it2 < m) {
    if (a[it1] == b[it2]) {
      ans++;
      it1++;
      it2++;
    } else if (a[it1] < b[it2]) {
      it1++;
    } else {
      it2++;
    }
  }
  cout << ans << endl;
}
