#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 3e5 + 7;
const long long mod = 1e9 + 7;
long long n, m;
long long a[maxn], b[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  long long ans = 0;
  int pa = 1, pb = 1;
  while (pa <= n && pb <= m) {
    if (a[pa] == b[pb]) {
      ans++;
      pa++;
      pb++;
    } else if (a[pa] < b[pb]) {
      a[pa + 1] += a[pa];
      pa++;
    } else {
      b[pb + 1] += b[pb];
      pb++;
    }
  }
  if (pa == n + 1 && pb == m + 1)
    cout << ans << endl;
  else
    cout << "-1" << endl;
  return 0;
}
