#include <bits/stdc++.h>
using namespace std;
int n, m, i = 1, j = 1;
long long sum1, sum2;
vector<long long> arr1(3e5 + 5), arr2(3e5 + 5), ans;
int main(void) {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> arr1[i];
    sum1 += arr1[i];
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> arr2[i];
    sum2 += arr2[i];
  }
  if (sum1 != sum2) {
    cout << -1 << "\n";
    return 0;
  }
  while (i <= n && j <= m) {
    sum1 = arr1[i];
    sum2 = arr2[j];
    while (true) {
      if (sum1 == sum2) {
        ans.emplace_back(sum1);
        ++i;
        ++j;
        break;
      } else if (sum1 < sum2) {
        ++i;
        sum1 += arr1[i];
      } else {
        ++j;
        sum2 += arr2[j];
      }
    }
  }
  cout << ans.size() << "\n";
  return 0;
}
