#include <bits/stdc++.h>
using namespace std;
using namespace std;
int mx[21][200005];
int mn[21][200005];
int _2pow[25];
int Log2[200005];
void ST1(int *a, int n) {
  _2pow[0] = 1;
  for (int i = 1; i <= 21; i++) {
    _2pow[i] = _2pow[i - 1] * 2;
  }
  Log2[0] = -1;
  for (int i = 1; i <= n; i++) {
    Log2[i] = Log2[i / 2] + 1;
  }
  for (int i = 1; i <= n; i++) {
    mx[0][i] = a[i];
  }
  for (int i = 1; i <= Log2[n]; i++) {
    for (int j = 1; j <= n; j++) {
      if (j + _2pow[i] - 1 <= n) {
        mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + _2pow[i - 1]]);
      }
    }
  }
}
void ST2(int *a, int n) {
  _2pow[0] = 1;
  for (int i = 1; i <= 21; i++) {
    _2pow[i] = _2pow[i - 1] * 2;
  }
  Log2[0] = -1;
  for (int i = 1; i <= n; i++) {
    Log2[i] = Log2[i / 2] + 1;
  }
  for (int i = 1; i <= n; i++) {
    mn[0][i] = a[i];
  }
  for (int i = 1; i <= Log2[n]; i++) {
    for (int j = 1; j <= n; j++) {
      if (j + _2pow[i] - 1 <= n) {
        mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + _2pow[i - 1]]);
      }
    }
  }
}
int chazhaomax(int x, int y) {
  int t = Log2[y - x + 1];
  return max(mx[t][x], mx[t][y + 1 - _2pow[t]]);
}
int chazhaomin(int x, int y) {
  int t = Log2[y - x + 1];
  return min(mn[t][x], mn[t][y - _2pow[t] + 1]);
}
int main() {
  int n;
  cin >> n;
  int a[n + 2];
  int b[n + 2];
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  ST1(a, n);
  ST2(b, n);
  int l, r, mid;
  long long ans1, ans2, sum = 0;
  for (int i = 1; i <= n; i++) {
    l = i;
    r = n;
    ans1 = -1, ans2 = -1;
    while (l <= r) {
      mid = (l + r) / 2;
      int x = chazhaomax(i, mid);
      int y = chazhaomin(i, mid);
      if (x < y) {
        l = mid + 1;
      } else {
        if (x == y) {
          ans1 = mid;
        }
        r = mid - 1;
      }
    }
    l = i;
    r = n;
    while (l <= r) {
      mid = (l + r) / 2;
      int x = chazhaomax(i, mid);
      int y = chazhaomin(i, mid);
      if (x <= y) {
        if (x == y) {
          ans2 = mid;
        }
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    if (ans2 == -1 || ans1 == -1) {
      continue;
    } else {
      sum += ans2 - ans1 + 1;
    }
  }
  cout << sum << endl;
  return 0;
}
