#include <bits/stdc++.h>
using namespace std;
const int maxn = 300011;
int n, a[maxn], b[maxn];
vector<pair<int, int>> dp0[maxn], dp1[maxn];
int dp[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1; i <= n; ++i) scanf("%d", b + i);
  static int LA[maxn], RA[maxn], LB[maxn], RB[maxn], LS[maxn], RS[maxn];
  {
    map<long long, int> mp;
    long long s = 0;
    mp[s] = 0;
    int Last = -1;
    for (int i = 1; i <= n; ++i) {
      s += a[i];
      if (mp.count(s) && mp[s] >= Last)
        Last = LA[i] = mp[s] + 1;
      else
        LA[i] = -1;
      mp[s] = i;
    }
    memset(RA, 255, sizeof(RA));
    RA[n + 1] = n + 1;
    for (int i = 1; i <= n; ++i)
      if (LA[i] != -1) RA[LA[i]] = i;
    for (int i = n; i >= 1; --i)
      if (RA[i] == -1) RA[i] = RA[i + 1];
  }
  {
    map<long long, int> mp;
    long long s = 0;
    mp[s] = 0;
    int Last = -1;
    for (int i = 1; i <= n; ++i) {
      s += b[i];
      if (mp.count(s) && mp[s] >= Last)
        LB[i] = mp[s] + 1;
      else
        LB[i] = -1;
      mp[s] = i;
    }
    memset(RB, 255, sizeof(RB));
    RB[n + 1] = n + 1;
    for (int i = 1; i <= n; ++i)
      if (LB[i] != -1) RB[LB[i]] = i;
    for (int i = n; i >= 1; --i)
      if (RB[i] == -1) RB[i] = RB[i + 1];
  }
  {
    map<long long, int> mp;
    long long s = 0;
    mp[s] = 0;
    int Last = -1;
    for (int i = 1; i <= n; ++i) {
      s += a[i] + b[i];
      if (mp.count(s) && mp[s] >= Last)
        LS[i] = mp[s] + 1;
      else
        LS[i] = -1;
      mp[s] = i;
    }
    memset(RS, 255, sizeof(RS));
    RS[n + 1] = n + 1;
    for (int i = 1; i <= n; ++i)
      if (LS[i] != -1) RS[LS[i]] = i;
    for (int i = n; i >= 1; --i)
      if (RS[i] == -1) RS[i] = RS[i + 1];
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    dp0[i].emplace_back(RA[i + 1], dp[i] + 1);
    dp1[i].emplace_back(RB[i + 1], dp[i] + 1);
    {
      int mn = n + 1;
      for (auto x : dp0[i])
        if (x.second == dp[i] + 1) mn = min(mn, x.first);
      if (mn <= n) {
        {
          int x = mn, y = i + 1, v = dp[i] + 1;
          if (x < y)
            dp1[x].emplace_back(y, v);
          else if (x > y)
            dp0[y].emplace_back(x, v);
          else
            dp[x] = max(dp[x], v);
        }
        {
          int x = mn, y = RB[i + 1], v = dp[i] + 2;
          if (x < y)
            dp1[x].emplace_back(y, v);
          else if (x > y)
            dp0[y].emplace_back(x, v);
          else
            dp[x] = max(dp[x], v);
        }
      }
    }
    {
      int mn = n + 1;
      for (auto x : dp1[i])
        if (x.second == dp[i] + 1) mn = min(mn, x.first);
      if (mn <= n) {
        {
          int x = i + 1, y = mn, v = dp[i] + 1;
          if (x < y)
            dp1[x].emplace_back(y, v);
          else if (x > y)
            dp0[y].emplace_back(x, v);
          else
            dp[x] = max(dp[x], v);
        }
        {
          int x = RA[i + 1], y = mn, v = dp[i] + 2;
          if (x < y)
            dp1[x].emplace_back(y, v);
          else if (x > y)
            dp0[y].emplace_back(x, v);
          else
            dp[x] = max(dp[x], v);
        }
      }
    }
    dp[RS[i + 1]] = max(dp[RS[i + 1]], dp[i] + 1);
    dp[i + 1] = max(dp[i + 1], dp[i]);
  }
  printf("%d\n", dp[n]);
}
