#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define reg register
inline char gc() {
static const int bs = 1 << 22;
static unsigned char buf[bs], *st, *ed;
if (st == ed) ed = buf + fread(st = buf, 1, bs, stdin);
return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
int res=0;char ch=gc();bool fu=0;
while(!isdigit(ch))fu|=(ch=='-'), ch=gc();
while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=gc();
return fu?-res:res;
}
#define N 100005
int n;
int a[N], b[N];
int pos[N];
int c[N*9], cnt, tmp[10];
int low[N*9], ans;
int main()
{
n = read();
for (reg int i = 1 ; i <= n ; i ++) a[i] = read();
for (reg int i = 1 ; i <= n ; i ++) pos[b[i] = read()] = i;
for (reg int i = 1 ; i <= n ; i ++)
{
int top = 0;
for (reg int j = max(1, a[i] - 4) ; j <= min(n, a[i] + 4) ; j ++)
tmp[++top] = pos[j];
sort(tmp + 1, tmp + 1 + top);
for (reg int j = top ; j >= 1 ; j --) c[++cnt] = tmp[j];
}
low[++ans] = c[1];
for (reg int i = 2 ; i <= cnt ; i ++)
{
if (c[i] > low[ans]) low[++ans] = c[i];
else {
int t = lower_bound(low + 1, low + 1 + ans, c[i]) - low;
low[t] = c[i];
}
}
cout << ans << endl;
return 0;
}