#include <bits/stdc++.h>
using namespace std;
const int M = 3e5 + 5;
long long sum[3][M];
int n, pre[3][M], F[M];
map<long long, int> mp;
map<pair<int, int>, int> f;
int DP(int x, int y) {
  pair<int, int> p = make_pair(x, y);
  if (f.find(p) != f.end()) return f[p];
  int res = F[min(x, y)];
  if (pre[0][x] > pre[1][y])
    res = max(res, DP(pre[0][x], y) + 1);
  else if (pre[1][y])
    res = max(res, DP(x, pre[1][y]) + 1);
  return f[p] = res;
}
int main() {
  scanf("%d", &n);
  n++;
  int i, k, a;
  for (k = 0; k < 2; k++) {
    mp.clear();
    mp[0] = 1;
    for (i = 2; i <= n; i++) {
      scanf("%d", &a);
      sum[k][i] = sum[k][i - 1] + a;
      pre[k][i] = max(pre[k][i - 1], mp[sum[k][i]]);
      mp[sum[k][i]] = i;
    }
  }
  mp.clear();
  mp[0] = 1;
  for (i = 2; i <= n; i++) {
    sum[2][i] = sum[1][i] + sum[0][i];
    pre[2][i] = max(pre[2][i - 1], mp[sum[2][i]]);
    mp[sum[2][i]] = i;
  }
  F[1] = 0;
  F[0] = -1;
  f[make_pair(1, 1)] = 0;
  f[make_pair(0, 0)] = -1;
  for (i = 2; i <= n; i++) {
    F[i] = max(F[pre[2][i]] + 1, DP(i, i));
    f[make_pair(i, i)] = F[i];
  }
  printf("%d\n", F[n]);
  return 0;
}
