#include <bits/stdc++.h>
using namespace std;
const int N = 3.1e5;
int n, in[N];
unordered_map<int64_t, int> ha, hb, hc;
vector<pair<int, int>> a, b, c;
int pa[N], pb[N], pc[N];
int getint() {
  int r, x, s = 1;
  while ((r = getchar()) < 33)
    ;
  if (r == '-')
    r = 0, s = -1;
  else
    r -= '0';
  while ((x = getchar()) >= '0') r = r * 10 + x - '0';
  return r * s;
}
int f[N];
map<pair<int, int>, int> g;
int ga[N], gb[N];
int dfs(int p, int q) {
  auto z = make_pair(p, q);
  if (g.count(z)) return g[z];
  int ans = 0;
  if (!p && !q) return g[z] = 0;
  int last = min(a[p].first, b[q].first) - 1;
  if (last >= 0) ans = max(ans, f[pc[last]] + 2);
  int na = p ? a[pa[a[p].first - 1]].first : -1;
  int nb = q ? b[pb[b[q].first - 1]].first : -1;
  if (na >= nb) ans = max(ans, 1 + dfs(pa[a[p].first - 1], q));
  if (na <= nb) ans = max(ans, 1 + dfs(p, pb[b[q].first - 1]));
  return g[z] = ans;
}
int dfsa(int p) {
  if (ga[p] >= 0) return ga[p];
  if (!p) return ga[p] = 0;
  int ans = 1 + dfsa(pa[a[p].first - 1]);
  ans = max(ans, 1 + f[pc[a[p].first - 1]]);
  return ga[p] = ans;
}
int dfsb(int p) {
  if (gb[p] >= 0) return gb[p];
  if (!p) return gb[p] = 0;
  int ans = 1 + dfsb(pb[b[p].first - 1]);
  ans = max(ans, 1 + f[pc[b[p].first - 1]]);
  return gb[p] = ans;
}
int main() {
  n = getint();
  int64_t s = 0;
  ha[0] = hb[0] = hc[0] = 0;
  a.emplace_back(0, 0);
  b = c = a;
  for (int i = 1; i <= n; ++i) {
    in[i] = getint();
    s += in[i];
    if (ha.count(s))
      if (a.empty() || ha[s] + 1 > a.back().first) a.emplace_back(ha[s] + 1, i);
    ha[s] = i;
  }
  int64_t t = s = 0;
  for (int i = 1; i <= n; ++i) {
    int x = getint();
    s += x;
    t += x + in[i];
    if (hb.count(s))
      if (b.empty() || hb[s] + 1 > b.back().first) b.emplace_back(hb[s] + 1, i);
    if (hc.count(t))
      if (c.empty() || hc[t] + 1 > c.back().first) c.emplace_back(hc[t] + 1, i);
    hb[s] = i;
    hc[t] = i;
  }
  c.emplace_back(n + 1, n + 1);
  int qa = 0, qb = 0, qc = 0;
  for (int i = 1; i <= n + 1; ++i) {
    while (qa < a.size() && a[qa].second <= i) ++qa;
    while (qb < b.size() && b[qb].second <= i) ++qb;
    while (qc < c.size() && c[qc].second <= i) ++qc;
    pa[i] = qa - 1;
    pb[i] = qb - 1;
    pc[i] = qc - 1;
  }
  memset(ga, -1, sizeof(ga));
  memset(gb, -1, sizeof(gb));
  for (int i = 1; i < c.size(); ++i) {
    int v = c[i].first - 1;
    int la = pa[v], lb = pb[v], lc = pc[v];
    f[i] = 1 + max(max(f[lc], dfs(la, lb)), max(dfsa(la), dfsb(lb)));
  }
  cout << f[c.size() - 1] - 1 << endl;
  return 0;
}
