#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100;
long long s1[maxn];
long long s2[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  s1[0] = s2[0] = 0;
  int n, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1[i + 1];
    s1[i + 1] += s1[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> s2[i + 1];
    s2[i + 1] += s2[i];
  }
  if (s1[n] != s2[m]) {
    cout << -1 << endl;
  } else {
    int i = 1, j = 1;
    int ans = 0;
    while (i <= n && j <= m) {
      if (s1[i] == s2[j]) {
        i++, j++;
      } else if (s1[i] < s2[j]) {
        i++;
        ans++;
      } else if (s1[i] > s2[j]) {
        j++;
      }
    }
    cout << n - ans << endl;
  }
  return 0;
}
