#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
int n;
int a[2][310000], f[3][310000], g[310000];
long long sum[3][310000];
map<long long, int> pre;
map<pair<int, int>, int> ma;
void calc(int tp) {
  pre.clear();
  pre[0] = 0;
  for (int i = 1; i <= n; i++) {
    f[tp][i] = f[tp][i - 1];
    if (pre.count(sum[tp][i])) f[tp][i] = max(f[tp][i], pre[sum[tp][i]] + 1);
    pre[sum[tp][i]] = i;
  }
}
void upd(int &x, int y) { x = max(x, y); }
int solve(int x, int y) {
  if (x < 0 || y < 0) return -inf;
  if (ma.count(make_pair(x, y))) return ma[make_pair(x, y)];
  int ret = g[min(x, y)];
  if (x >= y) ret = max(ret, solve(f[0][x] - 1, y) + 1);
  if (y >= x) ret = max(ret, solve(x, f[1][y] - 1) + 1);
  return ma[make_pair(x, y)] = ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[0][i]);
    sum[0][i] = sum[0][i - 1] + a[0][i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[1][i]);
    sum[1][i] = sum[1][i - 1] + a[1][i];
    sum[2][i] = sum[1][i] + sum[0][i];
  }
  calc(0);
  calc(1);
  calc(2);
  for (int i = 1; i <= n; i++) {
    g[i] = g[i - 1];
    if (f[2][i]) g[i] = max(g[i], g[f[2][i] - 1] + 1);
    g[i] = max(g[i], solve(f[0][i] - 1, i) + 1);
    g[i] = max(g[i], solve(i, f[1][i] - 1) + 1);
  }
  printf("%d\n", g[n]);
  return 0;
}
