#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= M;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % M;
    a = a * a % M;
  }
  return res % M;
}
const int N = 2e5 + 10;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  vector<long long> v1(n + 1);
  long long sum1 = 0, sum2 = 0;
  for (int i = 1; i <= n; ++i) cin >> v1[i], sum1 += v1[i];
  cin >> m;
  vector<long long> v2(m + 1);
  for (int i = 1; i <= m; ++i) cin >> v2[i], sum2 += v2[i];
  if (sum1 != sum2) {
    cout << "-1\n";
    return 0;
  }
  int l, r1, r2;
  int ans = 0;
  for (l = 1; l <= n && l <= m; ++l)
    if (v1[l] != v2[l]) break;
  l--;
  ans += l;
  for (r1 = n, r2 = m; r1 > 0 && r2 > 0; --r1, --r2) {
    if (v1[r1] != v2[r2]) break;
  }
  if (r1 > l) ans += n - r1;
  l++;
  if (r1 < l && r2 < l) {
    cout << ans << endl;
    return 0;
  }
  int l1 = l, l2 = l;
  sum1 = v1[l1++], sum2 = v2[l2++];
  while (l1 <= r1 && l2 <= r2) {
    if (sum1 > sum2) {
      sum2 += v2[l2++];
    } else if (sum1 < sum2) {
      sum1 += v1[l1++];
    } else {
      ans++;
      sum1 = v1[l1++], sum2 = v2[l2++];
    }
  }
  ans++;
  cout << ans << endl;
  return 0;
}
