#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
long long sum[3][maxn];
int pre[3][maxn];
map<pair<int, int>, int> vis, dp;
int ans[maxn];
map<long long, int> pos;
int dfs(int x, int y) {
  if (vis[make_pair((x), (y))])
    return dp[make_pair((x), (y))];
  else {
    vis[make_pair((x), (y))] = 1;
    dp[make_pair((x), (y))] =
        max(ans[min(x, y)], pre[0][x] > pre[1][y] ? dfs(pre[0][x], y) + 1
                                                  : dfs(x, pre[1][y]) + 1);
    return dp[make_pair((x), (y))];
  }
}
int main() {
  int n, a;
  cin >> n;
  n++;
  for (int i = 2; i <= n; i++) {
    cin >> a;
    sum[0][i] = sum[0][i - 1] + a;
  }
  for (int i = 2; i <= n; i++) {
    cin >> a;
    sum[1][i] = sum[1][i - 1] + a;
    sum[2][i] = sum[0][i] + sum[1][i];
  }
  for (int k = 0; k < 3; k++) {
    pos.clear();
    for (int i = 1; i <= n; i++) {
      pre[k][i] = max(pre[k][i - 1], pos[sum[k][i]]);
      pos[sum[k][i]] = i;
    }
  }
  ans[0] = pre[0][0] = pre[1][0] = -1;
  vis[make_pair((0), (0))] = 1;
  dp[make_pair((0), (0))] = -2;
  for (int i = 2; i <= n; i++) {
    ans[i] = max(ans[pre[2][i]] + 1, dfs(i, i));
    dp[make_pair((i), (i))] = ans[i];
  }
  cout << ans[n] << "\n";
  return 0;
}
