#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300005;
int a[MAX_N];
int b[MAX_N];
int n, m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
  }
  long long value1 = a[1];
  long long value2 = b[1];
  int i = 1;
  int j = 1;
  int ans = 0;
  while (i <= n && j <= m) {
    if (value1 == value2) {
      i++;
      j++;
      ans++;
      if (i <= n) {
        value1 = a[i];
      }
      if (j <= m) {
        value2 = b[j];
      }
    } else if (value1 < value2) {
      i++;
      if (i <= n) {
        value1 += a[i];
      }
    } else {
      j++;
      if (j <= m) {
        value2 += b[j];
      }
    }
  }
  if (i == n + 1 && j == m + 1) {
    cout << ans;
  } else {
    cout << -1;
  }
  return 0;
}
