#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  while (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];
    int i = 0;
    int j = 0;
    int lim = 1e6;
    while (i < n && j < m && lim-- > 0) {
      if (a[i] == b[j]) {
        i++;
        j++;
        continue;
      }
      while (i + 1 < n && a[i] < b[j]) {
        a[i + 1] += a[i];
        a[i] = 0;
        i++;
      }
      while (j + 1 < m && a[i] > b[j]) {
        b[j + 1] += b[j];
        b[j] = 0;
        j++;
      }
    }
    vector<int> aa, bb;
    for (int i = 0; i < n; ++i)
      if (a[i]) aa.push_back(a[i]);
    for (int i = 0; i < m; ++i)
      if (b[i]) bb.push_back(b[i]);
    if (aa == bb)
      cout << aa.size() << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
