#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
#define INF 1000000000
#define MOD 1
#define SIZE 100001
using namespace std;

void solve(){
	ll n,p,k;
	cin>>n>>p>>k;
	vector<ll>v(n);
	for (ll i=0; i<n; i++){
		cin>>v[i];
	}
	sort(v.begin(),v.end());
	ll ans=0;
	for (ll i=n-1; i>=0; i--){
		ll res=0,ptr=i,cost=p;
		while(true){
			if(ptr<0){break;}
			if(cost<v[ptr]){break;}
			cost-=v[ptr];res++;
			if(ptr-1>=0){res++;ptr-=2;}
			else{ptr--;}
		}
		ans=max(res,ans);
	}
	cout<<ans<<"\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t;cin>>t;
  while(t--){solve();}
}

// 2 3 4 5 7


#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
#define INF 1000000000
#define MOD 1
#define SIZE 100001
using namespace std;

void solve(){
	ll n,p,k;
	cin>>n>>p>>k;
	vector<ll>v(n);
	ll sum=0;
	for (ll i=0; i<n; i++){
		cin>>v[i];
		sum+=v[i];
	}
	sort(v.begin(),v.end());
	if(sum<=p){cout<<n<<"\n";return;}
	int ans=n,cost=p,i=n-1;
	while (true){
		if(i<0){break;}
		if(sum>cost){
			if(i>0 && sum-v[i-1]>cost){
				sum-=v[i];ans--;i--;
			}
			else if(i>0 && sum-v[i-1]<=cost){
				break;
			}
			else{
				sum-=v[i];ans--;i--;
			}
		}
		else{break;}
	}
	cout<<ans<<"\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t;cin>>t;
  while(t--){solve();}
}

// 2 3 4 5 7


#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
#define INF 1000000000
#define MOD 1
#define SIZE 100001
using namespace std;

void solve(){
	ll n,p,k;
	cin>>n>>p>>k;
	vector<ll>v(n);
	ll sum=0;
	for (ll i=0; i<n; i++){
		cin>>v[i];
		sum+=v[i];
	}
	sort(v.begin(),v.end());
	if(sum<=p){cout<<n<<"\n";return;}
	int ans=n,cost=p;
	for (ll i=n-1; i>=0; i--){
		if(sum>cost){
			if(i>0){
				if(sum-v[i-1]<=cost){
					sum-=v[i-1];
					break;
				}
				else{
					sum-=v[i];ans--;
				}
			}
			else{
				sum-=v[i];ans--;
			}
		}
		else{break;}
	}
	cout<<ans<<"\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t;cin>>t;
  while(t--){solve();}
}

// 2 3 4 5 7


#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
#define INF 1000000000
#define MOD 1
#define SIZE 100001
using namespace std;

void solve(){
	int n,p,k;cin>>n>>p>>k;
	vector<int>v(n);
	for (int i=0; i<n; i++){
		cin>>v[i];
	}
	int ans1=0,ans2=0;
	int temp=p;
	sort(v.begin(),v.end());
	int ptr=0;
	while(true){
		if(ptr>=n){break;}
		if(p<v[ptr]){break;}
		if(ptr+1<n){
			if(p-v[ptr+1]>=0){
				p-=v[ptr+1];
				ans1+=2;
				ptr+=2;
			}
			else{
				p-=v[ptr];
				ans1++;break;
			}
		}
		else{
			p-=v[ptr];ans1++;ptr++;
		}
	}
	ptr=1;
	p=temp;
	while(true){
		if(ptr>=n){break;}
		if(p<v[ptr]){break;}
		if(ptr+1<n){
			if(p-v[ptr+1]>=0){
				p-=v[ptr+1];
				ans2+=2;
				ptr+=2;
			}
			else{
				p-=v[ptr];
				ans2++;break;
			}
		}
		else{
			p-=v[ptr];ans2++;ptr++;
		}
	}
	if(p-v[0]>=0){p-=v[0];ans2++;}
	cout<<max(ans1,ans2)<<"\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;cin>>t;
  while(t--){solve();}
}

// 2 3 4 5 7


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include <map>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i]){
                        count++;
                    }
                }
            }
        }
        else{
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i]){
                        count++;
                    }
                }
            }
            if(a[n-1]<=p) count++;
        }
        cout<<count<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main(){
//    int t;
//    cin>>t;
//    while(t--){
//        string s;
//        cin>>s;
//        int n=s.size();
//    }
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
        }
        else{
            for(int i=1;i<n-1; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
            if(a[n-1]<=p) count++;
            else if(p+a[0]>=a[n-1]) count++;
        }
        cout<<count<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include <map>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
            }
        }
        else{
            for(int i=1;i<n-1; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
            }
            if(a[n-1]<=p) count++;
        }
        cout<<count<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
        }
        else{
            for(int i=1;i<n-1; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
            if(a[n-1]<=p) count++;
        }
        cout<<count<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main(){
//    int t;
//    cin>>t;
//    while(t--){
//        string s;
//        cin>>s;
//        int n=s.size();
//    }
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>1){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
        }
        else{
            for(int i=1;i<n-1; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>1){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i-1]){
                        count++;
                    }
                }
                else{
                    if(p>=a[0]) count++;
                }
            }
            if(a[n-1]<=p) count++;
            else if(p+a[0]>=a[n-1] and count>1) count++;
        }
        cout<<count<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k;
        cin>>n>>p>>k;
        long long int a[n+1],val[n+1];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        val[0]=0;
        int answer=0;
        for(int i=1;i<n+1;i++){
            if(i>=2) val[i]=val[i-2]+a[i-1];
            else val[i]=val[i-1] + a[i-1];
//            cout<<val[i];
            if(val[i]<=p){
                answer=max(answer,i);
            }
        }
        cout<<answer<<endl;
    }
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include <map>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int n,p,k,count=0;
        cin>>n>>p>>k;
        long long int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a,a+n);
        if(n%2==0){
            for(int i=1;i<n; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i]){
                        count++;
                    }
                }
            }
        }
        else{
            for(int i=1;i<n-1; i+=2){
                if(p>=a[i]){
                    p=p-a[i];
                    count+=2;
                }
                else if(count>0){
                    if(p>=a[i-1]){
                        count++;
                    }
                    else if(p+a[0]>=a[i]){
                        count++;
                    }
                }
            }
            if(a[n-1]<=p) count++;
        }
        cout<<count<<endl;
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int a[300000], b[3000];
int N, M;
int dp[2][110000];
int pre[110000], next[110000];

int main(){
    while( scanf( "%d%d", &N, &M ) != EOF ){
        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 + 1 + N );
        sort( b + 1, b + 1 + M );
        pre[1] = 1;
        for( int i = 2; i <= N; i++ ){
            if( a[i] == a[i-1] + 1 ){
                pre[i] = pre[i-1];
            }else{
                pre[i] = i - 1;
            }
        }
        next[N] = N;
        for( int i = N - 1; i >= 1; i-- ){
            if( a[i] == a[i+1] - 1 ){
                next[i] = next[i+1];
            }else{
                next[i] = i;
            }
        }
        memset( dp, 0, sizeof( dp ) );
        int cur = 1;
        int ans = 0;
        for( int i = 1; i <= N; i++ ){
            while( cur <= M && b[cur] <= a[i] ) cur++;
            for( int j = cur - 1; j >= 1 && i - ( a[i] - b[j] ) > 0; j-- ){
                int k = pre[i-(a[i]-b[j])];
                dp[0][i] = max( dp[0][i], dp[1][k] + cur - j );
            }
            for( int j = cur; j <= M && i + ( b[j] - a[i] ) <= N; j++ ){
                int k = next[i+(b[j]-a[i])];
                dp[1][k] = max( dp[1][k], dp[0][i] + j - cur + 1 );
            }
            ans = max( dp[0][i], ans );
            ans = max( ans, dp[1][i] );
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int pre[100010];
int next[100010];

int main() {
    int index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    pre[0] = 0;
    monster[0] = 0;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[monster[i]] = pre[monster[i-1]]; }
        else { pre[monster[i]] = monster[i-1]; }
    }
    for (i = n; i; i--) {
        if (monster[i] == monster[i+1] - 1) { next[monster[i]] = next[monster[i+1]]; }
        else { next[monster[i]] = monster[i+1]; }
    }

    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]] + (index-j+1));
        }
        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], dp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int sdp[100010];
int pre[100010];

int main() {
    int fix, index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    memset(sdp, 0, sizeof(sdp));
    monster[0] = -INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[i] = pre[i-1]; }
        else { pre[i] = i; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        sdp[i] = max(sdp[i-1], dp[pre[i]-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]-1] + (index-j+1));
            sdp[i] = max(sdp[i], dp[pre[i-cost]-1] + (index-j+1));
        }

        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], sdp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int rdp[100010];
int pre[100010];
int next[100010];

int main() {
    int fix, index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    memset(rdp, 0, sizeof(rdp));
    monster[0] = -INF;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[i] = pre[i-1]; }
        else { pre[i] = i; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        rdp[i] = max(rdp[i], rdp[i-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]-1] + (index-j+1));
            rdp[i] = max(rdp[i], dp[pre[i-cost]-1] + (index-j+1));
        }

        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], rdp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int rdp[100010];
int pre[100010];
int next[100010];

int main() {
    int fix, index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    memset(rdp, 0, sizeof(rdp));
    monster[0] = -INF;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[i] = pre[i-1]; }
        else { pre[i] = i; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        rdp[i] = max(rdp[i-1], dp[pre[i]-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]-1] + (index-j+1));
            rdp[i] = max(rdp[i], dp[pre[i-cost]-1] + (index-j+1));
        }

        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], rdp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int pre[100010];
int next[100010];

int main() {
    int index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    pre[0] = 0;
    monster[0] = 0;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[monster[i]] = pre[monster[i-1]]; }
        else { pre[monster[i]] = monster[i-1]; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]] + (index-j+1));
        }
        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], dp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int pre[100010];
int next[100010];

int main() {
    int fix, index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    monster[0] = -INF;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[i] = pre[i-1]; }
        else { pre[i] = i; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]-1] + (index-j+1));
        }

        fix = 0;
        for (j = index; j && star[j] >= monster[pre[i]]; j--, fix++);

        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], dp[pre[i]-1] + (j-index+fix));
        }
    }
    cout << dp[n] << endl;
}


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

int monster[100010];
int star[2010];
int dp[100010];

int main() {
    int index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }

    for (i = index = 1; i <= n; i++) {
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j++) {
            int cost = monster[i] - star[j] + 1;
            dp[i] = max(dp[i], dp[i-cost] + (index-j+1));
        }
        for (j = index+1; j < m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i] + 1;
            dp[i+cost] = max(dp[i+cost], dp[i] + (j-index));
        }
    }
    cout << dp[n] << endl;
}


#include<bits/stdc++.h>
#define INF (1<<29)
using namespace std;

int monster[100010];
int star[2010];
int dp[100010];
int pre[100010];
int next[100010];

int main() {
    int fix, index, i, j, n, m;
    cin >> n >> m;

    memset(dp, 0, sizeof(dp));
    monster[0] = -INF;
    monster[n+1] = INF;
    for (i = 1; i <= n; i++) { cin >> monster[i]; }
    for (i = 1; i <= m; i++) { cin >> star[i]; }
    sort(monster+1, monster+n+1);
    sort(star+1, star+m+1);

    for (i = 1; i <= n; i++) {
        if (monster[i] == monster[i-1] + 1) { pre[i] = pre[i-1]; }
        else { pre[i] = i; }
    }

    for (i = 1, index = 0; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        while (index < m && star[index+1] <= monster[i]) { index++; }

        for (j = index; j && monster[i] - star[j] < i; j--) {
            int cost = monster[i] - star[j];
            dp[i] = max(dp[i], dp[pre[i-cost]-1] + (index-j+1));
        }

        fix = 0;
        for (j = index; j && star[j] >= pre[i]; j--, fix++);

        for (j = index+1; j <= m && star[j] - monster[i] <= n - i; j++) {
            int cost = star[j] - monster[i];
            dp[i+cost] = max(dp[i+cost], dp[pre[i]-1] + (j-index+fix));
        }
    }
    cout << dp[n] << endl;
}


#include <algorithm>
#include <stdio.h>

using namespace std;

#define NN 110000
#define inf 1000000000

int a[NN], dp[NN], b[2100];
int st[NN], en[NN];

int  main() {

	int N, M;
	scanf("%d%d", &N, &M);
	for (int i=0; i<N; i++) scanf("%d", &a[i]);
	for (int i=0; i<M; i++) scanf("%d", &b[i]);

	sort(a, a+N); sort(b, b+M);

	for (int i=0, k; i<N; i=k) {
		for (k=i; k<N && a[k]==a[i]+k-i; k++);
		for (int j=i; j<k; j++) st[j]=i, en[j]=k-1;
	}

	for (int i=0; i<=N; i++) dp[i]=-inf;

	dp[0]=0;
	for (int i=0, j=0; i<N; i++) if (en[i]==i) {
		while (j<M && b[j]<a[i]) j++;
		int mx=dp[st[i]];
		for (int k=j-1; k>=0; k--) {
			int cnt=a[i]-b[k]+1;
			int p=i-cnt+1;
			if (p<0) break;
			mx=max(mx, dp[st[p]]+j-k);
		}
		dp[i+1]=max(dp[i+1], mx);
		for (int k=j; k<M; k++) {
			int cnt=b[k]-a[i]+1;
			int p=i+cnt-1;
			if (p>=N) continue;
			dp[en[p]+1]=max(dp[en[p]+1], mx+k-j+1);
		}
	}

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

	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 2010;
const int M = 100010;

int d[N][N],e[N][N],f[N][N];
// d - ultimul x e de acumulare
// e - ultimul x nu e de acumulare si folosesc ultimul j
// f - ultimul x nu e de acumulare

int n,m;
int pl[M],where[N];

int good(int x)
{
    return max(x,0);
}

int main()
{
    ios::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        ifstream F("p.in");
    #endif

    F>>m>>n;
    for (int i=1;i<=m;++i)
        F>>pl[i];
    for (int i=1;i<=n;++i)
        F>>where[i];

    sort(pl+1,pl+m+1);
    sort(where+1,where+n+1);

    int now = 1;
    int ans = 0;
    for (int i=1;i<=n;++i)
    {
        while ( pl[now] < where[i] && now <= m ) ++now;
        for (int j=1;j<=i;++j)
        {
            d[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 ) d[i][j] = min(d[i][j],d[i-1][j]);
            if ( now <= m && (pl[now]-where[i]) + min(e[i-1][j-1],d[i-1][j-1]) < now )
                d[i][j] = min(d[i][j],now);

            e[i][j] = 1<<30;
            if ( where[i] >= pl[ d[i-1][j-1] ] && j-1 > 0 ) e[i][j] = min(e[i][j],where[i] - pl[ d[i-1][j-1] ] + d[i-1][j-1] );

            f[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 )
                f[i][j] = min(f[i-1][j]+where[i]-where[i-1],f[i][j]);
            f[i][j] = min(f[i][j],e[i][j]);

            if ( i-1 > 0 && j <= i-1 ) e[i][j] = min(e[i][j],e[i-1][j]);
            if ( j > 0 ) e[i][j] = min(e[i][j],f[i][j]); // j-1 , f[i][j-1]

            if ( d[i][j] <= m || e[i][j] <= m ) ans = max(ans,j);
        }
    }
    G<<ans<<'\n';
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 2010;
const int M = 100010;

long long d[N][N],e[N][N],f[N][N];
// d - ultimul x e de acumulare
// e - ultimul x nu e de acumulare si folosesc ultimul j
// f - ultimul x nu e de acumulare

int n,m;
int pl[M],where[N];

int good(int x)
{
    return max(x,0);
}

int main()
{
    ios::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        ifstream F("p.in");
    #endif

    F>>m>>n;
    for (int i=1;i<=m;++i)
        F>>pl[i];
    for (int i=1;i<=n;++i)
        F>>where[i];

    sort(pl+1,pl+m+1);
    sort(where+1,where+n+1);

    long long now = 1;
    int ans = 0;
    for (int i=1;i<=n;++i)
    {
        while ( pl[now] < where[i] && now <= m ) ++now;
        for (int j=1;j<=i;++j)
        {
            d[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 ) d[i][j] = min(d[i][j],d[i-1][j]);
            if ( now <= m && (pl[now]-where[i]) + min(e[i-1][j-1],d[i-1][j-1]) < now )
                d[i][j] = min(d[i][j],now);

            e[i][j] = 1<<30;
            if ( where[i] >= pl[ d[i-1][j-1] ] && j-1 > 0 ) e[i][j] = min(e[i][j],where[i] - pl[ d[i-1][j-1] ] + d[i-1][j-1] );

            f[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 )
                f[i][j] = min(f[i-1][j]+where[i]-where[i-1],f[i][j]);
            f[i][j] = min(f[i][j],e[i][j]);

            if ( i-1 > 0 && j <= i-1 ) e[i][j] = min(e[i][j],e[i-1][j]);
            if ( j-1 > 0 ) e[i][j] = min(e[i][j],f[i][j-1]);

            if ( d[i][j] <= m || e[i][j] <= m ) ans = max(ans,j);
        }
    }
    G<<ans<<'\n';
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 2010;
const int M = 100010;

int d[N][N],e[N][N],f[N][N];
// d - ultimul x e de acumulare
// e - ultimul x nu e de acumulare si folosesc ultimul j
// f - ultimul x nu e de acumulare

int n,m;
int pl[M],where[N];

int good(int x)
{
    return max(x,0);
}

int main()
{
    ios::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        ifstream F("p.in");
    #endif

    F>>m>>n;
    for (int i=1;i<=m;++i)
        F>>pl[i];
    for (int i=1;i<=n;++i)
        F>>where[i];

    sort(pl+1,pl+m+1);
    sort(where+1,where+n+1);

    int now = 1;
    int ans = 0;
    for (int i=1;i<=n;++i)
    {
        while ( pl[now] < where[i] && now <= m ) ++now;
        for (int j=1;j<=i;++j)
        {
            d[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 ) d[i][j] = min(d[i][j],d[i-1][j]);
            if ( now <= m && (pl[now]-where[i]) + min(e[i-1][j-1],d[i-1][j-1]) < now )
                d[i][j] = min(d[i][j],now);

            e[i][j] = 1<<30;
            if ( j > 1 ) e[i][j] = min(e[i][j],good(where[i] - pl[ d[i-1][j-1] ]) + d[i-1][j-1] );

            if ( i-1 > 0 && j <= i-1 )
                f[i][j] = min(f[i-1][j]+pl[i]-pl[i-1],e[i][j]);
            else
                f[i][j] = e[i][j];

            if ( i-1 > 0 && j <= i-1 ) e[i][j] = min(e[i][j],e[i-1][j]);
            if ( j-1 > 0 ) e[i][j] = min(e[i][j],f[i][j-1]);

            if ( d[i][j] <= m || e[i][j] <= m ) ans = max(ans,j);
        }
    }
    G<<ans<<'\n';
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 2010;
const int M = 100010;

long long d[N][N],e[N][N],f[N][N];
// d - ultimul x e de acumulare
// e - ultimul x nu e de acumulare si folosesc ultimul j
// f - ultimul x nu e de acumulare

int n,m;
int pl[M],where[N];

int good(int x)
{
    return max(x,0);
}

int main()
{
    ios::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        ifstream F("p.in");
    #endif

    F>>m>>n;
    for (int i=1;i<=m;++i)
        F>>pl[i];
    for (int i=1;i<=n;++i)
        F>>where[i];

    sort(pl+1,pl+m+1);
    sort(where+1,where+n+1);

    long long now = 1;
    int ans = 0;
    int x = 1;
    while ( pl[x] < where[1] && x <= m )
        ++x;
    --x;
    d[0][0] = x;
    for (int i=1;i<=n;++i)
    {
        while ( pl[now] < where[i] && now <= m ) ++now;
        for (int j=1;j<=i;++j)
        {
            d[i][j] = 1<<30;
            if ( j <= i-1 ) d[i][j] = min(d[i][j],d[i-1][j]);
            if ( now <= m && (pl[now]-where[i]) + min(e[i-1][j-1],d[i-1][j-1]) < now )
                d[i][j] = min(d[i][j],now);

            e[i][j] = 1<<30;
            if ( where[i] >= pl[ d[i-1][j-1] ] ) e[i][j] = min(e[i][j],where[i] - pl[ d[i-1][j-1] ] + d[i-1][j-1] );

            f[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 )
                f[i][j] = min(f[i-1][j]+where[i]-where[i-1],f[i][j]);
            f[i][j] = min(f[i][j],e[i][j]);

            if ( i-1 > 0 && j <= i-1 ) e[i][j] = min(e[i][j],e[i-1][j]);
            if ( j-1 > 0 ) e[i][j] = min(e[i][j],f[i][j-1]);

            if ( d[i][j] <= m || e[i][j] <= m ) ans = max(ans,j);
        }
    }
    G<<ans<<'\n';
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 2010;
const int M = 100010;

int d[N][N],e[N][N],f[N][N];
// d - ultimul x e de acumulare
// e - ultimul x nu e de acumulare si folosesc ultimul j
// f - ultimul x nu e de acumulare

int n,m;
int pl[M],where[N];

int good(int x)
{
    return max(x,0);
}

int main()
{
    ios::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        ifstream F("p.in");
    #endif

    F>>m>>n;
    for (int i=1;i<=m;++i)
        F>>pl[i];
    for (int i=1;i<=n;++i)
        F>>where[i];

    sort(pl+1,pl+m+1);
    sort(where+1,where+n+1);

    int now = 1;
    int ans = 0;
    for (int i=1;i<=n;++i)
    {
        while ( pl[now] < where[i] && now <= m ) ++now;
        for (int j=1;j<=i;++j)
        {
            d[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 ) d[i][j] = min(d[i][j],d[i-1][j]);
            if ( now <= m && (pl[now]-where[i]) + min(e[i-1][j-1],d[i-1][j-1]) < now )
                d[i][j] = min(d[i][j],now);

            e[i][j] = 1<<30;
            if ( where[i] >= pl[ d[i-1][j-1] ] && j-1 > 0 ) e[i][j] = min(e[i][j],where[i] - pl[ d[i-1][j-1] ] + d[i-1][j-1] );

            f[i][j] = 1<<30;
            if ( i-1 > 0 && j <= i-1 )
                f[i][j] = min(f[i-1][j]+where[i]-where[i-1],e[i][j]);
            else
                f[i][j] = min(f[i][j],e[i][j]);

            if ( i-1 > 0 && j <= i-1 ) e[i][j] = min(e[i][j],e[i-1][j]);
            if ( j-1 > 0 ) e[i][j] = min(e[i][j],f[i][j-1]);

            if ( d[i][j] <= m || e[i][j] <= m ) ans = max(ans,j);
        }
    }
    G<<ans<<'\n';
}


#include <iostream>
#include <algorithm>
using namespace std;

#define F cin
#define G cout

const int N = 100010;

int n,m,a[N],b[N],l[N],d[N],e[N];

int main()
{
    F>>n>>m;
    for (int i=0;i<n;++i) F>>a[i];
    for (int i=0;i<m;++i) F>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    for (int i=0;i<n;++i)
    {
        if ( i && a[i-1]+1 == a[i] )
            l[i] = l[i-1];
        else
            l[i] = i;

        int k = upper_bound(b,b+m,a[i])-b;
        for (int j=k-1;j>=0 && i-a[i]+b[j]>=0;--j)
            d[i] = max( e[ l[i-a[i]+b[j]] ] + k-j , d[i]);
        if ( l[i] == i )
            d[i] = max( d[i],e[i] );

        e[i+1] = max( e[i+1],d[i] );
        e[i+1] = max( e[i+1],e[i] );
        d[i+1] = max( d[i+1],d[i] );

        for (int j=k;j<m && i-a[i]+b[j]<n;++j)
            e[i-a[i]+b[j]+1] = max( e[i-a[i]+b[j]+1],d[i]+j-k+1 );
    }
    G<<e[n]<<'\n';
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

const int maxw = 2 * 1e5;
const int W = maxw + 10;
const int N = 100010, M = 2020;

int n, m, a[W], b[W], s[W], od[W], col[W], lef[W];
int t, at[N], ls[N], rs[N], f[N], g[N];
int pos[M];

int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int foo;
		scanf("%d", &foo);
		a[foo] = 1;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d", pos + i);
		b[pos[i]] = 1;
	}

	for (int i = 1, j; i <= maxw; ++i) {
		if (!a[i])
			continue;
		for (j = i; j + 1 <= maxw && a[j + 1]; ++j);
		ls[++t] = i, rs[t] = j;

		for (int l = i; l <= j; ++l)
			col[l] = t;
		i = j;
	}
	int cnt = 0;
	for (int i = 1; i <= maxw; ++i)
		if (a[i]) {
			at[++cnt] = i;
			od[i] = cnt;
		}
	for (int i = 1; i <= maxw; ++i)
		lef[i] = a[i]? i: lef[i - 1];

	for (int i = 1; i <= maxw; ++i)
		s[i] = s[i - 1] + b[i];

	for (int i = 1; i <= t; ++i) {
		g[i] = g[i - 1];
		for (int o = 1, x; x = pos[o], o <= m; ++o) {
			if (x > rs[i])
				continue;
			int dta = rs[i] - x + 1;
			int need = od[rs[i]] - dta + 1;
			if (need > 0) {
				int j = col[at[need]];
				int li = rs[i] - (od[rs[i]] - od[ls[j]] + 1);
				g[i] = max(g[i], f[j - 1] + s[rs[i]] - s[li]);
			}
		}
		f[i] = f[i - 1];
		for (int o = 1, x; x = lef[pos[o]], o <= m; ++o) {
			if (x <= 0)
				continue;
			int j = col[x];
			int len = od[rs[i]] - od[rs[j]];
			int pts = s[rs[j] + len] - s[rs[j]];
			f[i] = max(f[i], g[j] + pts);
		}
	}
	printf("%d\n", f[t]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

const int maxw = 2 * 1e5;
const int W = maxw + 10;
const int N = 100010, M = 2020;

int n, m, a[W], b[W], s[W], od[W], col[W], lef[W];
int t, at[N], ls[N], rs[N], f[N], g[N];
int pos[M];

int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int foo;
		scanf("%d", &foo);
		a[foo] = 1;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d", pos + i);
		b[pos[i]] = 1;
	}

	for (int i = 1, j; i <= maxw; ++i) {
		if (!a[i])
			continue;
		for (j = i; j + 1 <= maxw && a[j + 1]; ++j);
		ls[++t] = i, rs[t] = j;

		for (int l = i; l <= j; ++l)
			col[l] = t;
		i = j;
	}
	int cnt = 0;
	for (int i = 1; i <= maxw; ++i)
		if (a[i]) {
			at[++cnt] = i;
			od[i] = cnt;
		}
	for (int i = 1; i <= maxw; ++i)
		lef[i] = a[i]? i: lef[i - 1];

	for (int i = 1; i <= maxw; ++i)
		s[i] = s[i - 1] + b[i];

	for (int i = 1; i <= t; ++i) {
		g[i] = max(g[i - 1], f[i - 1]);
		for (int o = 1, x; x = pos[o], o <= m; ++o) {
			if (x > rs[i])
				continue;
			int dta = rs[i] - x + 1;
			int need = od[rs[i]] - dta + 1;
			if (need > 0) {
				int j = col[at[need]];
				int li = rs[i] - (od[rs[i]] - od[ls[j]] + 1);
				g[i] = max(g[i], f[j - 1] + s[rs[i]] - s[li]);
			}
		}
		f[i] = f[i - 1];
		for (int o = 1, x; x = lef[pos[o]], o <= m; ++o) {
			if (x <= 0)
				continue;
			int j = col[x];
			int len = od[rs[i]] - od[rs[j]];
			int pts = s[rs[j] + len] - s[rs[j]];
			f[i] = max(f[i], g[j] + pts);
		}
	}
	printf("%d\n", f[t]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

const int maxw = 2 * 1e5;
const int W = maxw + 10;
const int N = 100010, M = 2020;

int n, m, a[W], b[W], s[W], od[W], col[W], lef[W];
int t, at[N], ls[N], rs[N], f[N], g[N];
int pos[M];

int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int foo;
		scanf("%d", &foo);
		a[foo] = 1;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d", pos + i);
		b[pos[i]] = 1;
	}
	sort(pos + 1, pos + m + 1);

	for (int i = 1, j; i <= maxw; ++i) {
		if (!a[i])
			continue;
		for (j = i; j + 1 <= maxw && a[j + 1]; ++j);
		ls[++t] = i, rs[t] = j;

		for (int l = i; l <= j; ++l)
			col[l] = t;
		i = j;
	}
	int cnt = 0;
	for (int i = 1; i <= maxw; ++i)
		if (a[i]) {
			at[++cnt] = i;
			od[i] = cnt;
		}
	for (int i = 1; i <= maxw; ++i)
		lef[i] = a[i]? i: lef[i - 1];

	for (int i = 1; i <= maxw; ++i)
		s[i] = s[i - 1] + b[i];

	for (int i = 1; i <= t; ++i) {
		g[i] = max(g[i - 1], f[i - 1]);
		for (int o = 1, x; x = pos[o], o <= m; ++o) {
			if (x > rs[i])
				break;
			int dta = rs[i] - x + 1;
			int need = od[rs[i]] - dta + 1;
			if (need > 0) {
				int j = col[at[need]];
				int li = rs[i] - (od[rs[i]] - od[ls[j]] + 1);
				g[i] = max(g[i], f[j - 1] + s[rs[i]] - s[li]);
			}
		}
		f[i] = max(f[i - 1], g[i]);
		for (int o = 1, x; x = lef[pos[o]], o <= m; ++o) {
			if (x > rs[i])
				break;
			if (x <= 0)
				continue;
			int j = col[x];
			int len = od[rs[i]] - od[rs[j]];
			int pts = s[rs[j] + len] - s[rs[j]];
			f[i] = max(f[i], g[j] + pts);
		}
	}
	printf("%d\n", f[t]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

const int maxw = 2 * 1e5;
const int W = maxw + 10;
const int N = 100010, M = 2020;

int n, m, a[W], b[W], s[W], od[W], col[W], lef[W];
int t, at[N], ls[N], rs[N], f[N], g[N];
int pos[M];

int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int foo;
		scanf("%d", &foo);
		a[foo] = 1;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d", pos + i);
		b[pos[i]] = 1;
	}

	for (int i = 1, j; i <= maxw; ++i) {
		if (!a[i])
			continue;
		for (j = i; j + 1 <= maxw && a[j + 1]; ++j);
		ls[++t] = i, rs[t] = j;

		for (int l = i; l <= j; ++l)
			col[l] = t;
		i = j;
	}
	int cnt = 0;
	for (int i = 1; i <= maxw; ++i)
		if (a[i]) {
			at[++cnt] = i;
			od[i] = cnt;
		}
	for (int i = 1; i <= maxw; ++i)
		lef[i] = a[i]? i: lef[i - 1];

	for (int i = 1; i <= maxw; ++i)
		s[i] = s[i - 1] + b[i];

	for (int i = 1; i <= t; ++i) {
		g[i] = max(g[i - 1], f[i - 1]);
		for (int o = 1, x; x = pos[o], o <= m; ++o) {
			if (x > rs[i])
				continue;
			int dta = rs[i] - x + 1;
			int need = od[rs[i]] - dta + 1;
			if (need > 0) {
				int j = col[at[need]];
				int li = rs[i] - (od[rs[i]] - od[ls[j]] + 1);
				g[i] = max(g[i], f[j - 1] + s[rs[i]] - s[li]);
			}
		}
		f[i] = max(f[i - 1], g[i]);
		for (int o = 1, x; x = lef[pos[o]], o <= m; ++o) {
			if (x <= 0)
				continue;
			int j = col[x];
			int len = od[rs[i]] - od[rs[j]];
			int pts = s[rs[j] + len] - s[rs[j]];
			f[i] = max(f[i], g[j] + pts);
		}
	}
	printf("%d\n", f[t]);
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100050
using namespace std;
int a[N],p[N],f[N],num[N],koe,n,m,l,i,j,wz,ans;
int main()
{
    scanf("%d%d%d",&n,&m);
    for (i=1; i<=n; i++) scanf("%d",&a[i]);
    for (i=1; i<=m; i++) scanf("%d",&p[i]);
    sort(a+1,a+n+1);
    sort(p+1,p+m+1);
    num[1]=1;
    for (i=2; i<=n; i++)
        if (a[i]-a[i-1]==1) num[i]=num[i-1]; else num[i]=i;
    for (i=1,wz=1; i<=n; i++)
    {
        while (p[wz] < a[i] && wz<=m) wz++;
        koe=f[num[i]-1];
        for (j=wz-1; j>0 && a[i] - p[j] < i; j--)
            koe=max(koe, f[num[i - a[i] + p[j]] - 1] + wz - j);
        f[i] = max(koe, max(f[i] , f[i - 1]));
        for (j=wz; j<=m && p[j] - a[i] <= n - i; j++)
            f[i + p[j] - a[i]]=max(f[i + p[j] - a[i]], koe + j - wz + 1);
    }
    printf("%d",f[n]);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100050
using namespace std;
int a[N],p[N],f[N],num[N],koe,n,m,l,i,j,wz,ans;
int main()
{
    scanf("%d%d%d",&n,&m);
    for (i=1; i<=n; i++) scanf("%d",&a[i]);
    for (i=1; i<=m; i++) scanf("%d",&p[i]);
    sort(a+1,a+n+1);
    sort(p+1,p+m+1);
    a[0]=-1;
    for (i=2; i<=n; i++)
        if (a[i]-a[i-1]==1) num[i]=num[i-1]; else num[i]=i;
    for (i=1,wz=1; i<=n; i++)
    {
        while (p[wz] < a[i] && wz<=m) wz++;
        koe=f[num[i]-1];
        for (j=wz-1; j>0 && a[i] - p[j] < i; j--)
            koe=max(koe, f[num[i - a[i] + p[j]] - 1] + wz - j);
        f[i] = max(koe, max(f[i] , f[i - 1]));
        for (j=wz; j<=m && p[j] - a[i] <= n - i; j++)
            f[i + p[j] - a[i]]=max(f[i + p[j] - a[i]], koe + j - wz + 1);
    }
    printf("%d",f[n]);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100050
using namespace std;
int a[N],p[N],f[N],num[N],koe,n,m,l,i,j,wz,ans;
int main()
{
    scanf("%d%d%d",&n,&m,&l);
    for (i=1; i<=n; i++) scanf("%d",&a[i]);
    for (i=1; i<=m; i++) scanf("%d",&p[i]);
    sort(a+1,a+n+1);
    sort(p+1,p+m+1);
    num[1]=1;
    for (i=2; i<=n; i++)
        if (a[i]-a[i-1]==1) num[i]=num[i-1]; else num[i]=i;
    for (i=1,wz=1; i<=n; i++)
    {
        while (p[wz] < a[i] && wz<=m) wz++;
        koe=f[num[i]-1];
        for (j=wz-1; j>0 && a[i] - p[j] < i; j--)
            koe=max(koe, f[num[i - a[i] + p[j]] - 1] + wz - j);
        f[i] = max(koe, max(f[i] , f[i - 1]));
        for (j=wz; j<=m && p[j] - a[i] <= n - i; j++)
            f[i + p[j] - a[i]]=max(f[i + p[j] - a[i]], koe + j - wz + 1);
    }
    printf("%d",f[n]);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100050
using namespace std;
int a[N],p[N],f[N],num[N],koe,n,m,l,i,j,wz,ans;
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++) scanf("%d",&a[i]);
    for (i=1; i<=m; i++) scanf("%d",&p[i]);
    sort(a+1,a+n+1);
    sort(p+1,p+m+1);
    num[1]=1;
    for (i=2; i<=n; i++)
        if (a[i]-a[i-1]==1) num[i]=num[i-1]; else num[i]=i;
    for (i=1,wz=1; i<=n; i++)
    {
        while (p[wz] < a[i] && wz<=m) wz++;
        koe=f[num[i]-1];
        for (j=wz-1; j>0 && a[i] - p[j] < i; j--)
            koe=max(koe, f[num[i - a[i] + p[j]] - 1] + wz - j);
        f[i] = max(koe, max(f[i] , f[i - 1]));
        for (j=wz; j<=m && p[j] - a[i] <= n - i; j++)
            f[i + p[j] - a[i]]=max(f[i + p[j] - a[i]], koe + j - wz + 1);
    }
    printf("%d",f[n]);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=100005;
int n,m,a[N],b[N],f[N],g[N],L[N];
inline void upd(int &x,int y){if (y>x) x=y;}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n) scanf("%d",a+i);
	rep(i,m) scanf("%d",b+i);
	sort(a,a+n),sort(b,b+m);
	rep(i,n){
		if (i && a[i-1]+1==a[i]) L[i]=L[i-1]; else L[i]=i;
		int k=upper_bound(b,b+m,a[i])-b;
		for (int j=k-1;j>=0 && i-a[i]+b[j]>=0;--j)
			upd(f[i],g[L[i-a[i]+b[j]]]+k-j);
		if (L[i]==i) upd(f[i],g[i]); upd(g[i+1],f[i]),upd(g[i+1],g[i]),upd(f[i+1],f[i]);
		for (int j=k;j<m && i-a[i]+b[j]<n;++j)
			upd(g[i-a[i]+b[j]+1],f[i]+j-k+1);
	}
	printf("%d\n",g[n]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=100005;
int n,m,a[N],b[N],f[N],g[N],L[N];
inline void upd(int &x,int y){if (y>x) x=y;}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n) scanf("%d",a+i);
	rep(i,m) scanf("%d",b+i);
	sort(a,a+n),sort(b,b+m);
	rep(i,n){
		if (i && a[i-1]+1==a[i]) L[i]=L[i-1]; else L[i]=i;
		upd(g[i+1],g[i]),upd(f[i+1],f[i]);
		int k=upper_bound(b,b+m,a[i])-b;
		for (int j=k-1;j>=0 && i-a[i]+b[j]>=0;--j)
			upd(f[i],g[L[i-a[i]+b[j]]]+k-j);
		for (int j=k;j<m && i-a[i]+b[j]<n;++j)
			upd(g[i-a[i]+b[j]+1],f[i]+j-k+1);
	}
	printf("%d\n",g[n]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=100005;
int n,m,a[N],b[N],f[N],g[N],L[N];
inline void upd(int &x,int y){if (y>x) x=y;}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n) scanf("%d",a+i);
	rep(i,m) scanf("%d",b+i);
	sort(a,a+n),sort(b,b+m);
	rep(i,n){
		if (i && a[i-1]+1==a[i]) L[i]=L[i-1]; else L[i]=i;
		int k=upper_bound(b,b+m,a[i])-b;
		for (int j=k-1;j>=0 && i-a[i]+b[j]>=0;--j)
			upd(f[i],g[L[i-a[i]+b[j]]]+k-j);
		upd(f[i],g[i]),upd(g[i+1],f[i]),upd(g[i+1],g[i]),upd(f[i+1],f[i]);
		for (int j=k;j<m && i-a[i]+b[j]<n;++j)
			upd(g[i-a[i]+b[j]+1],f[i]+j-k+1);
	}
	printf("%d\n",g[n]);
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100 * 1000 + 5, MAXM = 2005;
int a[MAXN], b[MAXM], l[MAXN], dp[MAXN], dp0[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];
    sort(a, a + n);
    sort(b, b + m);
    l[0] = 0;
    for(int i = 1; i < n; i++)
        l[i] = a[i] == a[i - 1] + 1? l[i - 1] : i;
    for(int i = 0; i < n; i++) {
        int p = upper_bound(b, b + m, a[i]) - b;
        if(i == l[i])
            dp[i] = max(dp[i], i? dp0[i - 1] : 0);
        else
            dp[i] = max(dp[i], dp[i - 1]);
        for(int j = 0; j < p; j++) {
            int k = a[i] - b[j];
            if(k <= i)
                dp[i] = max(dp[i], (l[i - k]? dp0[l[i - k] - 1] : 0) + p - j);
        }
        dp0[i] = max(dp0[i], dp[i]);
        for(int j = p; j < m; j++) {
            int k = b[j] - a[i];
            if(i + k < n)
                dp0[i + k] = max(dp0[i + k], dp[i] + j - p + 1);
        }
        dp0[i + 1] = max(dp0[i + 1], dp0[i]);
    }
    cout << dp0[n - 1] << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100 * 1000 + 5, MAXM = 2005;
int a[MAXN], b[MAXM], l[MAXN], dp[MAXN], dp0[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];
    sort(a, a + n);
    sort(b, b + m);
    l[0] = 0;
    for(int i = 1; i < n; i++)
        l[i] = a[i] == a[i - 1] + 1? l[i - 1] : i;
    for(int i = 0; i < n; i++) {
        int p = upper_bound(b, b + m, a[i]) - b;
        for(int j = 0; j < p; j++) {
            int k = a[i] - b[j];
            if(k <= i)
                dp[i] = max(dp[i], (l[i - k]? dp0[l[i - k] - 1] : 0) + p - j);
        }
        dp0[i] = max(dp0[i], dp[i]);
        for(int j = p; j < m; j++) {
            int k = b[j] - a[i];
            if(i + k < n)
                dp0[i + k] = max(dp0[i + k], dp[i] + j - p + 1);
        }
        dp0[i + 1] = max(dp0[i + 1], dp0[i]);
    }
    cout << dp0[n - 1] << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100 * 1000 + 5, MAXM = 2005;
int a[MAXN], b[MAXM], l[MAXN], dp[MAXN], dp0[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];
    sort(a, a + n);
    sort(b, b + m);
    l[0] = 0;
    for(int i = 1; i < n; i++)
        l[i] = a[i] == a[i - 1] + 1? l[i - 1] : i;
    for(int i = 0; i < n; i++) {
        int p = upper_bound(b, b + m, a[i]) - b;
        for(int j = 0; j < p; j++) {
            int k = a[i] - b[j];
            if(k <= i)
                dp[i] = max(dp[i], (l[i - k]? 0 : dp0[l[i - k] - 1]) + p - j);
        }
        dp0[i] = max(dp0[i], dp[i]);
        for(int j = p; j < m; j++) {
            int k = b[j] - a[i];
            if(i + k < n)
                dp0[i + k] = max(dp0[i + k], dp[i] + j - p + 1);
        }
        dp0[i + 1] = max(dp0[i + 1], dp0[i]);
    }
    cout << dp0[n - 1] << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100 * 1000 + 5, MAXM = 2005;
int a[MAXN], b[MAXM], l[MAXN], r[MAXN], dp[MAXN], dp0[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];
    sort(a, a + n);
    sort(b, b + m);
    l[0] = 0;
    for(int i = 1; i < n; i++)
        l[i] = a[i] == a[i - 1] + 1? l[i - 1] : i;
    r[n - 1] = n - 1;
    for(int i = n - 2; i >= 0; i--)
        r[i] = a[i] == a[i + 1] - 1? r[i + 1] : i;
    for(int i = 0; i < n; i++) {
        int p = upper_bound(b, b + m, a[i]) - b;
        for(int j = 0; j < p; j++) {
            int k = a[i] - b[j];
            if(k <= i)
                dp[i] = max(dp[i], (l[i - k]? 0 : dp0[l[i - k] - 1]) + p - j);
        }
        dp0[i] = max(dp0[i], dp[i]);
        for(int j = p; j < m; j++) {
            int k = b[j] - a[i];
            if(i + k < n)
                dp0[r[i + k]] = max(dp0[r[i + k]], dp[i] + j - p + 1);
        }
    }
    cout << dp0[n - 1] << '\n';
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int inf = (int)1e9;
const int N = 1234567;

int a[N], b[N];
int bfrom[N], bto[N];
int f[N];

int main() {
  int n, m;
  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);
  int beg = 1;
  while (beg <= n) {
    int end = beg;
    while (end + 1 <= n && a[end + 1] == a[end] + 1) {
      end++;
    }
    for (int j = beg; j <= end; j++) {
      bfrom[j] = beg;
      bto[j] = end;
    }
    beg = end + 1;
  }
  for (int i = 0; i <= n; i++) f[i] = -inf;
  f[0] = 0;
  int j = 0;
  for (int i = 1; i <= n; i++) {
    if (i != bto[i]) {
      continue;
    }
    while (j + 1 <= m && b[j + 1] <= a[i]) {
      j++;
    }
    int mx = f[bfrom[i] - 1];
    for (int k = j; k >= 1; k--) {
      int cnt = a[i] - b[k];
      int pos = i - cnt;
      if (pos < 1) {
        break;
      }
      int cur = f[bfrom[pos] - 1] + (j - k + 1);
      if (cur > mx) {
        mx = cur;
      }
    }
    if (mx > f[i]) {
      f[i] = mx;
    }
    for (int k = j + 1; k <= m; k++) {
      int cnt = b[k] - a[i];
      int pos = i + cnt;
      if (pos > n) {
        break;
      }
      int cur = mx + (k - j);
      if (cur > f[bto[pos]]) {
        f[bto[pos]] = cur;
      }
    }
    if (i < n && f[i] > f[bto[i + 1]]) {
      f[bto[i + 1]] = f[i];
    }
  }
  printf("%d\n", f[n]);
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
    LL res = 0, q = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') q = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
    return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)1e5;
typedef LL arr[N + 10];

arr f, g, a, b, num;
LL i, j, k, p, n, m, mx;

LL main() {
#ifndef ONLINE_JUDGE
    freopen("pud.in", "r", stdin);
    freopen("pud.out", "w", stdout);
#endif

    scanf(R R "\n", &n, &m);
    FOR (i, 0, n - 1) scanf(R, a + i);
    FOR (i, 0, m - 1) scanf(R, b + i);
    sort(a, a + n), sort(b, b + m);

    FOR (i, 0, n - 1) {
        if (a[i] == a[i - 1] + 1 && i) num[i] = num[i - 1]; else num[i] = i;
        for ( ; b[mx] <= a[i] && mx < m; ++mx);

        g[i + 1] = f[num[i]];
        for (LL j = mx - 1; j >= 0 && i - a[i] + b[j] >= 0; --j)
            g[i + 1] = Max(g[i + 1], f[num[i - (a[i] - b[j])]] + mx - j);

        f[i + 1] = Max(f[i + 1], g[i + 1]);
        for (LL j = mx; j < m && i + b[j] - a[i] + 1 <= n; ++j)
            f[i + b[j] - a[i] + 1] = Max(f[i + b[j] - a[i] + 1], g[i + 1] + j - mx + 1);
        f[i + 1] = Max(f[i + 1], f[i]);
    }

    printf(R "\n", f[n]);

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)1e5;
typedef LL arr[N + 10];

arr f, g, a, b, num;
LL i, j, k, p, n, m, mx;

LL main() {
#ifndef ONLINE_JUDGE
	freopen("pud.in", "r", stdin);
	freopen("pud.out", "w", stdout);
#endif

	scanf(R R "\n", &n, &m);
	FOR (i, 1, n) scanf(R, a + i);
	FOR (i, 1, m) scanf(R, b + i);
	sort(a + 1, a + n + 1), sort(b + 1, b + m + 1);

	FOR (i, 1, n) {
		if (a[i] == a[i - 1] + 1 && a[i - 1]) num[i] = num[i - 1]; else num[i] = i;
		for ( ; b[mx] <= a[i] && mx <= m; ++mx);

		g[i] = f[num[i]];
		for (LL j = mx - 1; j >= 1 && i - a[i] + b[j] - 1 >= 0; --j)
			g[i] = Max(g[i], f[num[i - (a[i] - b[j]) - 1]] + mx - j);

		f[i] = Max(f[i], g[i]);
		for (LL j = mx; j <= m && i + b[j] - a[i] <= n; ++j)
			f[i + b[j] - a[i]] = Max(f[i + b[j] - a[i]], g[i] + j - mx + 1);
		f[i] = Max(f[i], f[i - 1]);
	}

	printf(R "\n", f[n]);

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#define DEBUG 0

using namespace std;

/*
 * */
#define MAXN 100010

int N, M;
int S[MAXN], O[MAXN], mx;
int tot, C[MAXN], ll[MAXN], rr[MAXN];
int F[MAXN], D[MAXN], R[MAXN << 1];
int main()
{
#if DEBUG
	freopen("input.txt", "r", stdin);
#endif

	scanf("%d%d", &N, &M);
	mx = 0;
	for (int i = 1; i <= N; i++)
	{
		scanf("%d", &O[i]);
		mx = max(mx, O[i]);
	}
	memset(R, 0, sizeof(R));
	for (int i = 1; i <= M; i++)
	{
		scanf("%d", &S[i]);
		R[S[i]] = 1;
		mx = max(mx, S[i]);
	}
	sort(O + 1, O + N + 1);
	sort(S + 1, S + M + 1);

	for (int i = 1; i <= mx; i++)
		R[i] += R[i - 1];
	tot = 0;
	for (int i = 1; i <= N; i++)
	{
		if (i == 1 || O[i] != O[i - 1] + 1)
			tot++, C[i] = tot, ll[tot] = rr[tot] = i;
		else if (O[i] == O[i - 1] + 1)
			C[i] = tot, rr[tot] = i;
	}
	memset(F, 0, sizeof(F));
	memset(D, 0, sizeof(D));
	for (int i = 1; i <= N; i++)
	{
		int j, k;
		F[i] = max(F[i], D[ll[C[i]] - 1]);
		for (j = 1; j <= M && S[j] <= O[i]; j++)
			if ((k = i - (O[i] - S[j])) > 0)
            {
                F[i] = max(F[i], D[ll[C[k]] - 1] + R[O[i]] - R[S[j] - 1]);
                // F[i] = max(F[i], D[k - 1] + R[O[i]] - R[S[j] - 1]);
            }
		D[i] = max(D[i], D[i - 1]);
		D[i] = max(D[i], F[i]);
		if (j - 1 && S[j - 1] == O[i]) j--;
		for (; j <= M && S[j] >= O[i]; j++)
			if ((k = i + (S[j] - O[i])) <= N)
            {
                D[rr[C[k]]] = max(D[rr[C[k]]], F[i] + R[S[j]] - R[O[i]]);
                // D[k] = max(D[k], F[i] + R[S[j]] - R[O[i]]);
            }

	}
	printf("%d\n", D[N]);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#define DEBUG 0

using namespace std;

/*
 * */
#define MAXN 100010

int N, M;
int S[MAXN], O[MAXN], mx;
int tot, C[MAXN], ll[MAXN], rr[MAXN];
int F[MAXN], D[MAXN], R[MAXN << 1];
int main()
{
#if DEBUG
	freopen("input.txt", "r", stdin);
#endif

	scanf("%d%d", &N, &M);
	mx = 0;
	for (int i = 1; i <= N; i++)
	{
		scanf("%d", &O[i]);
		mx = max(mx, O[i]);
	}
	memset(R, 0, sizeof(R));
	for (int i = 1; i <= M; i++)
	{
		scanf("%d", &S[i]);
		R[S[i]] = 1;
		mx = max(mx, S[i]);
	}
	sort(O + 1, O + N + 1);
	sort(S + 1, S + M + 1);

	for (int i = 1; i <= mx; i++)
		R[i] += R[i - 1];
	tot = 0;
	for (int i = 1; i <= N; i++)
	{
		if (i == 1 || O[i] != O[i - 1] + 1)
			tot++, C[i] = tot, ll[tot] = rr[tot] = i;
		else if (O[i] == O[i - 1] + 1)
			C[i] = tot, rr[tot] = i;
	}
	memset(F, 0, sizeof(F));
	memset(D, 0, sizeof(D));
	for (int i = 1; i <= N; i++)
	{
		int j, k;
		for (j = 1; j <= M && S[j] <= O[i]; j++)
			if ((k = i - (O[i] - S[j])) > 0)
            {
                F[i] = max(F[i], D[ll[C[k]] - 1] + R[O[i]] - R[S[j] - 1]);
                // F[i] = max(F[i], D[k - 1] + R[O[i]] - R[S[j] - 1]);
            }
		D[i] = max(D[i], D[i - 1]);
		if (j - 1 && S[j - 1] == O[i]) j--;
		for (; j <= M && S[j] >= O[i]; j++)
			if ((k = i + (S[j] - O[i])) <= N)
            {
                D[rr[C[k]]] = max(D[rr[C[k]]], F[i] + R[S[j]] - R[O[i]]);
                // D[k] = max(D[k], F[i] + R[S[j]] - R[O[i]]);
            }

	}
	printf("%d\n", D[N]);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#define DEBUG 0

using namespace std;

/*
 * */
#define MAXN 100010

int N, M;
int S[MAXN], O[MAXN], mx;
int F[MAXN], D[MAXN], R[MAXN << 1];
int main()
{
#if DEBUG
	freopen("input_data", "r", stdin);
#endif

	scanf("%d%d", &N, &M);
	mx = 0;
	for (int i = 1; i <= N; i++)
	{
		scanf("%d", &O[i]);
		mx = max(mx, O[i]);
	}
	memset(R, 0, sizeof(R));
	for (int i = 1; i <= M; i++)
	{
		scanf("%d", &S[i]);
		R[S[i]] = 1;
		mx = max(mx, S[i]);
	}
	sort(O + 1, O + N + 1);
	sort(S + 1, S + M + 1);

	for (int i = 1; i <= mx; i++)
		R[i] += R[i - 1];

	memset(F, 0, sizeof(F));
	memset(D, 0, sizeof(D));
	for (int i = 1; i <= N; i++)
	{
		int j, k;
		for (j = 1; j <= M && S[j] <= O[i]; j++)
			if ((k = i - (O[i] - S[j]) - 1) >= 0)
				F[i] = max(F[i], D[k] + R[O[i]] - R[S[j] - 1]);
		D[i] = max(D[i], D[i - 1]);
		if (j - 1 && S[j - 1] == O[i]) j--;
		for (; j <= M && S[j] >= O[i]; j++)
			if ((k = i + (S[j] - O[i])) <= N)
				D[k] = max(D[k], F[i] + R[S[j]] - R[O[i]]);
	}
	printf("%d\n", D[N]);
	return 0;
}


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

#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define per(i,a,b) for(int i=int(a);i>=int(b);i--)
#define ms(a,v) memset(a,v,sizeof a)

int n,m,a[100005],b[2005],d[100005],z[100005],L[100005];

inline void upd(int &a,int b) { if(a<b) a=b; }

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    while(cin>>n>>m) {
        rep(i,1,n) cin>>a[i];
        rep(i,1,m) cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        ms(d,0),ms(z,0),ms(L,0);
        rep(i,1,n) {
            if(i>1 && a[i]-1==a[i-1]) L[i]=L[i-1]; else L[i]=i;
            int x=upper_bound(b+1,b+m+1,a[i])-b;
            upd(d[i],z[L[i]-1]);
            per(j,x-1,1) {
                if(i<=a[i]-b[j]) break;
                upd(d[i],z[L[i-a[i]+b[j]]-1]+x-j);
            }
            upd(z[i],max(z[i-1],d[i]));
            rep(j,x,m) {
                if(i+b[j]-a[i]>n) break;
                upd(z[i+b[j]-a[i]],d[i]+j-x+1);
            }
        }
        cout<<z[n]<<endl;
    }
    return 0;
}


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

#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define per(i,a,b) for(int i=int(a);i>=int(b);i--)
#define ms(a,v) memset(a,v,sizeof a)

int n,m,a[100005],b[2005],d[100005],z[100005],L[100005];

inline void upd(int &a,int b) { if(a<b) a=b; }

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    while(cin>>n>>m) {
        rep(i,1,n) cin>>a[i];
        rep(i,1,m) cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        ms(d,0),ms(z,0),ms(L,0);
        rep(i,1,n) {
            if(i>1 && a[i]-1==a[i-1]) L[i]=L[i-1]; else L[i]=i;
            int x=upper_bound(b+1,b+m+1,a[i])-b;
//            upd(d[i],z[L[i]-1]);
            per(j,x-(b[x]!=a[i]),1) {
                if(i<=a[i]-b[j]) break;
                upd(d[i],z[L[i-a[i]+b[j]]-1]+x-j);
            }
            upd(z[i],max(z[i-1],d[i]));
            rep(j,x,m) {
                if(i+b[j]-a[i]>n) break;
                upd(z[i+b[j]-a[i]],d[i]+j-x+1);
            }
        }
        cout<<z[n]<<endl;
    }
    return 0;
}


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

#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define per(i,a,b) for(int i=int(a);i>=int(b);i--)
#define ms(a,v) memset(a,v,sizeof a)

int n,m,a[100005],b[2005],d[100005],z[100005],L[100005];

inline void upd(int &a,int b) { if(a<b) a=b; }

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    while(cin>>n>>m) {
        rep(i,1,n) cin>>a[i];
        rep(i,1,m) cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        ms(d,0),ms(z,0),ms(L,0);
        rep(i,1,n) {
            if(i>1 && a[i]-1==a[i-1]) L[i]=L[i-1]; else L[i]=i;
            int x=upper_bound(b+1,b+m+1,a[i])-b;
//            upd(d[i],z[L[i]-1]);
            per(j,x,1) {
                if(i<=a[i]-b[j]) break;
                upd(d[i],z[L[i-a[i]+b[j]]-1]+x-j);
            }
            upd(z[i],max(z[i-1],d[i]));
            rep(j,x,m) {
                if(i+b[j]-a[i]>n) break;
                upd(z[i+b[j]-a[i]],d[i]+j-x+1);
            }
        }
        cout<<z[n]<<endl;
    }
    return 0;
}


/*
 * PROB: Codeforces - 436D - Pudding Monsters
 * LANG: C++
 */

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

#define INF 0x3f3f3f3f
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define ms(a,v) memset(a,v,sizeof a)

vector<int> mons,cell;
int n,m,d[100005],z[100005],lim;

int sum(int l,int r) {
    if(l>r) swap(l,r);
    vector<int>::iterator lo=lower_bound(cell.begin(),cell.end(),l);
    vector<int>::iterator hi=upper_bound(cell.begin(),cell.end(),r);
    return hi-lo;
}

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    while(cin>>n>>m) {
        mons.assign(n+1,0);
        cell.assign(m+1,0);
        lim=0;
        rep(i,1,n) cin>>mons[i],lim=max(lim,mons[i]);
        rep(i,1,m) cin>>cell[i],lim=max(lim,cell[i]);
        sort(mons.begin(),mons.end());
        sort(cell.begin(),cell.end());
//        cout<<*upper_bound(cell.begin(),cell.end(),7);
//        break;
        ms(z,0),ms(d,0);
        rep(i,1,n) {
            int x=upper_bound(cell.begin(),cell.end(),mons[i])-cell.begin();
            rep(j,1,x-1) {
                int r=mons[i],l=cell[j];
                d[i]=max(d[i],z[i-(r-l)]+sum(l,r));
            }
            rep(j,x,m) {
                int l=mons[i],r=cell[j];
                z[i+(r-l)]=max(z[i+(r-l)],d[i]+sum(l+1,r));
            }
            z[i]=max(z[i],z[i-1]);
        }
        cout<<z[n]<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    d[0][0] = (int)binary_search(q.begin(), q.end(), v[0]);
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind > 0) {
                d[0][i] = max(d[0][i], max(d[0][ind-1], d[1][ind-1]) + it-j);
            } else if (ind == 0) {
                d[0][i] = max(d[0][i], it-j);
            }
        }
        if (i) d[1][i] = max(d[0][i-1], d[1][i-1]);
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
    }
    cout << max(d[0][n-1], d[1][n-1]) << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    vi next(v.size());
    for (int i = 0; i < v.size(); ++i) {
        next[i] = i;
        if (i && v[i-1] + 1 == v[i]) {
            next[i] = next[i-1];
        }
    }
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind >= 0) ind = next[ind];
            if (ind > 0) d[0][i] = max(d[0][i], d[1][ind-1] + it-j);
            if (ind == 0) d[0][i] = max(d[0][i], it-j);
        }
        if (i) {
            d[1][i] = max(d[1][i], d[1][i-1]);
            d[0][i] = max(d[0][i], d[1][i-1]);
        }
        d[1][i] = max(d[1][i], d[0][i]);
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
//        cerr << d[0][i] << ' ' << d[1][i] << endl;
    }
    cout << d[1][n-1] << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    vi next(v.size());
    for (int i = 0; i < v.size(); ++i) {
        next[i] = i;
        if (i && v[i-1] + 1 == v[i]) {
            next[i] = next[i-1];
        }
    }
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind >= 0) ind = next[ind];
            if (ind > 0) d[0][i] = max(d[0][i], d[1][ind-1] + it-j);
            if (ind == 0) d[0][i] = max(d[0][i], it-j);
        }
        if (i) {
            d[1][i] = max(d[1][i], d[1][i-1]);
            if (next[i] > 0) d[0][i] = max(d[0][i], d[1][next[i]-1]);
        }
        d[1][i] = max(d[1][i], d[0][i]);
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
//        cerr << d[0][i] << ' ' << d[1][i] << endl;
    }
    cout << d[1][n-1] << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    d[0][0] = (int)binary_search(q.begin(), q.end(), v[0]);
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind > 0) {
                d[0][i] = max(d[0][i], max(d[0][ind-1], d[1][ind-1]) + it-j);
            } else if (ind == 0) {
                d[0][i] = max(d[0][i], it-j);
            }
        }
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
    }
    cout << max(d[0][n-1], d[1][n-1]) << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    vi next(v.size());
    for (int i = 0; i < v.size(); ++i) {
        next[i] = i;
        if (i && v[i-1] + 1 == v[i]) {
            next[i] = next[i-1];
        }
    }
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind >= 0) ind = next[ind];
            if (ind > 0) {
                d[0][i] = max(d[0][i], d[1][ind-1] + it-j);
            } else if (ind == 0) {
                d[0][i] = max(d[0][i], it-j);
            }
        }
        if (i) d[1][i] = max(d[1][i], d[1][i-1]);
        d[1][i] = max(d[1][i], d[0][i]);
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
//        cerr << d[0][i] << ' ' << d[1][i] << endl;
    }
    cout << d[1][n-1] << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    vi next(v.size());
    for (int i = 0; i < v.size(); ++i) {
        next[i] = i;
        if (i && v[i-1] + 1 == v[i]) {
            next[i] = next[i-1];
        }
    }
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    d[0][0] = (int)binary_search(q.begin(), q.end(), v[0]);
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind >= 0) ind = next[ind];
            if (ind > 0) {
                d[0][i] = max(d[0][i], max(d[0][ind-1], d[1][ind-1]) + it-j);
            } else if (ind == 0) {
                d[0][i] = max(d[0][i], it-j);
            }
        }
        if (i) d[1][i] = max(d[1][i], max(d[0][i-1], d[1][i-1]));
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
//        cerr << d[0][i] << ' ' << d[1][i] << endl;
    }
    cout << max(d[0][n-1], d[1][n-1]) << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;

int main() {
    int n, m;
    cin >> n >> m;
    vi v(n), q(m);
    for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
    for (int i = 0; i < m; ++i) scanf("%d", &q[i]);
    sort(v.begin(), v.end());
    vi next(v.size());
    for (int i = 0; i < v.size(); ++i) {
        next[i] = i;
        if (i && v[i-1] + 1 == v[i]) {
            next[i] = next[i-1];
        }
    }
    sort(q.begin(), q.end());
    vvi d(2, vi(n));
    for (int i = 0; i < n; ++i) {
        int it = upper_bound(q.begin(), q.end(), v[i]) - q.begin();
        d[0][i] = max(d[0][i], it);
        for (int j = 0; j < it; ++j) {
            int ind = i - (v[i] - q[j]);
            if (ind >= 0) ind = next[ind];
            if (ind > 0) d[0][i] = max(d[0][i], d[1][ind-1] + it-j);
        }
        if (i) d[1][i] = max(d[1][i], d[1][i-1]);
        d[1][i] = max(d[1][i], d[0][i]);
        int cnt = 0;
        for (int j = it; j < m; ++j) {
            ++cnt;
            int ind = i + q[j]-v[i];
            if (ind < n) d[1][ind] = max(d[1][ind], d[0][i]+cnt);
        }
//        cerr << d[0][i] << ' ' << d[1][i] << endl;
    }
    cout << d[1][n-1] << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		dp2[i] = max(dp2[i], dp[i-1]);
		dp2[i] = max(dp2[i], dp2[i-1]);
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		if(l[i] == i) dp[i] = max(dp[i], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << dp2[n+1] << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		dp[i] = dp[i-1];
		dp2[i] = max(dp2[i], dp[i-1]);
		dp2[i] = max(dp2[i], dp2[i-1]);
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		if(l[i] == i) dp[i] = max(dp[i], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << *max_element(dp2+1,dp2+n+2) << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 1e5 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		if(l[i] == i)
			dp[i] = max(dp[i], dp2[i]);
		dp[i+1] = max(dp[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << dp2[n+1] << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		dp2[i] = max(dp2[i], dp[i-1]);
		dp2[i] = max(dp2[i], dp2[i-1]);
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		if(l[i] == i) dp[i] = max(dp[i], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << dp2[n] << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		dp[i+1] = max(dp[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << dp2[n+1] << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 150000 + 5;

int l[N], r[N], n, m, a[N], b[N], dp[N], dp2[N], pre[N];

int take(int x, int y) {
	return pre[y] - pre[x-1];
}

int main() {
	scanf("%d %d", &n, &m);
	FOR(i, 1, n) {
		scanf("%d",&a[i]);
	}
	FOR(i, 1, m) {
		scanf("%d",&b[i]);
		pre[b[i]]++;
	}
	FOR(i, 1, N-1)
		pre[i] += pre[i-1];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	a[0] = -1; a[n+1] = -1;
	FOR(i, 1, n) {
		l[i] = i;
		if(a[i] - 1 == a[i-1])
			l[i] = l[i-1];
	}
	ROF(i, n, 1) {
		r[i] = i;
		if(a[i] + 1 == a[i+1])
			r[i] = r[i+1];
	}
	FOR(i, 1, n) {
		FOR(j, 1, m) {
			if(b[j] <= a[i] && i-a[i]+b[j] > 0 ) {
				int t = l[i-a[i]+b[j]];
				dp[i] = max(dp[i], dp2[t] + take(b[j], a[i]));
			}
		}
		dp[i+1] = max(dp[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp[i]);
		dp2[i+1] = max(dp2[i+1], dp2[i]);
		FOR(j, 1, m)
			if(b[j] >= a[i] && i+b[j]-a[i] <= n) {
				int t = i+(b[j]-a[i])+1;
				dp2[t] = max(dp2[t], dp[i] + take(a[i]+1, b[j]));
			}
	}
	cout << dp2[n+1] << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
#define N 100010
using namespace std;
int a[N], b[N], l[N], dp[N], dp0[N];
int n, m;

int main(){
	int i, j;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);

	for (i = 1; i <= m; ++ i)
		scanf("%d", &b[i]);
	sort(b + 1, b + 1 + m);
	l[1] = 1;
	for (i = 2; i <= n; ++ i)
		l[i] = a[i] == a[i - 1] ? l[i - 1] : 0;

	for (i = 1; i <= n; ++ i){
		int p = upper_bound(b + 1, b + 1 + m, a[i]) - b;
		if (i == l[i])
			dp[i] = max(dp[i], dp0[i - 1]);
		else
			dp[i] = max(dp[i], dp[i - 1]);
		for (j = 1; j < p; ++ j){
			int k = a[i] - b[j];
			if (k < i)
				dp[i] = max(dp[i], dp0[l[i - k] - 1] + p - j);
		}

		dp0[i] = max(dp0[i], dp[i]);
		for (j = p; j <= m; ++ j) {
			int k = b[j] - a[i];
			if (i + k <= n)
				dp0[i + k] = max(dp0[i + k], dp[i] + j - p + 1);
		}
		dp0[i + 1] = max(dp0[i + 1], dp0[i]);
	}
	printf("%d\n", dp0[n]);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
#define N 100010
using namespace std;
int a[N], b[N], l[N], dp[N], dp0[N];
int n, m;

int main(){
	int i, j;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);

	for (i = 1; i <= m; ++ i)
		scanf("%d", &b[i]);
	sort(b + 1, b + 1 + m);
	l[1] = 1;
	for (i = 2; i <= n; ++ i)
		l[i] = (a[i] == a[i - 1] + 1? l[i - 1] : i);

	for (i = 1; i <= n; ++ i){
		int p = upper_bound(b + 1, b + 1 + m, a[i]) - b - 1;
		if (i == l[i])
			dp[i] = max(dp[i], dp0[i - 1]);
		else
			dp[i] = max(dp[i], dp[i - 1]);
		//printf("dp === %d\n", dp[i]);
		for (j = 1; j <= p; ++ j){
			int k = a[i] - b[j];
			//printf("k == %d\n", k);
			if (k < i)
				dp[i] = max(dp[i], dp0[l[i - k] - 1] + p - j + 1);
		}

		dp0[i] = max(dp0[i], dp[i]);
		for (j = p + 1; j <= m; ++ j) {
			int k = b[j] - a[i];
			if (i + k <= n)
				dp0[i + k] = max(dp0[i + k], dp[i] + j - p);
		}
		dp0[i + 1] = max(dp0[i + 1], dp0[i]);
	}
	printf("%d\n", dp0[n]);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
#define N 100010
using namespace std;
int a[N], b[N], l[N], dp[N], dp0[N];
int n, m;

int main(){
	int i, j;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);

	for (i = 1; i <= m; ++ i)
		scanf("%d", &b[i]);
	sort(b + 1, b + 1 + m);
	l[1] = 1;
	for (i = 2; i <= n; ++ i)
		l[i] = a[i] == a[i - 1] ? l[i - 1] : i;

	for (i = 1; i <= n; ++ i){
		int p = upper_bound(b + 1, b + 1 + m, a[i]) - b - 1;
		//printf("p == %d\n", p);
		if (i == l[i])
			dp[i] = max(dp[i], dp0[i - 1]);
		else
			dp[i] = max(dp[i], dp[i - 1]);
		for (j = 1; j <= p; ++ j){
			int k = a[i] - b[j];
			//printf("k == %d\n", k);
			if (k < i)
				dp[i] = max(dp[i], dp0[l[i - k] - 1] + p - j + 1);
		}

		dp0[i] = max(dp0[i], dp[i]);
		for (j = p + 1; j <= m; ++ j) {
			int k = b[j] - a[i];
			if (i + k <= n)
				dp0[i + k] = max(dp0[i + k], dp[i] + j - p);
		}
		dp0[i + 1] = max(dp0[i + 1], dp0[i]);
	}
	//for (i = 1; i <= n; ++ i)  printf("%d %d\n", dp[i], dp0[i]);
	//printf("\n");
	printf("%d\n", dp0[n]);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
#define N 100010
using namespace std;
int a[N], b[N], l[N], dp[N], dp0[N];
int n, m;

int main(){
	int i, j;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);

	for (i = 1; i <= m; ++ i)
		scanf("%d", &b[i]);
	sort(b + 1, b + 1 + m);
	l[1] = 1;
	for (i = 2; i <= n; ++ i)
		l[i] = a[i] == a[i - 1] ? l[i - 1] : i;

	for (i = 1; i <= n; ++ i){
		int p = upper_bound(b + 1, b + 1 + m, a[i]) - b;
		if (i == l[i])
			dp[i] = max(dp[i], dp0[i - 1]);
		else
			dp[i] = max(dp[i], dp[i - 1]);
		for (j = 1; j < p; ++ j){
			int k = a[i] - b[j];
			if (k < i)
				dp[i] = max(dp[i], dp0[l[i - k] - 1] + p - j);
		}

		dp0[i] = max(dp0[i], dp[i]);
		for (j = p; j <= m; ++ j) {
			int k = b[j] - a[i];
			if (i + k <= n)
				dp0[i + k] = max(dp0[i + k], dp[i] + j - p + 1);
		}
		dp0[i + 1] = max(dp0[i + 1], dp0[i]);
	}
	printf("%d\n", dp0[n]);

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn= 1e6;
int a[maxn],b[maxn],head[maxn],f[maxn],g[maxn];
int i,j,n,m,now;
void update(int &a,int b){
	a= max(a,b);
}
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d %d",&n, &m);
	for (i= 1;i<=n;i++) scanf("%d",&a[i]);
	for (i= 1;i<=m;i++) scanf("%d",&b[i]);
	sort(a+1,a+n+1);sort(b+1,b+m+1);
	for (i= now= 1;i<=n;i++){
		head[i]= i>1&&a[i]==a[i-1]+1?head[i-1]:i;
		while (now<=m&&b[now]<=a[i])
			now++;
		update(f[i],g[head[i]-1]);
		for (j= now-1;j>0&&a[i]-b[j]+1<=i;j--)
			update(f[i],g[head[i-(a[i]-b[j])]-1]+now-j);
		update(g[i],f[i]);update(g[i],g[i-1]);
		for (j= now;j<=m&&b[j]-a[i]<=n-i;j++)
			update(g[i+b[j]-a[i]],f[i]+j-now+1);
	}printf("%d\n",g[n]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;

const int MaxN = 100000;
const int MaxM = 2000;

template <class T>
inline void relax(T &a, const T &b)
{
	if (b > a)
		a = b;
}

int main()
{
	int n, m;
	static int a[MaxN];
	static int b[MaxM];

	cin >> 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);

	static int head[MaxN];
	for (int i = 0, j; i < n; i++)
	{
		if (i == 0 || a[i - 1] + 1 != a[i])
			j = i;
		head[i] = j;
	}

	static int f[MaxN + 1], g[MaxN + 1];

	int b_p = 0;
	for (int i = 0; i < n; i++)
	{
		while (b_p < m && b[b_p] <= a[i])
			b_p++;
		f[i + 1] = g[i];
		for (int j = b_p - 1; j >= 0 && a[i] - b[j] <= i; j--)
			relax(f[i + 1], g[head[i - (a[i] - b[j])]] + (b_p - j));
		for (int j = b_p; j < m && i + b[j] - a[i] < n; j++)
			relax(g[i + b[j] - a[i] + 1], f[i + 1] + j - b_p + 1);
		relax(g[i + 1], g[i]);
	}

	cout << g[n] << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;

const int MaxN = 100000;
const int MaxM = 2000;

template <class T>
inline void relax(T &a, const T &b)
{
	if (b > a)
		a = b;
}

int main()
{
	int n, m;
	static int a[MaxN];
	static int b[MaxM];

	cin >> 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);

	static int head[MaxN];
	for (int i = 0, j; i < n; i++)
	{
		if (i == 0 || a[i - 1] + 1 != a[i])
			j = i;
		head[i] = j;
	}

	static int f[MaxN + 1], g[MaxN + 1];

	int b_p = 0;
	for (int i = 0; i < n; i++)
	{
		while (b_p < m && b[b_p] <= a[i])
			b_p++;
		f[i + 1] = g[i];
		for (int j = b_p - 1; j >= 0 && a[i] - b[j] <= i; j--)
			relax(f[i + 1], g[head[i - (a[i] - b[j])]] + (b_p - j));
		for (int j = b_p; j < m && i + b[j] - a[i] < n; j++)
			relax(g[i + b[j] - a[i] + 1], f[i + 1] + j - b_p + 1);
		relax(g[i + 1], f[i + 1]);
		relax(g[i + 1], g[i]);
	}

	cout << g[n] << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;

const int MaxN = 100000;
const int MaxM = 2000;

template <class T>
inline void relax(T &a, const T &b)
{
	if (b > a)
		a = b;
}

int main()
{
	int n, m;
	static int a[MaxN];
	static int b[MaxM];

	cin >> 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);

	static int head[MaxN];
	static int f[MaxN + 1], g[MaxN + 1];

	for (int i = 0, b_p = 0; i < n; i++)
	{
		head[i] = (i > 0 && a[i - 1] + 1 == a[i]) ? head[i - 1] : i;

		while (b_p < m && b[b_p] <= a[i])
			b_p++;
		f[i + 1] = g[head[i]];
		for (int j = b_p - 1; j >= 0 && a[i] - b[j] <= i; j--)
			relax(f[i + 1], g[head[i - (a[i] - b[j])]] + (b_p - j));

		relax(g[i + 1], f[i + 1]);
		for (int j = b_p; j < m && i + b[j] - a[i] < n; j++)
			relax(g[i + b[j] - a[i] + 1], f[i + 1] + j - b_p + 1);
		relax(g[i + 1], g[i]);
	}

	cout << g[n] << endl;

	return 0;
}