#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') q = 0, c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
long long s0[N], s1[N], s[N];
int nxt[N], nxt0[N], nxt1[N];
int f[N], n;
vector<pair<pair<int, int>, int> > v[N];
inline void upd(int &x, int y) { x < y ? x = y : 0; }
inline void ins(int a, int b, int w) {
  upd(f[max(a, b)], w);
  v[min(a, b)].push_back(make_pair(make_pair(a, b), w));
}
inline void update(int a, int b, int w) {
  if (a < n) {
    ins(a + 1, b, w);
    if (nxt0[a]) ins(nxt0[a], b, w + 1);
  }
  if (b < n) {
    ins(a, b + 1, w);
    if (nxt1[b]) ins(a, nxt1[b], w + 1);
  }
}
int main() {
  n = gi();
  int i, A, B;
  map<long long, int> mp;
  vector<pair<pair<int, int>, int> >::iterator it;
  for (i = 1; i <= n; i++) s0[i] = s0[i - 1] + gi();
  for (i = 1; i <= n; i++) s[i] = s0[i] + (s1[i] = s1[i - 1] + gi());
  for (i = n; i >= 0; i--) nxt0[i] = mp[s0[i]], mp[s0[i]] = i;
  for (i = n, mp.clear(); i >= 0; i--) nxt1[i] = mp[s1[i]], mp[s1[i]] = i;
  for (i = n, mp.clear(); i >= 0; i--) nxt[i] = mp[s[i]], mp[s[i]] = i;
  for (i = 0; i <= n; i++) {
    update(i, i, f[i]);
    upd(f[i + 1], f[i]);
    if (nxt[i]) upd(f[nxt[i]], f[i] + 1);
    for (it = v[i].begin(), A = B = n + 1; it != v[i].end(); it++)
      if (it->second == f[i] + 1) {
        if (it->first.second == i) A = min(A, it->first.first);
        if (it->first.first == i) B = min(B, it->first.second);
      }
    if (A != n + 1) update(A, i, f[i] + 1);
    if (B != n + 1) update(i, B, f[i] + 1);
  }
  printf("%d\n", f[n]);
  return 0;
}
