#include <bits/stdc++.h>
using namespace std;
void getre() {
  int x = 0;
  printf("%d\n", 1 / x);
}
void gettle() {
  int res = 1;
  while (1) res <<= 1;
  printf("%d\n", res);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename N, typename PN>
inline N flo(N a, PN b) {
  return a >= 0 ? a / b : -((-a - 1) / b) - 1;
}
template <typename N, typename PN>
inline N cei(N a, PN b) {
  return a > 0 ? (a - 1) / b + 1 : -(-a / b);
}
template <typename N>
N gcd(N a, N b) {
  return b ? gcd(b, a % b) : a;
}
template <typename N>
inline int sgn(N a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
  while ((c = getchar()) > 126 || c < 33)
    ;
}
inline void pc(char c) { putchar(c); }
inline long long sqr(long long a) { return a * a; }
inline double sqrf(double a) { return a * a; }
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
int n;
long long a[3][333333];
int nex[3][333333];
int go[3][20][333333];
map<long long, int> cnt;
void init(long long *a, int *nex) {
  cnt.clear();
  int las = 0;
  for (int i = (0), _ed = (n + 1); i < _ed; i++) {
    cnt[a[i]]++;
    while (cnt[a[i]] >= 2) {
      nex[las] = i;
      cnt[a[las]]--;
      las++;
    }
  }
  for (int j = (las), _ed = (n + 2); j < _ed; j++) nex[j] = n + 1;
}
int f[333333];
map<int, int> g[2][333333];
void kai(map<int, int> *g, int i, int j) {
  if (!g[i].count(j)) g[i][j] = inf;
}
int zhui(int id, int far, int low, int &bushu) {
  bushu = 0;
  for (int i = 19; i >= 0; i--) {
    if (go[id][i][low] <= far) {
      bushu += 1 << i;
      low = go[id][i][low];
    }
  }
  return low;
}
int main() {
  gn(n);
  for (int i = (1), _ed = (n + 1); i < _ed; i++) gn(a[0][i]);
  for (int i = (1), _ed = (n + 1); i < _ed; i++) gn(a[1][i]);
  for (int i = (1), _ed = (n + 1); i < _ed; i++) {
    a[0][i] = a[0][i - 1] + a[0][i];
    a[1][i] = a[1][i - 1] + a[1][i];
  }
  for (int i = (0), _ed = (n + 1); i < _ed; i++) a[2][i] = a[0][i] + a[1][i];
  for (int j = (0), _ed = (3); j < _ed; j++) {
    init(a[j], nex[j]);
    for (int u = (0), _ed = (n + 2); u < _ed; u++) go[j][0][u] = nex[j][u];
    for (int i = (1), _ed = (20); i < _ed; i++) {
      for (int u = (0), _ed = (n + 2); u < _ed; u++)
        go[j][i][u] = go[j][i - 1][go[j][i - 1][u]];
    }
  }
  f[0] = 0;
  for (int i = (0), _ed = (n + 1); i < _ed; i++) {
    for (__typeof((g[0][i]).begin()) it = (g[0][i]).begin();
         it != (g[0][i]).end(); it++) {
      upmax(f[max(i, it->second)], it->first);
    }
    for (__typeof((g[1][i]).begin()) it = (g[1][i]).begin();
         it != (g[1][i]).end(); it++) {
      upmax(f[max(i, it->second)], it->first);
    }
    int res = f[i];
    upmax(f[nex[2][i]], res + 1);
    kai(g[0], i, res + 1);
    upmin(g[0][i][res + 1], nex[0][i]);
    kai(g[1], i, res + 1);
    upmin(g[1][i][res + 1], nex[1][i]);
    kai(g[0], i + 1, res + 1);
    kai(g[1], i + 1, res + 1);
    upmin(g[0][i + 1][res + 1], g[0][i][res + 1]);
    upmin(g[1][i + 1][res + 1], g[1][i][res + 1]);
    kai(g[0], nex[1][i], res + 2);
    upmin(g[0][nex[1][i]][res + 2], g[0][i][res + 1]);
    kai(g[1], nex[0][i], res + 2);
    upmin(g[1][nex[0][i]][res + 2], g[1][i][res + 1]);
    kai(g[1], g[0][i][res + 1], res + 2);
    kai(g[0], g[1][i][res + 1], res + 2);
    upmin(g[1][g[0][i][res + 1]][res + 2], nex[1][i]);
    upmin(g[0][g[1][i][res + 1]][res + 2], nex[0][i]);
    upmax(f[i + 1], f[i]);
    upmax(f[g[0][i][res + 1]], res + 1);
    upmax(f[g[1][i][res + 1]], res + 1);
  }
  int ans = 0;
  for (int i = (0), _ed = (n + 1); i < _ed; i++) upmax(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}
