#include <bits/stdc++.h>
using namespace std;
int n, m, res;
long long a[300005], b[300005];
int main() {
  ios_base::sync_with_stdio(0);
  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];
  int i = 1, j = 1;
  while (i <= n && j <= m) {
    long long s1 = a[i], s2 = b[j];
    ++i;
    ++j;
    while ((s1 < s2 && i <= n) || (s2 < s1 && j <= m)) {
      while (s1 < s2 && i <= n) s1 += a[i++];
      while (s2 < s1 && j <= m) s2 += b[j++];
    }
    if (s1 != s2) {
      cout << -1;
      return 0;
    } else
      ++res;
  }
  if (i <= n || j <= m)
    cout << -1;
  else
    cout << res;
}
