#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n;
  vector<int> A(n);
  vector<long long> Asum(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
    if (i == 0)
      Asum[i] = A[i];
    else
      Asum[i] = Asum[i - 1] + A[i];
  }
  cin >> m;
  vector<int> B(m);
  vector<long long> Bsum(m);
  for (int i = 0; i < m; ++i) {
    scanf("%d", &B[i]);
    if (i == 0)
      Bsum[i] = B[i];
    else
      Bsum[i] = Bsum[i - 1] + B[i];
  }
  if (Asum[n - 1] != Bsum[m - 1]) {
    cout << "-1";
    return 0;
  }
  int i = 0, j = 0;
  int ans = 0;
  while (i < n || j < m) {
    if (Asum[i] < Bsum[j]) ++i;
    if (Asum[i] > Bsum[j]) ++j;
    if (Asum[i] == Bsum[j]) {
      ++ans;
      ++i;
      ++j;
    }
  }
  cout << ans;
  return 0;
}
