#include <bits/stdc++.h>
using namespace std;
long long int MOD(long long int a, long long int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}
int max3(int a, int b, int c) { return max(c, max(a, b)); }
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int logg(long long int a) {
  long long int x = 0;
  while (a > 1) {
    x++;
    a /= 2;
  }
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  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 sum = 0;
  long long int t = 0;
  long long int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) sum1 += A[i];
  for (int i = 0; i < m; i++) sum2 += B[i];
  if (sum1 != sum2) {
    cout << -1;
    exit(0);
  }
  int i = 0, j = 0;
  long long int t1 = A[0], t2 = B[0];
  while (i < n && j < m) {
    if (t1 == t2) {
      sum++;
      i++;
      j++;
      if (i == n && j == m) break;
      t1 = A[i];
      t2 = B[j];
    } else if (t1 > t2) {
      j++;
      t2 += B[j];
    } else {
      i++;
      t1 += A[i];
    }
  }
  cout << sum;
}
