#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  const int N = 3e5 + 5;
  long long v1[N], v2[N];
  cin >> n;
  for (int i = int(0); i < int(n); i++) cin >> v1[i];
  cin >> m;
  for (int i = int(0); i < int(m); i++) cin >> v2[i];
  int c1 = 0, c2 = 0;
  long long sum1 = v1[0], sum2 = v2[0];
  long long ans = 0;
  while (c1 < n and c2 < m) {
    if (sum1 < sum2) {
      c1++;
      if (c1 < n) sum1 += v1[c1];
    } else if (sum1 > sum2) {
      c2++;
      if (c2 < m) sum2 += v2[c2];
    } else {
      c1++;
      c2++;
      ans++;
      if (c2 < m) sum2 = v2[c2];
      if (c1 < n) sum1 = v1[c1];
    }
  }
  if (c1 < n or c2 < m)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
