#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (long long &i : a) cin >> i;
  int m;
  cin >> m;
  vector<long long> b(m);
  for (long long &i : b) cin >> i;
  long long sum_a = accumulate(a.begin(), a.end(), 0);
  long long sum_b = accumulate(b.begin(), b.end(), 0);
  if (sum_a != sum_b) {
    cout << -1 << endl;
    return 0;
  }
  int i = 0, j = 0;
  int cnt = 0;
  while (i < n && j < m) {
    if (a[i] == b[j]) {
      cnt++;
      i++;
      j++;
    } else {
      if (a[i] > b[j]) {
        if (j < m - 1) {
          j++;
          b[j] += b[j - 1];
        } else {
          cout << -1 << endl;
          return 0;
        }
      } else {
        if (i < n - 1) {
          i++;
          a[i] += a[i - 1];
        } else {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  if (i == n && j == m)
    cout << cnt << endl;
  else
    cout << -1 << endl;
  return 0;
}
