#include <bits/stdc++.h>
using namespace std;
long long int f_array[1000000];
long long int s_array[1000000];
int main() {
  int n;
  cin >> n;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &f_array[i]);
    sum += f_array[i];
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d", &s_array[i]);
    sum -= s_array[i];
  }
  if (sum != 0) {
    cout << -1 << endl;
  } else {
    int f_pos = 0;
    int s_pos = 0;
    int operations = 0;
    while (f_pos < n || s_pos < m) {
      if (f_array[f_pos] > s_array[s_pos]) {
        s_pos++;
        s_array[s_pos] += s_array[s_pos - 1];
      } else if (f_array[f_pos] < s_array[s_pos]) {
        f_pos++;
        f_array[f_pos] += f_array[f_pos - 1];
      } else {
        s_pos++;
        f_pos++;
        operations++;
      }
    }
    cout << operations << endl;
  }
}
