#include <bits/stdc++.h>
using namespace std;
const int oo = 1E9;
const int MAXN = 300010;
int n;
int in[2][MAXN];
vector<int> Fpos(vector<long long> on) {
  vector<int> res(n, oo);
  map<long long, int> pos;
  long long sum = 0;
  pos[0] = n;
  for (int i = n - 1; i >= 0; --i) {
    sum += on[i];
    if (pos.count(sum)) res[i] = pos[sum] - 1;
    pos[sum] = i;
  }
  for (int i = n - 2; i >= 0; --i) (res[i]) = min((res[i]), (res[i + 1]));
  res.push_back(oo);
  return res;
}
struct Compare {
  bool operator()(const pair<int, int>& f, const pair<int, int>& s) const {
    return make_pair(f.first + f.second, f.first) <
           make_pair(s.first + s.second, s.first);
  }
};
set<pair<int, int>, Compare> use;
map<pair<int, int>, int> dp;
void AddState(int x, int y, int v) {
  pair<int, int> state(x, y);
  use.insert(state);
  if (!dp.count(state))
    dp[state] = v;
  else
    (dp[state]) = max((dp[state]), (v));
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &in[0][i]);
  for (int i = 0; i < n; ++i) scanf("%d", &in[1][i]);
  auto top = Fpos(vector<long long>(in[0], in[0] + n));
  auto bot = Fpos(vector<long long>(in[1], in[1] + n));
  vector<long long> sum(in[0], in[0] + n);
  for (int i = 0; i < n; ++i) sum[i] += in[1][i];
  auto two = Fpos(sum);
  int ans = 0;
  use.insert(make_pair(0, 0));
  dp[make_pair(0, 0)] = 0;
  while (!use.empty()) {
    auto c = *use.begin();
    use.erase(c);
    int v = dp[c];
    (ans) = max((ans), (v));
    if (max(c.first, c.second) < n) {
      int e = two[max(c.first, c.second)];
      if (e < oo) AddState(e + 1, e + 1, v + 1);
    }
    int ptop = top[c.first];
    int pbot = bot[c.second];
    if (ptop < oo && pbot >= ptop) AddState(ptop + 1, c.second, v + 1);
    if (pbot < oo && ptop >= pbot) AddState(c.first, pbot + 1, v + 1);
  }
  printf("%d\n", ans);
  return 0;
}
