#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,i,now,u,v;
struct node{int x,y,z,u,v;}a[N];
int cmp1(const node&a,const node&b){return a.x<b.x || a.x==b.x && a.y>b.y;}
int cmp2(const node&a,const node&b){return a.z<b.z;}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++) scanf("%d%d",&a[i].x,&a[i].y),a[i].z=i;
    sort(a+1,a+m+1,cmp1);
    now=1; u=1; v=2;
    for (i=1;i<=m;i++)
        if (a[i].y) a[i].u=1,a[i].v=++now;
        else
        {
            if (++u==v) u=2,v++;
            if (v>now) {now=0; break;}  else a[i].u=u,a[i].v=v;
        }
    if (!now) printf("-1\n");
    else
    {
        sort(a+1,a+m+1,cmp2);
        for (i=1;i<=m;i++) printf("%d %d\n",a[i].u,a[i].v);
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,i,now,u,v;
struct node{int x,y,z,u,v;}a[N];
int cmp1(const node&a,const node&b){return a.x<b.x || a.x==b.x && a.y>b.y;}
int cmp2(const node&a,const node&b){return a.z<b.z;}
int main()
{
    freopen("335B.in","r",stdin);
    freopen("335B.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++) scanf("%d%d",&a[i].x,&a[i].y),a[i].z=i;
    sort(a+1,a+m+1,cmp1);
    now=1; u=1; v=2;
    for (i=1;i<=m;i++)
        if (a[i].y) a[i].u=1,a[i].v=++now;
        else
        {
            if (++u==v) u=2,v++;
            if (v>now) {now=0; break;}  else a[i].u=u,a[i].v=v;
        }
    if (!now) printf("-1\n");
    else
    {
        sort(a+1,a+m+1,cmp2);
        for (i=1;i<=m;i++) printf("%d %d\n",a[i].u,a[i].v);
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,i,now,u,v;
struct node{int x,y,z,u,v;}a[N];
int cmp1(const node&a,const node&b){return a.x<b.x || a.x==b.x && a.y==1;}
int cmp2(const node&a,const node&b){return a.z<b.z;}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++) scanf("%d%d",&a[i].x,&a[i].y),a[i].z=i;
    sort(a+1,a+m+1,cmp1);
    now=1; u=1; v=2;
    for (i=1;i<=m;i++)
        if (a[i].y) a[i].u=1,a[i].v=++now;
        else
        {
            if (++u==v) u=2,v++;
            if (v>now) {now=0; break;}  else a[i].u=u,a[i].v=v;
        }
    if (!now) printf("-1\n");
    else
    {
        sort(a+1,a+m+1,cmp2);
        for (i=1;i<=m;i++) printf("%d %d\n",a[i].u,a[i].v);
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,i,now,u,v;
struct node{int x,y,z,u,v;}a[N];
int cmp1(const node&a,const node&b){return a.x<b.x || a.x==b.x && a.x==1;}
int cmp2(const node&a,const node&b){return a.z<b.z;}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++) scanf("%d%d",&a[i].x,&a[i].y),a[i].z=i;
    sort(a+1,a+m+1,cmp1);
    now=1; u=1; v=2;
    for (i=1;i<=m;i++)
        if (a[i].y) a[i].u=1,a[i].v=++now;
        else
        {
            if (++u==v) u=2,v++;
            if (v>now) {now=0; break;}  else a[i].u=u,a[i].v=v;
        }
    if (!now) printf("-1\n");
    else
    {
        sort(a+1,a+m+1,cmp2);
        for (i=1;i<=m;i++) printf("%d %d\n",a[i].u,a[i].v);
    }
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define Each(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define sqr(x) ((x) * (x))
#define Debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 1e18;
const double PI = acos((double) -1);
inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }
inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }
inline int fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

using namespace std;

struct Item
{
	int x, y, id;
	Item() {}
	Item(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
	operator<(const Item p)
	{
		if (x == p.x) return y > p.y;
		else return x < p.x;
	}
};

int n, m;
Item a[N];
pair<int, int> ans[N];

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort(a, a + m);
	int num = 0;
	for (int i = 0; i < m ; i++)
	{
		if (a[i].y) num++;
		if (num == n - 1) break;
		if (i == 0 && !a[i].y)
		{
			cout << -1;
			return 0;
		}
		else
		if (a[i].x > a[i-1].x && !a[i].y)
		{
			cout << -1;
			return 0;
		}
	}
	int z = 1, x = 3, y = 2;
	num = 0;
	for (int i = 0; i < m; i++)
		if (a[i].y)
		{
			ans[a[i].id].first = 1;
			ans[a[i].id].second = ++z;
			num++;
		}
		else
		{
			if (num < n - 1 && x > z)
			{
				cout << -1;
				return 0;
			}
			ans[a[i].id].first = x;
			ans[a[i].id].second = y;
			y++;
			if (y == x)
			{
				x++;
				y = 2;
			}
		}
	for (int i = 0; i < m; i++)
		cout << ans[i].first << " " << ans[i].second << endl;
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define Each(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define sqr(x) ((x) * (x))
#define Debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 1e18;
const double PI = acos((double) -1);
inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }
inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }
inline int fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

using namespace std;

int n, m;
pair<int, int> a[N];

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	if (m < n - 1)
	{
		cout << -1;
		return 0;
	}
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].first >> a[i].second;
	}
	sort (a, a + m);
	for (int i = 0; i < n - 1; i++)
		if (!a[i].second)
		{
			cout << -1;
			return 0;
		}
	int x = 1, y = 2;
	for (int i = 0; i < m; i++)
	{
		cout << x << " " << y << endl;
		y++;
		if (y > n)
		{
			x++;
			y = x + 1;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define Each(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define sqr(x) ((x) * (x))
#define Debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 1e18;
const double PI = acos((double) -1);
inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }
inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }
inline int fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

using namespace std;

struct Item
{
	int x, y, id;
	Item() {}
	Item(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
	operator<(const Item p)
	{
		if (x == p.x) return y > p.y;
		else return x < p.x;
	}
};

int n, m;
Item a[N];
pair<int, int> ans[N];

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	if (m < n - 1)
	{
		cout << -1;
		return 0;
	}
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort (a, a + m);
	for (int i = 0; i < n - 1; i++)
		if (!a[i].y)
		{
			cout << -1;
			return 0;
		}
	int x = 1, y = 2;
	for (int i = 0; i < n - 1; i++)
	{
		ans[a[i].id].first = 1;
		ans[a[i].id].second = i + 2;
	}
	x = 2, y = 3;
	for (int i = 0; i < m; i++)
		if (!a[i].y)
		{
			ans[a[i].id].first = x;
			ans[a[i].id].second = y;
			y++;
			if (y > x)
			{
				x++;
				y = x + 1;
			}
		}
	for (int i = 0; i < m; i++)
		cout << ans[i].first << " " << ans[i].second << endl;
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define Each(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define sqr(x) ((x) * (x))
#define Debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 1e18;
const double PI = acos((double) -1);
inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }
inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }
inline int fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

using namespace std;

struct Item
{
	int x, y, id;
	Item() {}
	Item(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
	operator<(const Item p)
	{
		if (x == p.x) return y < p.y;
		else return x < p.x;
	}
};

int n, m;
Item a[N];
pair<int, int> ans[N];

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	if (m < n - 1)
	{
		cout << -1;
		return 0;
	}
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort (a, a + m);
	for (int i = 0; i < n - 1; i++)
		if (!a[i].y)
		{
			cout << -1;
			return 0;
		}
	int x = 1, y = 2;
	for (int i = 0; i < n - 1; i++)
	{
		ans[a[i].id].first = 1;
		ans[a[i].id].second = i + 2;
	}
	x = 2, y = 3;
	for (int i = 0; i < m; i++)
		if (!a[i].y)
		{
			ans[a[i].id].first = x;
			ans[a[i].id].second = y;
			y++;
			if (y > x)
			{
				x++;
				y = x + 1;
			}
		}
	for (int i = 0; i < m; i++)
		cout << ans[i].first << " " << ans[i].second << endl;
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define Each(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define sqr(x) ((x) * (x))
#define Debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 1e18;
const double PI = acos((double) -1);
inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }
inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }
inline int fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

using namespace std;

struct Item
{
	int x, y, id;
	Item() {}
	Item(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
	operator<(const Item p)
	{
		if (x == p.x) return y > p.y;
		else return x < p.x;
	}
};

int n, m;
Item a[N];
pair<int, int> ans[N];

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].x >> a[i].y;
		a[i].id = i;
	}
	sort(a, a + m);
	int num = 0;
	int z = 1, x = 3, y = 2;
	num = 0;
	for (int i = 0; i < m; i++)
		if (a[i].y)
		{
			ans[a[i].id].first = 1;
			ans[a[i].id].second = ++z;
			num++;
		}
		else
		{
			if (num < n - 1 && x > z)
			{
				cout << -1;
				return 0;
			}
			ans[a[i].id].first = x;
			ans[a[i].id].second = y;
			y++;
			if (y == x)
			{
				x++;
				y = 2;
			}
		}
	for (int i = 0; i < m; i++)
		cout << ans[i].first << " " << ans[i].second << endl;
	return 0;
}


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

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define forn(low,high,i) for(i = low;i < high;i++)
#define forrev(high,low,i) for(i = high; i >= low;i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define line cout << __LINE__;
#define prv(a) for(auto x : a) cout << x << ' ';cout << '\n';
#define prvp(a) for(auto x : a) cout << "{" << x.ff << ',' << x.ss << "}";cout << '\n';
#define decimal_digits cout << fixed << setprecision(15);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

typedef unordered_map<int,int> umi;
typedef unordered_map<ll,ll> uml;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;

inline int add(int a, int b){a += b; if(a >= mod) a -= mod; return a;}
inline int sub(int a, int b){a -= b; if(a < 0) a += mod; return a;}
inline int mul(int a, int b){return (int)((long long) a * b % mod);}
inline int modexpo(int a, int b){int res = 1; while(b > 0){ if(b & 1) res = mul(res, a); a = mul(a, a); b /= 2;} return res;}
inline int divide(int a, int b){ return mul(a, modexpo(b, mod - 2));}

clock_t time_p = clock();

void ktj(){
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p)/CLOCKS_PER_SEC << "\n";
}

void pre(){ // Reset each global variable (esp. for graphs)

}

void solve(){
  int n, m;
  cin >> n >> m;
  vpii taken, not_taken;
  for(int i = 1; i <= m; i++){
    int w, b; cin >> w >> b;
    if(b){
      taken.push_back(mp(w, i));
    }else{
      not_taken.push_back(mp(w, i));
    }
  }
  sort(all(taken));
  sort(all(not_taken));

  vpii graph(m);

  if(not_taken.size()){
    if(taken.back().ff <= not_taken[0].ff){
      for(int i = 1; i <= (int)taken.size(); i++){
        int idx = taken[i - 1].ss;
        graph[idx - 1] = mp(i, i + 1);
      }
      int l = 1, r = 3;
      for(int i = 1; i <= (int)not_taken.size(); i++){
        int idx = not_taken[i - 1].ss;
        graph[idx - 1].ff = l;
        graph[idx - 1].ss = r;
        r++;
        if(r > n){
          l++; r = l + 2;
        }
      }
      for(int i = 0; i < m; i++){
        cout << graph[i].ff << ' ' << graph[i].ss << '\n';
      }
    }else{
      cout << -1 << '\n'; return;
    }
  }else{
    for(int i = 1; i <= (int)taken.size(); i++){
      int idx = taken[i - 1].ss;
      graph[idx - 1] = mp(i, i + 1);
    }
    for(int i = 0; i < m; i++){
      cout << graph[i].ff << ' ' << graph[i].ss << '\n';
    }
  }
}

int main(){
  fastio

  pre();

  int t = 1;
  // cin >> t;
  while(t--){
    solve();
  }

  ktj();
}


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 4];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k + 1 < sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 4 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vi v;
int f[2][maxm << 1][maxm << 1];

int calc1(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(va[k]);
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(va[k]);
	}
	v.pb(0); v.pb((maxn << 1) - 1);
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) {
			v.pb(mp(va[k - 1], r[k - 1]));
		}
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
//	FOR(i, 0, sz(va)) {
//		cout<<va[i]<<" "<<r[i]<<"\n";
//	}
//	cout<<"\n";
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
//			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k && b[i] <= r[k]) v.pb(mp(va[k], r[k]));
		else if (k - 1 >= 0) {
			v.pb(mp(va[k - 1], r[k - 1]));
		}
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k && b[i] >= r[k]) v.pb(mp(va[k], r[k]));
		else if (k - 1 >= 0) {
			v.pb(mp(va[k - 1], r[k - 1]));
		}
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

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

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

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 3];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		ms(f[0][0], 0); ms(f[0][1], 0);
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(r, r + sz(va), b[i]) - r - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 3];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k + 1< sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 3 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vi v;
int f[2][maxm << 1][maxm << 1];

int calc1(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1 || a[i] != a[i + 1] - 1) {
			va.pb(a[i]);
		}
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(va[k]);
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(va[k]);
	}
	v.pb(0); v.pb((maxn << 1) - 1);
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", max(f[0][0][0], f[1][0][0]));
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 8];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k + 3 < sz(va)) v.pb(mp(va[k + 3], r[k + 3]));
		if (k + 2 < sz(va)) v.pb(mp(va[k + 2], r[k + 2]));
		if (k + 1 < sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
		if (k - 3 >= 0) v.pb(mp(va[k - 3], r[k - 3]));
		if (k - 4 >= 0) v.pb(mp(va[k - 4], r[k - 4]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 8 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", max(f[0][cnt][0], f[1][cnt][0]));
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vi v;
int f[2][maxm << 1][maxm << 1];

int calc1(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	FOR(i, 0, m) {
		int k = upper_bound(a, a + n, b[i]) - a - 1;
		if (~k) v.pb(a[k]);
		k = lower_bound(a, a + n, b[i]) - a;
		if (k < n) v.pb(a[k]);
	}
	v.pb(0); v.pb((maxn << 1) - 1);
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(j, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", max(f[0][0][0], f[1][0][0]));
}

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


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

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

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

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

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		if (k + 1 < sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm << 1][maxm << 1];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
//	FOR(i, 0, sz(va)) {
//		cout<<va[i]<<" "<<r[i]<<"\n";
//	}
//	cout<<"\n";
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vi v;
int f[2][maxm << 1][maxm << 1];

int calc1(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(va[k]);
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(va[k]);
	}
	v.pb(0); v.pb((maxn << 1) - 1);
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", max(f[0][0][0], f[1][0][0]));
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 6];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k + 2 < sz(va)) v.pb(mp(va[k + 2], r[k + 2]));
		if (k + 1 < sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
		if (k - 3 >= 0) v.pb(mp(va[k - 3], r[k - 3]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 6 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 3];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 3 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][3 * maxm];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vii va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(mp(a[i], a[i]));
		}
		va.back().se = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), mp(b[i], INF)) - va.begin();
		if (k < sz(va)) v.pb(va[k]);
		if (k - 1 >= 0) v.pb(va[k - 1]);
		if (k - 2 >= 0) v.pb(va[k - 2]);
	}
	v.pb(mp(0, 0)); v.pb(mp(2 * maxn - 1, 2 * maxn - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		ms(f[cnt ^ 1], 0);
		FOR(j, 0, i + 1) {
			setmax(f[cnt ^ 1][0][j], f[cnt][0][j]);
			if (j) {
				setmax(f[cnt ^ 1][0][j], f[cnt][0][i + 1] + calc1(i, j));
				setmax(f[cnt ^ 1][0][j], f[cnt][1][i + 1] + calc1(i, j));
			}
			setmax(f[cnt ^ 1][1][j], f[cnt][1][j]);
			if (i < sz(v) - 1) {
				setmax(f[cnt ^ 1][1][j], f[cnt][0][i + 1] + calc2(i, j));
				setmax(f[cnt ^ 1][1][j], f[cnt][1][i + 1] + calc2(i, j));
				setmax(f[cnt ^ 1][1][j], f[cnt][0][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[cnt][1][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vi v;
int f[2][maxm << 1][maxm << 1];

int calc1(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i], y = v[j];
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	FOR(i, 0, m) {
		int k = upper_bound(a, a + n, b[i]) - a - 1;
		if (~k) v.pb(a[k]);
		k = lower_bound(a, a + n, b[i]) - a;
		if (k < n) v.pb(a[k]);
	}
	v.pb(0); v.pb((maxn << 1) - 1);
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i]] + (v[i] ? d[v[i] - 1] : 0));
		}
	}
	printf("%d", max(f[0][0][0], f[1][0][0]));
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][maxm * 8];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin();
		if (k + 3 < sz(va)) v.pb(mp(va[k + 3], r[k + 3]));
		if (k + 2 < sz(va)) v.pb(mp(va[k + 2], r[k + 2]));
		if (k + 1 < sz(va)) v.pb(mp(va[k + 1], r[k + 1]));
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) v.pb(mp(va[k - 1], r[k - 1]));
		if (k - 2 >= 0) v.pb(mp(va[k - 2], r[k - 2]));
		if (k - 3 >= 0) v.pb(mp(va[k - 3], r[k - 3]));
		if (k - 4 >= 0) v.pb(mp(va[k - 4], r[k - 4]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		FOR(j, 0, 8 * maxm) f[0][cnt ^ 1][j] = f[1][cnt ^ 1][j] = 0;
		FOR(j, 0, i + 1) {
			setmax(f[0][cnt ^ 1][j], f[0][cnt][j]);
			if (j) {
				setmax(f[0][cnt ^ 1][j], f[0][cnt][i + 1] + calc1(i, j));
				setmax(f[0][cnt ^ 1][j], f[1][cnt][i + 1] + calc1(i, j));
			}
			setmax(f[1][cnt ^ 1][j], f[1][cnt][j]);
			if (i < sz(v) - 1) {
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[1][cnt][i + 1] + calc2(i, j));
				setmax(f[1][cnt ^ 1][j], f[0][cnt][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[1][cnt][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][2][3 * maxm];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vii va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(mp(a[i], a[i]));
		}
		va.back().se = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), mp(b[i], INF)) - va.begin();
		if (k < sz(va)) v.pb(va[k]);
		if (k - 1 >= 0) v.pb(va[k - 1]);
		if (k - 2 >= 0) v.pb(va[k - 2]);
	}
	v.pb(mp(2 * maxn - 1, 2 * maxn - 1));
	sort(all(v)); uni(v);
	int cnt = 0;
	FORd(i, sz(v), 0) {
		ms(f[cnt ^ 1], 0);
		FOR(j, 0, i + 1) {
			setmax(f[cnt ^ 1][0][j], f[cnt][0][j]);
			if (j) {
				setmax(f[cnt ^ 1][0][j], f[cnt][0][i + 1] + calc1(i, j));
				setmax(f[cnt ^ 1][0][j], f[cnt][1][i + 1] + calc1(i, j));
			}
			setmax(f[cnt ^ 1][1][j], f[cnt][1][j]);
			if (i < sz(v) - 1) {
				setmax(f[cnt ^ 1][1][j], f[cnt][0][i + 1] + calc2(i, j));
				setmax(f[cnt ^ 1][1][j], f[cnt][1][i + 1] + calc2(i, j));
				setmax(f[cnt ^ 1][1][j], f[cnt][0][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
			}
		}
		cnt ^= 1;
	}
	printf("%d", f[cnt][1][0]);
}

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


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

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

const int maxn = 100010;
const int maxm = 2010;
int n, m;
int a[maxn];
int b[maxm];
int c[maxn << 1];
int d[maxn << 1];
vii v;
int f[2][maxm * 3][maxm * 3];
int r[maxn];

int calc1(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[y + e - 1] - (y ? d[y - 1] : 0);
}

int calc2(int i, int j) {
	int x = v[i].se, y = v[j].fi;
	int e = c[x] - (y ? c[y - 1] : 0);
	return d[x] - (x >= e ? d[x - e] : 0);
}

void solve() {
	scanf("%d%d", &n, &m);
	FOR(i, 0, n) {
		scanf("%d", a + i);
		c[a[i]]++;
	}
	FOR(i, 0, m) {
		scanf("%d", b + i);
		d[b[i]]++;
	}
	sort(a, a + n); sort(b, b + m);
	FOR(i, 1, maxn << 1) {
		c[i] += c[i - 1];
		d[i] += d[i - 1];
	}
	vi va;
	FOR(i, 0, n) {
		if (!i || a[i] > a[i - 1] + 1) {
			va.pb(a[i]);
		}
		r[sz(va) - 1] = a[i];
	}
	FOR(i, 0, m) {
		int k = upper_bound(all(va), b[i]) - va.begin() - 1;
		if (~k) v.pb(mp(va[k], r[k]));
		if (k - 1 >= 0) {
			v.pb(mp(va[k - 1], r[k - 1]));
		}
		k = lower_bound(all(va), b[i]) - va.begin();
		if (k < sz(va)) v.pb(mp(va[k], r[k]));
	}
	v.pb(mp(0, 0)); v.pb(mp((maxn << 1) - 1, (maxn << 1) - 1));
	sort(all(v)); uni(v);
	FORd(i, sz(v), 0) FOR(j, 0, i + 1) {
		setmax(f[0][i][j], f[0][i + 1][j]);
		if (j) {
			setmax(f[0][i][j], f[0][i + 1][i + 1] + calc1(i, j));
			setmax(f[0][i][j], f[1][i + 1][i + 1] + calc1(i, j));
		}
		setmax(f[1][i][j], f[1][i + 1][j]);
		if (i < sz(v) - 1) {
			setmax(f[1][i][j], f[0][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[1][i + 1][i + 1] + calc2(i, j));
			setmax(f[1][i][j], f[0][i + 1][i] + calc2(i, j) - d[v[i].se] + (v[i].fi ? d[v[i].fi - 1] : 0));
		}
	}
	printf("%d", f[1][0][0]);
}

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


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

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

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

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

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