#include <bits/stdc++.h>
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcount(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
#define EPS 1e-9
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = 2 * acos((ld) 0);
ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> void setmin(T& a, T val) {if (a > val) a = val;}
template<class T> void setmax(T& a, T val) {if (a < val) a = val;}
void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

const int maxn = 100010;
int n, m;
int a[maxn];
int b[maxn];
int l[maxn];
int r[maxn];
int f[maxn];
int g[maxn];

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 1, n + 1) {
		scanf("%d", a + i);
		l[i] = r[i] = i;
	}
	FOR(i, 0, m) scanf("%d", b + i);
	sort(a + 1, a + n + 1); sort(b, b + m);
	FOR(i, 2, n + 1) {
		if (a[i] == a[i - 1] + 1) {
			l[i] = l[i - 1];
		}
	}
	FOR(i, 1, n + 1) {
		int k = upper_bound(b, b + m, a[i]) - b;
		FOR(j, 0, k) if (i - a[i] + b[j] >= 0 && l[i - a[i] + b[j]]) {
			setmax(g[i], k - j + f[l[i - a[i] + b[j]] - 1]);
		}
		setmax(f[i], f[i - 1]);
		setmax(f[i], g[i]);
		setmax(g[i], g[i - 1]);
		setmax(g[i], f[i - 1]);
		FOR(j, k, m) {
			if (i + b[j] - a[i] <= n) {
				setmax(f[i + b[j] - a[i]], g[i] + j - k + 1);
			}
		}
	}
	printf("%d", f[n]);
}

int main() {
//	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL_
	freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);
#endif
	solve();
#ifdef _LOCAL_
	//printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int it = 1;

    for(int i = 1; i <= n; i++) {
        while(it <= m and b[it] < a[i].first)
            it++;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[i] = max(dp[i], (it - j) + dp[id]);
        }
        dp[i] = max(dp[i], dp[i - 1]);
        while(it <= m and b[it] <= a[i].second) {
            dp[i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or beq[id] == n + 1)
                break;
            id = beq[id];
            dp[id] = max(dp[id], (j - it + 1) + dp[i]);
        }
    }

    printf("%d\n", dp[n]);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;
const int M = 1000 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int ans = 0;

    for(int i = 1; i <= n; i++) {
        int it = lower_bound(b + 1, b + m + 1, a[i].first) - b;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        ans = max(ans, dp[0][i]);
        ans = max(ans, dp[1][i]);
    }

    printf("%d\n", ans);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int ans = 0, it = 1;

    for(int i = 1; i <= n; i++) {
        while(it <= n and b[it] < a[i].first)
            it++;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        dp[0][i] = max(dp[0][i], dp[1][i - 1]);
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        dp[1][i] = max(dp[1][i], dp[0][i]);
        dp[1][i] = max(dp[1][i], dp[1][i - 1]);
        ans = max(ans, dp[0][i]);
        ans = max(ans, dp[1][i]);
    }

    printf("%d\n", ans);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int it = 1;

    for(int i = 1; i <= n; i++) {
        while(it <= m and b[it] < a[i].first)
            it++;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        dp[0][i] = max(dp[0][i], dp[1][i - 1]);
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        dp[1][i] = max(dp[1][i], dp[0][i]);
    }

    printf("%d\n", dp[1][n]);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 3e5 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int ans = 0;

    for(int i = 1; i <= n; i++) {
        int it = lower_bound(b + 1, b + m + 1, a[i].first) - b;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        dp[1][i] = max(dp[1][i], dp[1][i - 1]);
        ans = max(ans, dp[0][i]);
        ans = max(ans, dp[1][i]);
    }

    printf("%d\n", ans);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;
const int M = 1000 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int ans = 0;

    for(int i = 1; i <= n; i++) {
        int it = lower_bound(b + 1, b + m + 1, a[i].first) - b;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        dp[1][i] = max(dp[1][i], dp[1][i - 1]);
        ans = max(ans, dp[0][i]);
        ans = max(ans, dp[1][i]);
    }

    printf("%d\n", ans);

    return 0;

}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 1e5 + 5;

int n, m, s[N], b[N], sum[N], seq[N], beq[N], dp[2][N];
ii a[N];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%d", s + i);

    for(int i = 1; i <= m; i++)
        scanf("%d", b + i);

    sort(s + 1, s + n + 1);
    sort(b + 1, b + m + 1);

    int sz = 0;

    for(int i = 1; i <= n; i++) {
        int j = i;
        while(j + 1 <= n and s[j + 1] - s[j] == 1)
            j++;
        a[++sz] = ii(s[i], s[j]);
        i = j;
    }

    n = sz;

    for(int i = 1; i <= n; i++)
        sum[i] = sum[i - 1] + (a[i].second - a[i].first + 1);

    int j = 0;

    for(int i = 0; i < N; i++) {
        while(j + 1 <= n and sum[j + 1] <= i)
            j++;
        seq[i] = j;
    }

    j = n + 1;

    for(int i = N - 1; i; i--) {
        while(j - 1 >= 1 and sum[j - 1] >= i)
            j--;
        beq[i] = j;
    }

    int ans = 0, it = 1;

    for(int i = 1; i <= n; i++) {
        while(it <= n and b[it] < a[i].first)
            it++;
        for(int j = it - 1; j >= 1; j--) {
            int id = sum[i - 1] - (a[i].first - b[j]);
            if(id < 0)
                break;
            id = seq[id];
            dp[0][i] = max(dp[0][i], (it - j) + dp[1][id]);
        }
        while(it <= m and b[it] <= a[i].second) {
            dp[0][i]++;
            it++;
        }
        for(int j = it; j <= m; j++) {
            int id = sum[i] + (b[j] - a[i].second);
            if(id >= N or   beq[id] == n + 1)
                break;
            id = beq[id];
            dp[1][id] = max(dp[1][id], (j - it + 1) + dp[0][i]);
        }
        dp[1][i] = max(dp[1][i], dp[0][i]);
        dp[1][i] = max(dp[1][i], dp[1][i - 1]);
        ans = max(ans, dp[0][i]);
        ans = max(ans, dp[1][i]);
    }

    printf("%d\n", ans);

    return 0;

}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long

const int N = 100100;
int n, m;
int a[N], b[N], pre[N], suf[N], f[N];

int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", a + i);
    for(int i = 1; i <= m; i++) scanf("%d", b + i);
    sort(a + 1, a + n + 1);
    sort(b + 1, b + m + 1);
    pre[1] = 1, suf[n] = n;
    for(int i = 2; i <= n; i++)
        if (a[i] == a[i - 1] + 1) pre[i] = pre[i - 1]; else pre[i] = i;
    for(int i = n - 1; i >= 1; i--)
        if (a[i] == a[i + 1] - 1) suf[i] = suf[i + 1]; else suf[i] = i;
    for(int i = 1, w; i <= n; i++){
        w = lower_bound(b + 1, b + m + 1, a[i]) - b;
        int best = f[i - 1];
        for(int j = w - 1; j; j--){
            int k = i - (a[i] - b[j]);
            if (k >= 1) best = max(best, f[pre[k]] + w - j); else break;
        }
        f[i] = max(f[i], best);
        for(int j = w; j <= m; j++){
            int k = i + (b[j] - a[i]);
            if (k <= n) f[suf[k]] = max(f[suf[k]], best + j - w - 1); else break;
        }
    }
    printf("%d\n", f[n]);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long

const int N = 100100;
int n, m;
int a[N], b[N], pre[N], suf[N], f[N];

int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", a + i);
    for(int i = 1; i <= m; i++) scanf("%d", b + i);
    sort(a + 1, a + n + 1);
    sort(b + 1, b + m + 1);
    pre[1] = 0, suf[n] = n;
    for(int i = 2; i <= n; i++)
        if (a[i] == a[i - 1] + 1) pre[i] = pre[i - 1]; else pre[i] = i - 1;
    for(int i = n - 1; i >= 1; i--)
        if (a[i] == a[i + 1] - 1) suf[i] = suf[i + 1]; else suf[i] = i;
    for(int i = 1, w; i <= n; i++){
        w = lower_bound(b + 1, b + m + 1, a[i]) - b;
        int best = f[i - 1];
        for(int j = w - 1; j; j--){
            int k = i - (a[i] - b[j]);
            if (k >= 1) best = max(best, f[pre[k]] + w - j); else break;
        }
        f[i] = max(f[i], best);
        for(int j = w; j <= m; j++){
            int k = i + (b[j] - a[i]);
            if (k <= n) f[suf[k]] = max(f[suf[k]], best + j - w + 1); else break;
        }
    }
    printf("%d\n", f[n]);
    return 0;
}


#include  <bits/stdc++.h>
#define  N  100100
using  namespace  std;

int  n,m,a[N],b[2010],f[N],pre[N],nex[N];

int  main(){
	scanf("%d%d",&n,&m);
	for  (int  i = 1;i <= n; i++)  scanf("%d",&a[i]);
	for  (int  i = 0;i < m; i++)  scanf("%d",&b[i]);
	sort(a+1,a+n+1);
	sort(b,b+m);
	pre[1] = 0;nex[n] = n;
	for  (int  i = 2;i <= n; i++)
		if  (a[i] == a[i-1]+1)  pre[i] = pre[i-1];
		else  pre[i] = i-1;
	for  (int  i = n-1; i; i--)
		if  (a[i] == a[i+1]-1)  nex[i] = nex[i+1];
		else  nex[i] = i;
	for  (int  i = 1;i <= n; i++){
		int  w = lower_bound(b,b+m,a[i]) - b;
		int  tmp = f[i-1];
		for  (int  j = w-1;j >= 0;j--){
			int  k = i-(a[i]-b[j]);
			if  (k < 1)  break;
			tmp = max(tmp,f[pre[k]]+w-j);
		}
		f[i] = max(f[i],tmp);
		for  (int  j = w;j < m; j++){
			int  k = i+b[j]-a[i];
			if  (k > n)  break;
			f[nex[k]] = max(f[nex[k]],tmp+j-w+1);
		}
	}
	printf("%d\n",f[n]);
	return  0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>

#define forit(it,S) for(__typeof(S.begin()) it = (S).begin(); it != (S).end(); it++)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)1e5 + 10;
const int maxm = 2005;

int a[maxn],p[maxm],L[maxn],R[maxn];
int d1[maxn],d2[maxn];

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    int n,m; cin >> n >> m;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a,a + n);

    L[0] = 0;

    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1] + 1) {
            L[i] = L[i - 1];
        } else {
            L[i] = i;
        }
    }

    R[n - 1] = n - 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] == a[i + 1] - 1) {
            R[i] = R[i + 1];
        } else {
            R[i] = i;
        }
    }

    for (int i = 0; i < m; i++) cin >> p[i];

    sort(p,p + m);

     for (int i = 0; i < n; i++) {

        if (i > 0) {
            d1[i] = max(d1[i - 1],d2[i - 1]);
            d2[i] = max(d2[i],d2[i - 1]);
          }

        int ptr = -1;

        while(ptr + 1 < m && p[ptr + 1] <= a[i]) ptr++;

        int j;

          for (j = 0; j < m && p[j] <= a[i]; j++) if (i >= a[i] - p[j]) {
            int lst = L[i - (a[i] - p[j])];
            lst = lst > 0 ? d2[lst - 1] : 0;
            d1[i] = max(d1[i],ptr - j + 1 + lst);
          }

          d2[i] = max(d2[i],d1[i]);

          for (; j < m && p[j] > a[i]; j++) {
            if (n - i < p[j] - a[i] + 1) break;
            int lst = R[i + p[j] - a[i]];
            d2[lst] = max(d2[lst],d1[i] + j - ptr);
          }

     }

     cout << d2[n - 1] << endl;

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>

#define forit(it,S) for(__typeof(S.begin()) it = (S).begin(); it != (S).end(); it++)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)1e5 + 10;
const int maxm = 2005;

int a[maxn],p[maxm],L[maxn],R[maxn];
int d1[maxn],d2[maxn];

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    int n,m; cin >> n >> m;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a,a + n);

    L[0] = 0;
    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1] + 1) {
            L[i] = L[i - 1];
        } else {
            L[i] = i;
        }
    }

    R[n - 1] = n - 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] == a[i + 1] - 1) {
            R[i] = R[i + 1];
        } else {
            R[i] = i;
        }
    }

    for (int i = 0; i < m; i++) cin >> p[i];
    sort(p,p + m);

    int ptr = -1;

     for (int i = 0; i < n; i++) {

        if (i > 0) {
            d1[i] = d1[i - 1];
            d2[i] = max(d2[i],d2[i - 1]);
          }

        int ptr = -1;

        while(ptr + 1 < m && p[ptr + 1] <= a[i]) ptr++;

        int j;

          for (j = 0; j < m && p[j] <= a[i]; j++) if (i >= a[i] - p[j]) {
            int lst = L[i - (a[i] - p[j])];
            lst = lst > 0 ? d2[lst - 1] : 0;
            d1[i] = max(d1[i],ptr - j + 1 + lst);
          }

          d2[i] = max(d2[i],d1[i]);

          for (; j < m && p[j] > a[i]; j++) {
            if (n - i < p[j] - a[i] + 1) break;
            int lst = R[i + p[j] - a[i]];
            d2[lst] = max(d2[lst],d1[i] + j - ptr);
          }

     }

     cout << d2[n - 1] << endl;

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>

#define forit(it,S) for(__typeof(S.begin()) it = (S).begin(); it != (S).end(); it++)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);

const int maxn = (int)1e5 + 10;
const int maxm = 2005;

int a[maxn],p[maxm],L[maxn],R[maxn];
int d1[maxn],d2[maxn];

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    int n,m; cin >> n >> m;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a,a + n);

    L[0] = 0;

    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1] + 1) {
            L[i] = L[i - 1];
        } else {
            L[i] = i;
        }
    }

    R[n - 1] = n - 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] == a[i + 1] - 1) {
            R[i] = R[i + 1];
        } else {
            R[i] = i;
        }
    }

    for (int i = 0; i < m; i++) cin >> p[i];

    sort(p,p + m);

     for (int i = 0; i < n; i++) {

        if (i > 0) {
            d1[i] = d2[i - 1];
            d2[i] = max(d2[i],d2[i - 1]);
          }

        int ptr = -1;

        while(ptr + 1 < m && p[ptr + 1] <= a[i]) ptr++;

        int j;

          for (j = 0; j < m && p[j] <= a[i]; j++) if (i >= a[i] - p[j]) {
            int lst = L[i - (a[i] - p[j])];
            lst = lst > 0 ? d2[lst - 1] : 0;
            d1[i] = max(d1[i],ptr - j + 1 + lst);
          }

          d2[i] = max(d2[i],d1[i]);

          for (; j < m && p[j] > a[i]; j++) {
            if (n - i < p[j] - a[i] + 1) break;
            int lst = R[i + p[j] - a[i]];
            d2[lst] = max(d2[lst],d1[i] + j - ptr);
          }

     }

     cout << d2[n - 1] << endl;

    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = star[a[i]] - star[a[i]-i];
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else{
                tam = star[b[j]] - star[a[i]];
                k  = b[j] - a[i];
                if (i+k <=n && tam + d[i] >v[i+k])
                    v[i+k] = tam+d[i];
            }
        v[i] = max(v[i],d[i]);
        v[i] = max(v[i],v[i-1]);
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
	int fi;
	int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
	//freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&m);
	int u;
	for(int i=1; i<=n; i++){
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=m; i++){
		scanf("%d",&b[i]);
	}

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	b[0]  =0;
	b[m+1] = N-1;

	int k = 1;
	tt[1] = 1;
	c[1].fi=1;
	c[1].la=1;
	for(int i=2; i<=n; i++){
		if (a[i]==a[i-1]+1){
			tt[i] = k;
			c[k].la = i;
		}else{
			tt[i] = ++k;
			c[k].fi = i;
			c[k].la = i;
		}
	}
	bn = k;

	k = 1;
	for(int i=1; i<=m; i++){
		while (k<bn && a[c[k+1].la]<b[i]) k++;
		if (a[c[k].la]<b[i]) pre[i] = k;
			else pre[i] = 0;
	}

	star[0] = 0;
	star[m+1] = N-1;
	int tam = 0 ;
	for(int i = 1; i<=m+1; i++){
		for(int j=b[i-1]; j<b[i]; j++)
			star[j] = tam;
		tam++;
	}
	v[0] = 0;
	d[0] = 0;
	int sum = 0;
	for(int i=1; i<=n; i++){
		d[i] = star[a[i]] - star[a[i]-i];
		for(int j=1; j<=m; j++)
			if (b[j]<=a[i]){
				tam = star[a[i]] - star[b[j]-1];
				k = a[i]-b[j]+1;
				if (k<=i && tam+v[c[tt[i-k]].fi-1]>d[i])
					d[i] = tam+v[c[tt[i-k]].fi-1];
			}else break;
		v[i] = d[i];
		for(int j=1; j<=m; j++)
			if (pre[j]!=0){
				tam = star[b[j]] - star[a[c[pre[j]].la]];
				k = b[j] - a[c[pre[j]].la];
				if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
					v[i] = tam+d[c[pre[j]].la];
			}
	}
	//cout<<v[4];
	cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int u;
    for(int i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    int sum = 0;
    for(int i=1; i<=n; i++){
        d[i] = star[a[i]] - star[a[i]-i];
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k]-1].la]>d[i])
                    d[i] = v[c[tt[i-k]-1].la];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else{
                tam = star[b[j]] - star[a[i]];
                k  = b[j] - a[i];
                if (i+k <=n && tam + d[i] >v[i+k])
                    v[i+k] = tam+d[i];
            }
        v[i] = max(v[i],d[i]);
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
	int fi;
	int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
	//freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&m);
	int u;
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=m; i++){
		scanf("%d",&b[i]);
	}

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	b[0]  =0;
	b[m+1] = N-1;

	int k = 1;
	tt[1] = 1;
	c[1].fi=1;
	c[1].la=1;
	for(int i=2; i<=n; i++){
		if (a[i]==a[i-1]+1){
			tt[i] = k;
			c[k].la = i;
		}else{
			tt[i] = ++k;
			c[k].fi = i;
			c[k].la = i;
		}
	}
	bn = k;

	k = 1;
	for(int i=1; i<=m; i++){
		while (k<bn && a[c[k+1].la]<b[i]) k++;
		if (a[c[k].la]<b[i]) pre[i] = k;
			else pre[i] = 0;
	}

	star[0] = 0;
	star[m+1] = N-1;
	int tam = 0 ;
	for(int i = 1; i<=m+1; i++){
		for(int j=b[i-1]; j<b[i]; j++)
			star[j] = tam;
		tam++;
	}
	v[0] = 0;
	d[0] = 0;
	int sum = 0;
	for(int i=1; i<=n; i++){
		d[i] = star[a[i]] - star[a[i]-i];
		for(int j=1; j<=m; j++)
			if (b[j]<=a[i]){
				tam = star[a[i]] - star[b[j]-1];
				k = a[i]-b[j]+1;
				if (k<=i && tam+v[c[tt[i-k]].fi-1]>d[i])
					d[i] = tam+v[c[tt[i-k]].fi-1];
			}else break;
		v[i] = d[i];
		for(int j=1; j<=m; j++)
			if (pre[j]!=0){
				tam = star[b[j]] - star[a[c[pre[j]].la]];
				k = b[j] - a[c[pre[j]].la];
				if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
					v[i] = tam+d[c[pre[j]].la];
			}
	}
	//cout<<v[4];
	cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
	int fi;
	int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
	//freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&m);
	int u;
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=m; i++){
		scanf("%d",&b[i]);
	}

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	b[0]  =0;
	b[m+1] = N-1;

	int k = 1;
	tt[1] = 1;
	c[1].fi=1;
	c[1].la=1;
	for(int i=2; i<=n; i++){
		if (a[i]==a[i-1]+1){
			tt[i] = k;
			c[k].la = i;
		}else{
			tt[i] = ++k;
			c[k].fi = i;
			c[k].la = i;
		}
	}
	bn = k;

	k = 1;
	for(int i=1; i<=m; i++){
		while (k<bn && a[c[k+1].la]<=b[i]) k++;
		if (a[c[k].la]<b[i]) pre[i] = k;
			else pre[i] = 0;
	}

	star[0] = 0;
	int tam = 0 ;
	for(int i = 1; i<=m+1; i++){
		for(int j=b[i-1]; j<b[i]; j++)
			star[j] = tam;
		tam++;
	}
	v[0] = 0;
	d[0] = 0;
	int sum = 0;
	for(int i=1; i<=n; i++){
		d[i] = star[a[i]] - star[a[i]-i];
		for(int j=1; j<=m; j++)
			if (b[j]<=a[i]){
				tam = star[a[i]] - star[b[j]-1];
				k = a[i]-b[j]+1;
				if (k<=i && tam+v[c[tt[i-k]-1].la]>d[i])
					d[i] = v[c[tt[i-k]-1].la];
			}else break;
		v[i] = d[i];
		for(int j=1; j<=m; j++)
			if (pre[j]!=0){
				tam = star[b[j]] - star[c[pre[j]].la];
				k = b[j] - a[c[pre[j]].la];
				if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
					v[i] = tam+d[c[pre[j]].la];
			}
	}
	//cout<<v[4];
	cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;

int main(){
	//freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&m);
	int u;
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=m; i++){
		scanf("%d",&b[i]);
	}

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	b[0]  =0;
	b[m+1] = N-1;

	int k = 1;
	for(int i=1; i<=m; i++){
		while (k<n && a[k+1]<=b[i]) k++;
		if (a[k]<=b[i]) pre[i] = k;
			else pre[i] = 0;
	}

	star[0] = 0;
	int tam = 0 ;
	for(int i = 1; i<=m+1; i++){
		for(int j=b[i-1]; j<b[i]; j++)
			star[j] = tam;
		tam++;
	}
	if (star[a[1]]-star[a[1]-1]==1) d[1] = 1;
		else d[1] = 0;
	v[1] = d[1];
	for(int i=2; i<=n; i++){
		d[i] = star[a[i]]-star[a[i]-i];
		for(int j=1; j<=m; j++)
			if (b[j]<=a[i]){
				tam = star[a[i]]-star[b[j]-1];
				k = a[i]-b[j]+1;
				if (k<=i && tam+v[i-k]>d[i])
					d[i] = tam+v[i-k];
			}else break;
		v[i] = d[i];
		for(int j=1; j<=m; j++)
			if (pre[j]!=0){
				tam = star[b[j]] - star[a[pre[j]]];
				k = b[j] - a[pre[j]];
				if (k<=i-pre[j] && tam + v[pre[j]]>v[i])
					v[i] = tam+v[pre[j]];
			}
	}
	cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=i-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[c[tt[i]-1].la]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else{
                tam = star[b[j]] - star[a[i]];
                k  = b[j] - a[i];
                if (i+k <=n && tam + d[i] >v[i+k])
                    v[i+k] = tam+d[i];
            }
        v[i] = max(v[i],d[i]);
        v[i] = max(v[i],v[i-1]);
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    int sum = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int u;
    for(int i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    star[m+1] = N-1;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    int sum = 0;
    for(int i=1; i<=n; i++){
        d[i] = star[a[i]] - star[a[i]-i];
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k]-1].la]>d[i])
                    d[i] = v[c[tt[i-k]-1].la];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int po[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int u;
    for(int i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<=b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    int sum = 0;
    for(int i=1; i<=n; i++){
        d[i] = star[a[i]] - star[a[i]-i];
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k]-1].la]>d[i])
                    d[i] = v[c[tt[i-k]-1].la];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=c[tt[i]].la-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else break;
        v[i] = d[i];
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[a[pre[j]]+i-pre[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=i-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = star[a[i]] - star[a[i]-i];
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else break;
        v[i] = max(d[i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (pre[j]!=0){
                tam = star[b[j]] - star[a[c[pre[j]].la]];
                k = b[j] - a[c[pre[j]].la];
                if (k<=i-c[pre[j]].la && tam + d[c[pre[j]].la]>v[i])
                    v[i] = tam+d[c[pre[j]].la];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int M = 2001;
const int N = 2e5 + 2;
typedef vector <int> vi;

struct block{
    int fi;
    int la;
};

block c[N];
int tt[N];
int a[N],b[N];
int pre[N];
int d[N];
int v[N];
int star[N];
int n, m;
int bn;

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    b[0]  =0;
    b[m+1] = N-1;

    c[0].fi = 0;
    c[0].la = 0;
    int k = 1;
    tt[1] = 1;
    c[1].fi=1;
    c[1].la=1;
    for(int i=2; i<=n; i++){
        if (a[i]==a[i-1]+1){
            tt[i] = k;
            c[k].la = i;
        }else{
            tt[i] = ++k;
            c[k].fi = i;
            c[k].la = i;
        }
    }
    bn = k;

    k = 1;
    for(int i=1; i<=m; i++){
        while (k<bn && a[c[k+1].la]<b[i]) k++;
        if (a[c[k].la]<b[i]) pre[i] = k;
            else pre[i] = 0;
    }

    star[0] = 0;
    int tam = 0 ;
    for(int i = 1; i<=m+1; i++){
        for(int j=b[i-1]; j<b[i]; j++)
            star[j] = tam;
        tam++;
    }
    v[0] = 0;
    d[0] = 0;
    for(int i=1; i<=n; i++){
        d[i] = max(star[a[i]] - star[a[i]-i],v[i-1]);
        for(int j=1; j<=m; j++)
            if (b[j]<=a[i]){
                tam = star[a[i]] - star[b[j]-1];
                k = a[i]-b[j]+1;
                if (k<=i && tam+v[c[tt[i-k+1]].fi-1]>d[i])
                    d[i] = tam+v[c[tt[i-k+1]].fi-1];
            }else{
                tam = star[b[j]] - star[a[i]];
                k  = b[j] - a[i];
                if (i+k <=n && tam + d[i] >v[i+k])
                    v[i+k] = tam+d[i];
            }
    }
    //cout<<v[4];
    cout<<v[n];
}


#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;

const int maxn=200010;

int dp[maxn],s[maxn],pre[maxn],nxt[maxn];
int dp1[maxn];

int vis[maxn];

int lft[maxn],rht[maxn],cnt;

int pos[maxn];

int qian[maxn],hou[maxn],tot[maxn];

int main()
{
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);

	for(i=1;i<=n;i++)
		scanf("%d",&j),vis[j]=1;

	for(i=1;i<=200000;i++)if(vis[i])
	{
		for(j=i;vis[j];j++);
		++cnt;
		lft[cnt]=i;
		rht[cnt]=j-1;
		i=j;
	}

	for(i=1;i<=cnt;i++)
		tot[i]=rht[i]-lft[i]+1+tot[i-1];

	for(i=1;i<=m;i++)
		scanf("%d",&pos[i]);
	sort(pos+1,pos+1+m);

	for(i=1;i<=m;i++)
	{
		pre[pos[i]]=nxt[pos[i]]=i;
		s[pos[i]]=1;
	}

	for(i=1;i<=200000;i++)
	{
		s[i]+=s[i-1];
		if(!pre[i])
			pre[i]=pre[i-1];
	}
	for(i=200000;i>=1;i--)if(!nxt[i])
		nxt[i]=nxt[i+1];

	for(i=1;i<=m;i++)
		qian[i]=cnt,hou[i]=cnt;

	for(i=1;i<=cnt;i++)
	{
		dp[i]=s[rht[i]]-s[lft[i]-1]+dp1[i-1];

		for(j=pre[lft[i]-1];j;j--)
		{
			while(qian[j]&&tot[i-1]-tot[qian[j]-1]<lft[i]-pos[j])
				qian[j]--;
			if(qian[j])
				dp[i]=max(dp[i],s[rht[i]]-s[lft[i]-1]+pre[lft[i]-1]-j+1+dp1[qian[j]-1]);
			else
				break;
		}

		for(j=nxt[rht[i]+1];j&&j<=m;j++)
		{
			while(tot[hou[j]-1]-tot[i]>=pos[j]-rht[i])
				hou[j]--;
			if(tot[hou[j]]-tot[i]>=pos[j]-rht[i])
				dp1[hou[j]]=max(dp1[hou[j]],dp[i]+j-nxt[rht[i]+1]+1);
			else
				break;
		}
		dp1[i]=max(dp1[i],dp[i]);
	}
	printf("%d\n",dp1[cnt]);
}


#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;

const int maxn=200010;

int dp[maxn],s[maxn],pre[maxn],nxt[maxn];
int dp1[maxn];

int vis[maxn];

int lft[maxn],rht[maxn],cnt;

int pos[maxn];

int main()
{
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);

	for(i=1;i<=n;i++)
		scanf("%d",&j),vis[j]=1;

	for(i=1;i<=200000;i++)if(vis[i])
	{
		for(j=i;vis[j];j++);
		++cnt;
		lft[cnt]=i;
		rht[cnt]=j-1;
		i=j;
	}

	for(i=1;i<=m;i++)
		scanf("%d",&pos[i]);
	sort(pos+1,pos+1+m);

	for(i=1;i<=m;i++)
	{
		pre[pos[i]]=nxt[pos[i]]=i;
		s[pos[i]]=1;
	}

	for(i=1;i<=200000;i++)
	{
		s[i]+=s[i-1];
		if(!pre[i])
			pre[i]=pre[i-1];
	}
	for(i=200000;i>=1;i--)if(!nxt[i])
		nxt[i]=nxt[i+1];

	for(i=1;i<=cnt;i++)
	{
		dp[i]=s[rht[i]]-s[lft[i]-1]+dp1[i-1];

		int cur=0;
		k=i-1;
		for(j=pre[lft[i]-1];j;j--)
		{
			while(k&&cur<lft[i]-pos[j])
				cur+=rht[k]-lft[k]+1,k--;
			if(cur>=lft[i]-pos[j])
				dp[i]=max(dp[i],s[rht[i]]-s[lft[i]-1]+pre[lft[i]-1]-j+1+dp1[k]);
			else
				break;
		}

		cur=0;
		k=i+1;
		for(j=nxt[rht[i]+1];j&&j<=m;j++)
		{
			while(k<=cnt&&cur<pos[j]-rht[i])
				cur+=rht[k]-lft[k]+1,k++;
			if(cur>=pos[j]-rht[i])
				dp1[k-1]=max(dp1[k-1],dp[i]+j-nxt[rht[i]+1]+1);
			else
				break;
		}
		dp1[i]=max(dp1[i],dp[i]);
	}
	printf("%d\n",dp1[cnt]);
}


#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;

const int maxn=200010;

int dp[maxn],s[maxn],pre[maxn],nxt[maxn];

int vis[maxn];

int lft[maxn],rht[maxn],cnt;

int pos[maxn];

int main()
{
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);

	for(i=1;i<=n;i++)
		scanf("%d",&j),vis[j]=1;

	for(i=1;i<=200000;i++)if(vis[i])
	{
		for(j=i;vis[j];j++);
		++cnt;
		lft[cnt]=i;
		rht[cnt]=j-1;
		i=j;
	}

	for(i=1;i<=m;i++)
		scanf("%d",&pos[i]);
	sort(pos+1,pos+1+m);

	for(i=1;i<=m;i++)
	{
		pre[pos[i]]=nxt[pos[i]]=i;
		s[pos[i]]=1;
	}

	for(i=1;i<=200000;i++)
	{
		s[i]+=s[i-1];
		if(!pre[i])
			pre[i]=pre[i-1];
	}
	for(i=200000;i>=1;i--)if(!nxt[i])
		nxt[i]=nxt[i+1];

	for(i=1;i<=cnt;i++)
	{
		dp[i]=max(dp[i],s[rht[i]]-s[lft[i]-1]+dp[i-1]);

		int cur=0;
		k=i-1;
		for(j=pre[lft[i]-1];j;j--)
		{
			while(k&&cur<lft[i]-pos[j])
				cur+=rht[k]-lft[k]+1,k--;
			if(cur>=lft[i]-pos[j])
				dp[i]=max(dp[i],s[rht[i]]-s[lft[i]-1]+pre[lft[i]-1]-j+1+dp[k]);
			else
				break;
		}

		cur=0;
		k=i+1;
		for(j=nxt[rht[i]+1];j&&j<=m;j++)
		{
			while(k<=cnt&&cur<pos[j]-rht[i])
				cur+=rht[k]-lft[k]+1,k++;
			if(cur>=pos[j]-rht[i])
				dp[k-1]=max(dp[k-1],dp[i]+j-nxt[rht[i]+1]+1);
			else
				break;
		}
	}
	printf("%d\n",dp[cnt]);
}


#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = int(l), __board_right##i = int(r); i < __board_right##i; i++)
#define STRUCT3(v1, v2, v3, name) struct name \
		{   \
			int v1, v2, v3; \
			name(int v1 = 0, int v2 = 0, int v3 = 0) : v1(v1), v2(v2), v3(v3) {} \
			friend bool operator < (const name& athis, const name& other) \
			{   \
				if (athis.v1 != other.v1) return athis.v1 < other.v1;   \
				if (athis.v2 != other.v2) return athis.v2 < other.v2;   \
				return athis.v3 < other.v3;   \
			}\
		}
#define PB push_back
#define LS (((k) << 1) + 1)
#define RS (((k) << 1) + 2)
#define LM ((l) + (r) >> 1)
#define RM (LM + 1)
#define FST first
#define SCD second
#define retunr return
#define modp 1000000007
#define EPS 1e-7
#define INF 0x3f3f3f3f
#define MAX2 113
#define MAX3 1013
#define MAX4 10013
#define MAX5 100013
#define MAX6 1000013
#define MAXN
#define MANX MAXN
using namespace std;
typedef long long LL;
typedef unsigned int UI;
// typedef pair<int, int> P;

const int B = 200001;

int N, M, K;
int locP[MAX5], locB[MAX5 * 2], locC[MAX3 * 2];
int S1[MAX5 * 2], S2[MAX5 * 2], S3[MAX5 * 2];
int pre[MAX5];
int sol[MAX5], act[MAX5];
void init()
{
    int k1 = 1, k2 = 1, k3 = 1;
    FOR(i, 1, N) {
        FOR(j, locP[i] + 1, locP[i + 1]) {
            locB[++K] = j;
        }
    }
    FOR(i, 1, B) {
        S1[i] = S1[i - 1];
        S2[i] = S2[i - 1];
        S3[i] = S3[i - 1];
        if (i == locP[k1]) {
            k1++;
            S1[i]++;
        }
        else if (i == locB[k2]) {
            k2++;
            S2[i]++;
        }
        if (i == locC[k3]) {
            k3++;
            S3[i]++;
        }
    }
}

int main()
{
    scanf("%d%d", &N, &M);
    FOR(i, 1, N + 1) {
        scanf("%d", locP + i);
    }
    sort(locP + 1, locP + N + 1);
    pre[1] = 1;
    FOR(i, 2, N + 1) {
        pre[i] = i;
        if (locP[i] == locP[i - 1] + 1) {
            pre[i] = pre[i - 1];
        }
    }
    FOR(i, 1, M + 1) {
        scanf("%d", locC + i);
    }
    sort(locC + 1, locC + M + 1);
    init();
    FOR(i, 1, N + 1) {
        sol[i] = pre[i] < i ? sol[i - 1] : act[i - 1];
        int x = S3[locP[i]];
        FOR(j, 1, x + 1) {
            int t = locP[i] - locC[j];
            if (t < i) {
                t = pre[i - t] - 1;
                sol[i] = max(sol[i], act[t] + x - j + 1);
            }
            int n = S1[locP[i] - 1] - S1[locC[j] - 1];
            if (n <= S2[locC[j] - 1]) {
                n = locB[S2[locC[j] - 1] - n + 1];
                if (locP[S1[n]] == n - 1) {
                    act[i] = max(act[i], sol[S1[n]] + j - S3[n - 1]);
                }
            }
        }
        act[i] = max(act[i], sol[i]);
    }
    printf("%d\n", act[N]);
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = int(l), __board_right##i = int(r); i < __board_right##i; i++)
#define STRUCT3(v1, v2, v3, name) struct name \
		{   \
			int v1, v2, v3; \
			name(int v1 = 0, int v2 = 0, int v3 = 0) : v1(v1), v2(v2), v3(v3) {} \
			friend bool operator < (const name& athis, const name& other) \
			{   \
				if (athis.v1 != other.v1) return athis.v1 < other.v1;   \
				if (athis.v2 != other.v2) return athis.v2 < other.v2;   \
				return athis.v3 < other.v3;   \
			}\
		}
#define PB push_back
#define LS (((k) << 1) + 1)
#define RS (((k) << 1) + 2)
#define LM ((l) + (r) >> 1)
#define RM (LM + 1)
#define FST first
#define SCD second
#define retunr return
#define modp 1000000007
#define EPS 1e-7
#define INF 0x3f3f3f3f
#define MAX2 113
#define MAX3 1013
#define MAX4 10013
#define MAX5 100013
#define MAX6 1000013
#define MAXN
#define MANX MAXN
using namespace std;
typedef long long LL;
typedef unsigned int UI;
// typedef pair<int, int> P;

const int B = 200001;

int N, M, K;
int locP[MAX5], locB[MAX5 * 2], locC[MAX3 * 2];
int S1[MAX5 * 2], S2[MAX5 * 2], S3[MAX5 * 2];
int pre[MAX5];
int sol[MAX5], act[MAX5];
void init()
{
    int k1 = 1, k2 = 1, k3 = 1;
    FOR(i, 1, N) {
        FOR(j, locP[i] + 1, locP[i + 1]) {
            locB[++K] = j;
        }
    }
    FOR(i, 1, B) {
        S1[i] = S1[i - 1];
        S2[i] = S2[i - 1];
        S3[i] = S3[i - 1];
        if (i == locP[k1]) {
            k1++;
            S1[i]++;
        }
        else if (i == locB[k2]) {
            k2++;
            S2[i]++;
        }
        if (i == locC[k3]) {
            k3++;
            S3[i]++;
        }
    }
}

int main()
{
    scanf("%d%d", &N, &M);
    FOR(i, 1, N + 1) {
        scanf("%d", locP + i);
    }
    sort(locP + 1, locP + N + 1);
    pre[1] = 1;
    FOR(i, 2, N + 1) {
        pre[i] = i;
        if (locP[i] == locP[i - 1] + 1) {
            pre[i] = pre[i - 1];
        }
    }
    FOR(i, 1, M + 1) {
        scanf("%d", locC + i);
    }
    sort(locC + 1, locC + M + 1);
    init();
    FOR(i, 1, N + 1) {
        int x = S3[locP[i]];
        FOR(j, 1, x + 1) {
            int t = locP[i] - locC[j];
            if (t < i) {
                t = pre[i - t] - 1;
                sol[i] = max(sol[i], act[t] + x - j + 1);
            }
            int n = S1[locP[i] - 1] - S1[locC[j] - 1];
            if (n <= S2[locC[j] - 1]) {
                n = locB[S2[locC[j] - 1] - n + 1];
                if (locP[S1[n]] == n - 1) {
                    act[i] = max(act[i], sol[S1[n]] + j - S3[n - 1]);
                }
            }
        }
        act[i] = max(act[i], sol[i]);
    }
    printf("%d\n", act[N]);
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = int(l), __board_right##i = int(r); i < __board_right##i; i++)
#define STRUCT3(v1, v2, v3, name) struct name \
		{   \
			int v1, v2, v3; \
			name(int v1 = 0, int v2 = 0, int v3 = 0) : v1(v1), v2(v2), v3(v3) {} \
			friend bool operator < (const name& athis, const name& other) \
			{   \
				if (athis.v1 != other.v1) return athis.v1 < other.v1;   \
				if (athis.v2 != other.v2) return athis.v2 < other.v2;   \
				return athis.v3 < other.v3;   \
			}\
		}
#define PB push_back
#define LS (((k) << 1) + 1)
#define RS (((k) << 1) + 2)
#define LM ((l) + (r) >> 1)
#define RM (LM + 1)
#define FST first
#define SCD second
#define retunr return
#define modp 1000000007
#define EPS 1e-7
#define INF 0x3f3f3f3f
#define MAX2 113
#define MAX3 1013
#define MAX4 10013
#define MAX5 100013
#define MAX6 1000013
#define MAXN
#define MANX MAXN
using namespace std;
typedef long long LL;
typedef unsigned int UI;
// typedef pair<int, int> P;

const int B = 200001;

int N, M, K;
int locP[MAX5], locB[MAX5 * 2], locC[MAX3 * 2];
int S1[MAX5 * 2], S2[MAX5 * 2], S3[MAX5 * 2];
int pre[MAX5];
int sol[MAX5], act[MAX5];
void init()
{
    int k1 = 1, k2 = 1, k3 = 1;
    FOR(i, 1, N) {
        FOR(j, locP[i] + 1, locP[i + 1]) {
            locB[++K] = j;
        }
    }
    FOR(i, 1, B) {
        S1[i] = S1[i - 1];
        S2[i] = S2[i - 1];
        S3[i] = S3[i - 1];
        if (i == locP[k1]) {
            k1++;
            S1[i]++;
        }
        else if (i == locB[k2]) {
            k2++;
            S2[i]++;
        }
        if (i == locC[k3]) {
            k3++;
            S3[i]++;
        }
    }
}

int main()
{
    scanf("%d%d", &N, &M);
    FOR(i, 1, N + 1) {
        scanf("%d", locP + i);
    }
    sort(locP + 1, locP + N + 1);
    pre[1] = 1;
    FOR(i, 2, N + 1) {
        pre[i] = i;
        if (locP[i] == locP[i - 1] + 1) {
            pre[i] = pre[i - 1];
        }
    }
    FOR(i, 1, M + 1) {
        scanf("%d", locC + i);
    }
    sort(locC + 1, locC + M + 1);
    init();
    FOR(i, 1, N + 1) {
        sol[i] = act[i - 1];
        int x = S3[locP[i]];
        FOR(j, 1, x + 1) {
            int t = locP[i] - locC[j];
            if (t < i) {
                t = pre[i - t] - 1;
                sol[i] = max(sol[i], act[t] + x - j + 1);
            }
            int n = S1[locP[i] - 1] - S1[locC[j] - 1];
            if (n <= S2[locC[j] - 1]) {
                n = locB[S2[locC[j] - 1] - n + 1];
                if (locP[S1[n]] == n - 1) {
                    act[i] = max(act[i], sol[S1[n]] + j - S3[n - 1]);
                }
            }
        }
        act[i] = max(act[i], sol[i]);
    }
    printf("%d\n", act[N]);
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = int(l), __board_right##i = int(r); i < __board_right##i; i++)
#define STRUCT3(v1, v2, v3, name) struct name \
		{   \
			int v1, v2, v3; \
			name(int v1 = 0, int v2 = 0, int v3 = 0) : v1(v1), v2(v2), v3(v3) {} \
			friend bool operator < (const name& athis, const name& other) \
			{   \
				if (athis.v1 != other.v1) return athis.v1 < other.v1;   \
				if (athis.v2 != other.v2) return athis.v2 < other.v2;   \
				return athis.v3 < other.v3;   \
			}\
		}
#define PB push_back
#define LS (((k) << 1) + 1)
#define RS (((k) << 1) + 2)
#define LM ((l) + (r) >> 1)
#define RM (LM + 1)
#define FST first
#define SCD second
#define retunr return
#define modp 1000000007
#define EPS 1e-7
#define INF 0x3f3f3f3f
#define MAX2 113
#define MAX3 1013
#define MAX4 10013
#define MAX5 100013
#define MAX6 1000013
#define MAXN
#define MANX MAXN
using namespace std;
typedef long long LL;
typedef unsigned int UI;
// typedef pair<int, int> P;

const int B = 200001;

int N, M;
int locP[MAX5], locB[MAX5 * 2], locC[MAX3 * 2];
int S1[MAX5 * 2], S2[MAX5 * 2], S3[MAX5 * 2];
int pre[MAX5];
int sol[MAX5], act[MAX5];
int dp[MAX3][MAX3];
void init()
{
    int k1 = 1, k2 = 1, k3 = 1;
    int K = 0;
    FOR(i, 1, N) {
        FOR(j, locP[i] + 1, locP[i + 1]) {
            locB[++K] = j;
        }
    }
    FOR(i, 1, B) {
        S1[i] = S1[i - 1];
        S2[i] = S2[i - 1];
        S3[i] = S3[i - 1];
        if (i == locP[k1]) {
            k1++;
            S1[i]++;
        }
        else if (i == locB[k2]) {
            k2++;
            S2[i]++;
        }
        if (i == locC[k3]) {
            k3++;
            S3[i]++;
        }
    }
}

int main()
{
//    int ti = 0;
//    while (1) {
//        ti ++;
//        N = rand() % 10 + 1;
//        M = rand() % 5;
//        bool mark[MAX3];
//        memset(mark, 0, sizeof(mark));
//        FOR(i, 1, N + 1) {
//            locP[i] = rand() % 100 + 1;
//            if (mark[locP[i]]) i--;
//            else mark[locP[i]] = true;
//        }
//        memset(mark, 0, sizeof(mark));
//        FOR(i, 1, M + 1) {
//            locC[i] = rand() % 100 + 1;
//            if (mark[locC[i]]) i--;
//            else mark[locC[i]] = true;
//        }

    scanf("%d%d", &N, &M);
    FOR(i, 1, N + 1) {
        scanf("%d", locP + i);
    }
    FOR(i, 1, M + 1) {
        scanf("%d", locC + i);
    }
    sort(locP + 1, locP + N + 1);
    pre[1] = 1;
    FOR(i, 2, N + 1) {
        pre[i] = i;
        if (locP[i] == locP[i - 1] + 1) {
            pre[i] = pre[i - 1];
        }
    }
    sort(locC + 1, locC + M + 1);
    init();
//    if (ti == 633) {
//        int as = 1;
//    }
    FOR(i, 1, N + 1) {
        sol[i] = pre[i] < i ? sol[i - 1] : act[i - 1];
        act[i] = act[i - 1];
        int x = S3[locP[i]];
        FOR(j, 1, x + 1) {
            int t = locP[i] - locC[j];
            if (t < i) {
                t = pre[i - t] - 1;
                sol[i] = max(sol[i], act[t] + x - j + 1);
            }
            int n = S1[locP[i] - 1] - S1[locC[j] - 1];
            if (n <= S2[locC[j] - 1]) {
                n = locB[S2[locC[j] - 1] - n + 1];
                if (locP[S1[n]] == n - 1) {
                    act[i] = max(act[i], sol[S1[n]] + j - S3[n - 1]);
                }
            }
        }
        act[i] = max(act[i], sol[i]);
    }
//    int tmp[MAX3];
//    memset(tmp, 0, sizeof(tmp));
//    FOR(i, 1, N + 1) {
//        dp[i][i] = S3[locP[i]] - S3[locP[i] - i];
//        FOR(j, 1, i) {
//            dp[i][j] = dp[i - 1][j] + S3[locP[j] + i - j] - S3[locP[j] + i - j - 1];
//            if (pre[j + 1] == j + 1) dp[i][i] = max(dp[i][i], tmp[j] + S3[locP[i]] - S3[locP[i] - i + j]);
//            tmp[i] = max(tmp[i], dp[i][j]);
//        }
//        tmp[i] = max(tmp[i], dp[i][i]);
//    }
//    if (act[N] != tmp[N] && M != 0) {
//        printf("ti:%d !!!!!!!!%d %d\n", ti, act[N], tmp[N]);
//        printf("%d %d\n", N, M);
//        FOR(i, 1, N + 1) {
//            printf("%d ", locP[i]);
//        }
//        printf("\n");
//        FOR(i, 1, M + 1) {
//            printf("%d ", locC[i]);
//        }
//        printf("\n");
//        while (1);
//    }
    printf("%d\n", act[N]);
//    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 300010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int f[nn],ff[nn],a[nn],b[nn],n,m,nx[nn],pr[nn],L[nn],R[nn];

inline int Ask(int p){
	int l=1,r=m,ps=0;
	while (l<=r){
		int md=(l+r)/2;
		if (b[md]<=p) ps=md,l=md+1;else r=md-1;
	}
	return(ps);
}

int main()
{
	n=Read(),m=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++) b[i]=Read();
	sort(a+1,a+1+n),sort(b+1,b+1+m),a[++n]=1e9;

	R[n]=n,L[1]=1;
	for (int i=n-1;i>=1;i--) if(a[i]+1==a[i+1]) R[i]=R[i+1];else R[i]=i;
	for (int i=2;i<=n;i++) if(a[i-1]+1==a[i]) L[i]=L[i-1];else L[i]=i;

	for (int j=1,i=1;i<=m;i++){while(a[j]<b[i]&&j<=n)j++;nx[i]=j;}
	for (int j=n,i=m;i>=1;i--){while(a[j]>b[i]&&j>=1)j--;pr[i]=j;}

	for (int i=1;i<=m;i++){

		for (int j=1;j<=n;j++) ff[j]=f[j];

		if (a[pr[i]]==b[i]) f[R[pr[i]]]++;
		else{
			int p=a[nx[i]]-b[i];p=nx[i]-p;
			if (p>0&&nx[i]<=n){
				int l_=a[nx[i]]-(nx[i]-L[p]);
				int nm=i-Ask(l_-1);
				f[R[nx[i]]]=max(f[R[nx[i]]],ff[L[p]-1]+nm);
			}

			p=b[i]-a[pr[i]];p=R[pr[i]+p];
			if (p<n&&pr[i]>0){
				int r_=a[pr[i]]+R[p]-pr[i];
				int nm=Ask(r_)-i+1;
				f[R[p+1]]=max(f[R[p+1]],ff[pr[i]]+nm);
			}
		}

		for (int j=1;j<=n;j++) f[j]=max(f[j],f[j-1]);
	}
	printf("%d\n",f[n]);
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 1010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int f[nn],ff[nn],a[nn],b[nn],n,m,nx[nn],pr[nn],L[nn],R[nn];

inline int Ask(int p){
	int l=1,r=m,ps=0;
	while (l<=r){
		int md=(l+r)/2;
		if (b[md]<=p) ps=md,l=md+1;else r=md-1;
	}
	return(ps);
}

int main()
{
	n=Read(),m=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++) b[i]=Read();
	sort(a+1,a+1+n),sort(b+1,b+1+m),a[++n]=1e9;

	R[n]=n,L[1]=1;
	for (int i=n-1;i>=1;i--) if(a[i]+1==a[i+1]) R[i]=R[i+1];else R[i]=i;
	for (int i=2;i<=n;i++) if(a[i-1]+1==a[i]) L[i]=L[i-1];else L[i]=i;

	for (int j=1,i=1;i<=m;i++){while(a[j]<b[i]&&j<=n)j++;nx[i]=j;}
	for (int j=n,i=m;i>=1;i--){while(a[j]>b[i]&&j>=1)j--;pr[i]=j;}

	for (int i=1;i<=m;i++){

		for (int j=1;j<=n;j++) ff[j]=f[j];

		if (a[pr[i]]==b[i]) f[R[pr[i]]]++;
		else{
			int p=a[nx[i]]-b[i];p=nx[i]-p;
			if (p>0&&nx[i]<=n){
				int l_=a[nx[i]]-(nx[i]-L[p]);
				int nm=i-Ask(l_-1);
				f[R[nx[i]]]=max(f[R[nx[i]]],ff[L[p]-1]+nm);
			}

			p=b[i]-a[pr[i]];p=R[pr[i]+p];
			if (p<n&&pr[i]>0){
				int r_=a[pr[i]]+R[p]-pr[i];
				int nm=Ask(r_)-i+1;
				f[R[p+1]]=max(f[R[p+1]],ff[pr[i]]+nm);
			}
		}

		for (int j=1;j<=n;j++) f[j]=max(f[j],f[j-1]);
	}
	printf("%d\n",f[n]);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
//#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left

template < typename T > T abs(T x)
{
	return x > 0 ? x : -x;
}

int a[300005], b[2005];
int dp[300005];

int ll[300005], rr[300005];

int main(int argc, char *argv[])
{
	//freopen("a.in", "r", stdin);

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	for (int j = 0; j < m; j++)
	{
		scanf("%d", b + j);
	}

	sort(a, a + n);
	sort(b, b + m);

	for (int i = 0; i < n; i++)
	{
		ll[i] = i;

		if (i && a[i] - 1 == a[i - 1])
			ll[i] = ll[i - 1];
	}

	for (int j = n - 1; j >= 0; j--)
	{
		rr[j] = j;
		if (j < n - 1 && a[j] + 1 == a[j + 1])
			rr[j] = rr[j + 1];
	}

	int bp = 0;

	for (int i = 0; i < n; i++)
	{
		while (bp < m && b[bp] < a[i]) bp++;

		int left = 0;

		if (bp < m && b[bp] == a[i])
			left++;

		int curr = 0;

		if (ll[i] - 1 >= 0)
			curr = dp[ll[i] - 1];

		for (int j = bp - 1; j >= 0; j--)
		{
			int diff = a[i] - b[j];
		//	cerr << i << " " << diff << endl;

			if (diff > i)
				break;

			int v = bp - j;

			int g = ll[i - diff];

			if (g - 1 >= 0)
				v += dp[g - 1];

			curr = max(curr, v);
		}

		left += curr;

		//cerr << i << " " << left << endl;

	//	cerr << i << " " << dp[i] << endl;
		dp[rr[i]] = max(dp[rr[i]], left);

		int j = bp;
		if (b[j] == a[i])
			j++;

		int cnt = 1;
		for (; j < m; j++)
		{
			int diff = b[j] - a[i];
			//cerr << i << " " << diff << endl;

			if (n - i - 1 < diff)
				break;

			int v = cnt;

			int g = rr[i + diff];

			if (g < n)
				dp[g] = max(dp[g], left + v);

			cnt++;
		}
	}

	int ans = 0;

	for (int i = 0; i < n; i++)
	{
		//cerr << dp[i] << endl;
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
//#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left

template < typename T > T abs(T x)
{
	return x > 0 ? x : -x;
}

int a[300005], b[2005];
int dp[300005];

int ll[300005], rr[300005];

int main(int argc, char *argv[])
{
	freopen("a.in", "r", stdin);

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	for (int j = 0; j < m; j++)
	{
		scanf("%d", b + j);
	}

	sort(a, a + n);
	sort(b, b + m);

	for (int i = 0; i < n; i++)
	{
		ll[i] = i;

		if (i && a[i] - 1 == a[i - 1])
			ll[i] = ll[i - 1];
	}

	for (int j = n - 1; j >= 0; j--)
	{
		rr[j] = j;
		if (j < n - 1 && a[j] + 1 == a[j + 1])
			rr[j] = rr[j + 1];
	}

	int bp = 0;

	for (int i = 0; i < n; i++)
	{
		while (bp < m && b[bp] < a[i]) bp++;

		int left = 0;

		if (bp < m && b[bp] == a[i])
			left++;

		int curr = 0;

		if (ll[i] - 1 >= 0)
			curr = dp[ll[i] - 1];

		for (int j = bp - 1; j >= 0; j--)
		{
			int diff = a[i] - b[j];
		//	cerr << i << " " << diff << endl;

			if (diff > i)
				break;

			int v = bp - j;

			int g = ll[i - diff];

			if (g - 1 >= 0)
				v += dp[g - 1];

			curr = max(curr, v);
		}

		left += curr;

		//cerr << i << " " << left << endl;

	//	cerr << i << " " << dp[i] << endl;
		dp[rr[i]] = max(dp[rr[i]], left);

		int j = bp;
		if (b[j] == a[i])
			j++;

		int cnt = 1;
		for (; j < m; j++)
		{
			int diff = b[j] - a[i];
			//cerr << i << " " << diff << endl;

			if (n - i - 1 < diff)
				break;

			int v = cnt;

			int g = rr[i + diff];

			if (g < n)
				dp[g] = max(dp[g], left + v);

			cnt++;
		}
	}

	int ans = 0;

	for (int i = 0; i < n; i++)
	{
		//cerr << dp[i] << endl;
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
//#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left

template < typename T > T abs(T x)
{
	return x > 0 ? x : -x;
}

int a[300005], b[2005];
int dp[300005];

int ll[300005], rr[300005];

int main(int argc, char *argv[])
{
	//freopen("a.in", "r", stdin);

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	for (int j = 0; j < m; j++)
	{
		scanf("%d", b + j);
	}

	sort(a, a + n);
	sort(b, b + m);

	for (int i = 0; i < n; i++)
	{
		ll[i] = i;

		if (i && a[i] - 1 == a[i - 1])
			ll[i] = ll[i - 1];
	}

	for (int j = n - 1; j >= 0; j--)
	{
		rr[j] = j;
		if (j < n - 1 && a[j] + 1 == a[j + 1])
			rr[j] = rr[j + 1];
	}

	int bp = 0;

	for (int i = 0; i < n; i++)
	{
		while (bp < m && b[bp] < a[i]) bp++;

		int left = 0;

		if (bp < m && b[bp] == a[i])
			left++;

		int curr = 0;
		for (int j = bp - 1; j >= 0; j--)
		{
			int diff = a[i] - b[j];
		//	cerr << i << " " << diff << endl;

			if (diff > i)
				break;

			int v = bp - j;

			int g = ll[i - diff];

			if (g - 1 >= 0)
				v += dp[g - 1];

			curr = max(curr, v);
		}

		left += curr;

		//cerr << i << " " << curr << endl;

	//	cerr << i << " " << dp[i] << endl;
		dp[rr[i]] = max(dp[rr[i]], left);

		int j = bp;
		if (b[j] == a[i])
			j++;

		int cnt = 1;
		for (; j < m; j++)
		{
			int diff = b[j] - a[i];
			//cerr << i << " " << diff << endl;

			if (n - i - 1 < diff)
				break;

			int v = cnt;

			int g = rr[i + diff];

			if (g < n)
				dp[g] = max(dp[g], left + v);

			cnt++;
		}
	}

	int ans = 0;

	for (int i = 0; i < n; i++)
	{
		//cerr << dp[i] << endl;
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
//#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left

template < typename T > T abs(T x)
{
	return x > 0 ? x : -x;
}

int a[300005], b[2005];
int dp[300005];

int ll[300005], rr[300005];

int main(int argc, char *argv[])
{
	freopen("a.in", "r", stdin);

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	for (int j = 0; j < m; j++)
	{
		scanf("%d", b + j);
	}

	sort(a, a + n);
	sort(b, b + m);

	for (int i = 0; i < n; i++)
	{
		ll[i] = i;

		if (i && a[i] - 1 == a[i - 1])
			ll[i] = ll[i - 1];
	}

	for (int j = n - 1; j >= 0; j--)
	{
		rr[j] = j;
		if (j < n - 1 && a[j] + 1 == a[j + 1])
			rr[j] = rr[j + 1];
	}

	int bp = 0;

	for (int i = 0; i < n; i++)
	{
		while (bp < m && b[bp] < a[i]) bp++;

		int left = 0;

		if (bp < m && b[bp] == a[i])
			left++;

		int curr = 0;
		for (int j = bp - 1; j >= 0; j--)
		{
			int diff = a[i] - b[j];
		//	cerr << i << " " << diff << endl;

			if (diff > i)
				break;

			int v = bp - j;

			int g = ll[i - diff];

			if (g - 1 >= 0)
				v += dp[g - 1];

			curr = max(curr, v);
		}

		left += curr;

		//cerr << i << " " << curr << endl;

	//	cerr << i << " " << dp[i] << endl;
		dp[rr[i]] = max(dp[rr[i]], left);

		int j = bp;
		if (b[j] == a[i])
			j++;

		int cnt = 1;
		for (; j < m; j++)
		{
			int diff = b[j] - a[i];
			//cerr << i << " " << diff << endl;

			if (n - i - 1 < diff)
				break;

			int v = cnt;

			int g = rr[i + diff];

			if (g < n)
				dp[g] = max(dp[g], left + v);

			cnt++;
		}
	}

	int ans = 0;

	for (int i = 0; i < n; i++)
	{
		//cerr << dp[i] << endl;
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[0] = g[0] = 0 ;
  for(int i = 1 ; i <= n ; i ++ ){

    f[i] = f[i - 1] + val[i] ;
    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] ;
      if(Rs < 0) continue ;
      int t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] ;
      if(Rs < 0) continue ;
      int t = net[Rs] ;
      if(t > msc) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200020 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;
  /*
  if(n <= 20000){

    f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    for(int j = 0 ; j < i ; j ++ )
        upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])]) ;
    for(int j = 1 ; j <= i ; j ++ )
        upd(g[i] , f[j] + S[r[j] + len[i] - len[j]] - S[r[j]]) ;
  }
    cout << max(f[n] , g[n]) << endl ;
    return 0 ;
  }
  */

  f[0] = g[0] = 0 ;
  for(int i = 1 ; i <= n ; i ++ ){

    f[i] = f[i - 1] + val[i] ;
    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] ;
      if(Rs < 0) continue ;
      int t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    upd(g[i] , f[i]) ;
    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] ;
      if(Rs < 0) continue ;
      int t = net[Rs] ;
      if(t > msc || t <= i ) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){

    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] ;
      if(Rs < 0) continue ;
      int t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] ;
      if(Rs < 0) continue ;
      int t = net[Rs] ;
      if(t > msc) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200020 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  if(n <= 10000){

    f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    for(int j = 0 ; j < i ; j ++ )
        upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])]) ;
    for(int j = 1 ; j <= i ; j ++ )
        upd(g[i] , f[j] + S[r[j] + len[i] - len[j]] - S[r[j]]) ;
  }
    cout << max(f[n] , g[n]) << endl ;
    return 0 ;
  }
  f[0] = g[0] = 0 ;
  for(int i = 1 ; i <= n ; i ++ ){

    f[i] = f[i - 1] + val[i] ;
    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] ;
      if(Rs < 0) continue ;
      int t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] ;
      if(Rs < 0) continue ;
      int t = net[Rs] ;
      if(t > msc) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ) S[i] += S[i - 1] ;

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
  }

  n = nt ;

  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    for(int j = 0 ; j < i ; j ++ )
            upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])]) ;
    for(int j = 1 ; j < i ; j ++ )
            upd(g[i] , f[j] + S[r[j] + len[i] - len[j]] - S[r[j]]) ;
  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ) S[i] += S[i - 1] ;

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
  }

  n = nt ;

  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    for(int j = 0 ; j < i ; j ++ ) upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])] + val[i]) ;
    for(int j = 1 ; j < i ; j ++ ) upd(g[i] , f[j] + S[r[j] + len[i] - len[j]] - S[r[j]]) ;
  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    /*
    for(int j = 0 ; j < i ; j ++ ) upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])]) ;
    */

    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] , t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    // g[j] = max(g[j] , f[i] + S[r[i] + len[j] - len[i]] - S[r[i]]) )
    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] , t = net[Rs] ;
      if(t > msc) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ) S[i] += S[i - 1] ;

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
  }

  n = nt ;

  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){
    for(int j = 0 ; j < i ; j ++ )
        upd(f[i] , g[j] + S[r[i]] - S[r[i] - (len[i] - len[j])]) ;
    for(int j = 1 ; j <= i ; j ++ )
        upd(g[i] , f[j] + S[r[j] + len[i] - len[j]] - S[r[j]]) ;
  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int msc = 200010 ;

bool b[msc] ;
int n , m , a[msc] , S[msc] ;
int l[msc] , r[msc] , len[msc] , f[msc] , g[msc] , val[msc] ;
int bel[msc] , top[msc] , net[msc] , nxt[msc] ;

void init(){
  int x ;
  for(int i = 1 ; i <= n ; i ++ ) scanf("%d",&x) , b[x] = 1 ;
  for(int i = 1 ; i <= m ; i ++ ) scanf("%d",&x) , a[i] = x , S[x] = 1 ;

  sort(a + 1 , a + m + 1) ;
  for(int i = 1 ; i <= msc ; i ++ ){
    S[i] += S[i - 1] ;
    if(S[i] != S[i - 1]) top[S[i]] = i ;
  }

  int nt = 0 ;
  for(int i = 1 ; i <= msc ; i ++ )if(b[i]){
    int j = i ;
    while(b[i + 1]) i ++ ;
    ++ nt , l[nt] = j , r[nt] = i ;
    val[nt] = S[i] - S[j - 1] ;
    len[nt] = i - j + 1 ;
        bel[nt] = S[i] ;
  }

  n = nt ;

  memset(net , 1 , sizeof(net)) ;
  for(int i = 1 ; i <= n ; i ++ ) len[i] += len[i - 1] , nxt[len[i]] = net[len[i]] = i ;
  for(int i = 1 ; i <= msc - 10 ; i ++ ) nxt[i] = max(nxt[i] , nxt[i - 1]) ;
  for(int i = msc - 10 ; i ; i -- ) net[i] = min(net[i] , net[i + 1]) ;
}

void upd(int & x , int y) { if(y > x) x = y ; }

int main(){

  cin >> n >> m ;

  init() ;

  f[1] = g[1] = val[1] ; f[0] = g[0] = 0 ;
  for(int i = 2 ; i <= n ; i ++ ){

    f[i] = f[i - 1] + val[i] ;
    for(int j = 0 ; j < bel[i] ; j ++ ){
      int Rs = top[j + 1] - 1 + len[i] - r[i] ;
      if(Rs < 0) continue ;
      int t = nxt[Rs] ;
      upd(f[i] , g[t] + S[r[i]] - S[r[i] - (len[i] - len[t])]) ;
    }

    for(int j = bel[i] ; j <= m ; j ++ ){
      int Rs = top[j] - r[i] + len[i] ;
      if(Rs < 0) continue ;
      int t = net[Rs] ;
      if(t > msc) continue ;
      upd(g[t] , f[i] + S[r[i] + len[t] - len[i]] - S[r[i]]) ;
    }

  }

  cout << max(g[n] , f[n]) << endl ;

  return 0 ;
}