#include <bits/stdc++.h>
using namespace std;
const int ms = 300010;
map<long long, long long> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n;
  long long v1 = 0;
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (i != 0) {
      v1 += a;
      mp[v1] = 1;
    } else {
      v1 = a;
      mp[v1] = 1;
    }
  }
  cin >> m;
  long long v2 = 0;
  int resp = 0;
  for (int i = 0; i < m; i++) {
    long long a;
    cin >> a;
    if (i) {
      v2 += a;
      if (mp[v2] == 1) {
        resp++;
      }
    } else {
      v2 = a;
      if (mp[v2] == 1) {
        resp++;
      }
    }
  }
  if (v1 != v2)
    cout << "-1" << endl;
  else
    cout << resp << endl;
  return 0;
}
