#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 100;
const int sz = 3e5 + 100;
struct _g {
  int a, b, d;
};
map<long long, int> m;
vector<int> ans;
vector<_g> g[sz];
int calc[3][3][sz], n;
long long pref[3][sz];
void modify(int a, int b, int d) {
  ans[max(a, b)] = max(ans[max(a, b)], d);
  g[min(a, b)].push_back({a, b, d});
}
void trans(int a, int b, int d) {
  if (a <= n) {
    modify(a + 1, b, d);
    int cur = calc[1][0][a];
    if (cur != -1) modify(cur + 1, b, d + 1);
  }
  if (b <= n) {
    modify(a, b + 1, d);
    int cur = calc[0][1][b];
    if (cur != -1) modify(a, cur + 1, d + 1);
  }
  if (a == b && a <= n) {
    int cur = calc[1][1][a];
    if (cur != -1) modify(cur + 1, cur + 1, d + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int i, j, k;
  cin >> n;
  ans.resize(n + 2);
  for (i = 1; i <= 2; i++)
    for (j = 1; j <= n; j++) {
      cin >> pref[i][j];
      pref[i][j] += pref[i][j - 1];
    }
  for (i = 0; i <= 1; i++)
    for (j = 0; j <= 1; j++) {
      m.clear();
      for (k = n; k >= 1; k--) {
        long long need = 0;
        long long cur = pref[1][k] * i + pref[2][k] * j;
        m[cur] = k;
        if (k != 1) need = pref[1][k - 1] * i + pref[2][k - 1] * j;
        if (m.count(need))
          calc[i][j][k] = m[need];
        else
          calc[i][j][k] = -1;
      }
    }
  for (i = 1; i <= n; i++) {
    int cur = ans[i];
    trans(i, i, cur);
    int u = inf, d = inf;
    for (j = 0; j < g[i].size(); j++) {
      if (g[i][j].a == i && g[i][j].d == cur + 1) d = min(d, g[i][j].b);
      if (g[i][j].b == i && g[i][j].d == cur + 1) u = min(u, g[i][j].a);
    }
    if (d != inf) trans(i, d, cur + 1);
    if (u != inf) trans(u, i, cur + 1);
  }
  cout << ans[n + 1];
  return 0;
}
