#include <bits/stdc++.h>
using namespace std;
void solve(long long int input) {
  long long int n;
  cin >> n;
  long long int *a = new long long int[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  long long int m;
  cin >> m;
  long long int *b = new long long int[m];
  for (long long int i = 0; i < m; i++) cin >> b[i];
  long long int s1 = 0, s2 = 0;
  long long int i = 0, j = 0;
  long long int ans = 0;
  bool k = 1;
  while (i < n && j < m) {
    if (a[i] == b[j] && k) {
      ans++;
      i++;
      j++;
      continue;
    }
    if (s1 + a[i] < s2 + b[j]) {
      s1 += a[i];
      i++;
      k = 0;
      continue;
    } else if (s1 + a[i] > s2 + b[j]) {
      s2 += b[j];
      j++;
      k = 0;
      continue;
    } else {
      ans++;
      i++;
      s1 = 0;
      s2 = 0;
      j++;
      k = 1;
      continue;
    }
  }
  if (i == n && j == m) {
    cout << ans << "\n";
  } else {
    cout << "-1"
         << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int x = 1;
  for (long long int i = 1; i <= x; i++) solve(i);
  return 0;
}
