#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
long long n, m, a[N], b[N], sum1[N], sum2[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], sum1[i] += sum1[i - 1] + a[i];
  cin >> m;
  for (int i = 1; i <= m; ++i) cin >> b[i], sum2[i] += sum2[i - 1] + b[i];
  if (sum1[n] != sum2[m]) {
    cout << -1 << '\n';
    return 0;
  }
  int i = 1, j = 1, len = 0;
  while (i <= n && j <= m) {
    if (sum1[i] > sum2[j])
      j++;
    else if (sum1[i] < sum2[j])
      ++i;
    else
      i++, j++, len++;
  }
  cout << len << '\n';
  return 0;
}
