#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3c3c3c3c;
const long long infl = 0x3c3c3c3c3c3c3c3c;
int N, M;
int A[300003];
int B[300003];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  int posA = 0, posB = 0;
  long long sumA = A[0], sumB = B[0];
  int cnt = 0;
  while (posA < N && posB < M) {
    if (sumA == sumB) {
      cnt++;
      posA++;
      posB++;
      if (posA < N) {
        sumA = A[posA];
      }
      if (posB < M) {
        sumB = B[posB];
      }
    } else if (sumA < sumB) {
      posA++;
      if (posA < N) {
        sumA += A[posA];
      }
    } else {
      posB++;
      if (posB < M) {
        sumB += B[posB];
      }
    }
  }
  if (posA == N && posB == M) {
    cout << cnt;
  } else {
    cout << -1;
  }
}
