#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int m;
  cin >> m;
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int i = 0, j = 0, ans = 0;
  long long curr1 = a[0], curr2 = b[0];
  while (i < n || j < m) {
    if (i >= n && curr2 > curr1) {
      ans = -1;
      break;
    }
    if (j >= m && curr1 > curr2) {
      ans = -1;
      break;
    }
    if (curr1 == curr2) {
      i++;
      j++;
      if (i < n)
        curr1 = a[i];
      else
        curr1 = -1;
      if (j < m)
        curr2 = b[j];
      else
        curr2 = -1;
      ans++;
    } else if (curr1 > curr2) {
      j++;
      if (j < m)
        curr2 += b[j];
      else
        curr2 = -1;
    } else {
      i++;
      if (i < n)
        curr1 += a[i];
      else
        curr1 = -1;
    }
  }
  cout << ans << '\n';
  return 0;
}
