#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <utility>
#include <assert.h>

#define MPI 3.141592653589793238462643
#define eps 1e-8
#define inf ((int)1e9)
#define pb push_back
#define mp make_pair

using namespace std;

//vector <int> V[1001000];
int Cnt[1001000], S[1001000], E[1001000];
int M[5010*5010/2];
int A[6000], n, k, ans;
int P[40], PP[40], e, ee;

int main()
{
  int i, cnt, j;
  //freopen(".in", "r", stdin);
  //freopen(".out", "w", stdout);
  scanf("%d%d", &n, &k);
  if (n<=k+1)
  {
    puts("1");
    return 0;
  }
  for (i=0; i<n; i++)
    scanf("%d", &A[i]);
  sort(A,A+n);
  //for (i=0; i<=A[n-1]; i++)
  //  V[i].reserve((A[n-1]*2)/n);
  for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
      Cnt[A[j]-A[i]]++;//V[A[j]-A[i]].pb(A[i]);
  for (i=0; i<=A[n-1]; i++)
    S[i+1]=S[i]+Cnt[i], E[i]=S[i];
  for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
      M[E[A[j]-A[i]]++]=A[i];
  //cerr<<clock()<<endl;
  ans=n-k;
  while (true)
  {
   // cerr<<ans<<endl;
    for (cnt=0, i=ans; i<=A[n-1]; i+=ans)
      cnt+=Cnt[i];//(int)V[i].size();
    if (cnt>(k*(k+1))/2)
    {
      ans++;
      continue;
    }
    e=0;
    for (i=ans; i<=A[n-1]; i+=ans)
      for (j=0; j<Cnt[i]; j++)
        P[e++]=M[S[i]+j], P[e++]=M[S[i]+j]+i;
    sort(P,P+e), e=unique(P,P+e)-P;
    for (ee=0, i=0; i<e; i++)
      PP[ee++]=P[i]%ans;
    sort(PP,PP+ee), ee=unique(PP,PP+ee)-PP;
    if (e-ee<=k)
    {
      printf("%d\n", ans);
     // cerr<<clock()<<endl;
      return 0;
    }
  //  cerr<<"!!!!  "<<ans<<endl;
    ans++;
  }
//  cerr<<clock()<<endl;
  return 0;
}


#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <utility>
#include <assert.h>

#define MPI 3.141592653589793238462643
#define eps 1e-8
#define inf ((int)1e9)
#define pb push_back
#define mp make_pair

using namespace std;

vector <int> V[1001000];
int A[6000], n, k, ans;
int P[40], PP[40], e, ee;

int main()
{
  int i, cnt, j;
  //freopen(".in", "r", stdin);
  //freopen(".out", "w", stdout);
  scanf("%d%d", &n, &k);
  if (n<=k+1)
  {
    puts("1");
    return 0;
  }
  for (i=0; i<n; i++)
    scanf("%d", &A[i]);
  sort(A,A+n);
  for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
      V[A[j]-A[i]].pb(A[i]);
  ans=n-k;
  while (true)
  {
   // cerr<<ans<<endl;
    for (cnt=0, i=ans; i<=A[n-1]; i+=ans)
      cnt+=(int)V[i].size();
    if (cnt>(k*(k+1))/2)
    {
      ans++;
      continue;
    }
    e=0;
    for (i=ans; i<=A[n-1]; i+=ans)
      for (j=0; j<(int)V[i].size(); j++)
        P[e++]=V[i][j], P[e++]=V[i][j]+i;
    sort(P,P+e), e=unique(P,P+e)-P;
    for (ee=0, i=0; i<e; i++)
      PP[ee++]=P[i]%ans;
    sort(PP,PP+ee), ee=unique(PP,PP+ee)-PP;
    if (e-ee<=k)
    {
      printf("%d\n", ans);
      return 0;
    }
  //  cerr<<"!!!!  "<<ans<<endl;
    ans++;
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <time.h>
#include <vector>

#if LOCAL
	#define DO_NOT_SEND
#endif

typedef long long LL;

int IntMaxVal = (int) 1e20;
int IntMinVal = (int) -1e20;
LL LongMaxVal = (LL) 1e20;
LL LongMinVal = (LL) -1e20;

#define FOR(i, a, b) for(int i = a; i < b ; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

template<typename T> inline void minimize(T &a, T b) { a = std::min(a, b); }
template<typename T> inline void maximize(T &a, T b) { a = std::max(a, b); }

#define all(v) v.begin(),v.end()

using namespace std;

#define endl '\n'
template<typename T> struct argument_type;
template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
#define next(t, i) argument_type<void(t)>::type i; cin >> i;
#define nextVector(t, v, size) vector< argument_type<void(t)>::type > v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

#define range(name, start, count) vector<int> name(count); { for (int i = 0 ; i < count ; i++) name[i] = i + start; }

template <typename T1, typename T2> istream& operator >>(istream& is, pair<T1, T2>& s) { is >> s.first >> s.second; return is; }
template <typename T> ostream& operator << (ostream& os, const vector<T> &v) { for (int i = 0 ; i < v.size() ; i++) os << v[i] << ' '; os << endl; return os; }
template <typename T1, typename T2> ostream& operator <<(ostream& s, pair<T1, T2>& t) { s << t.first << ' ' << t.second; return s; }
template <typename T> vector<T> readVector(int n) { vector<T> res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }

int main() {
	srand (time(NULL));
    ios_base::sync_with_stdio(false); cin.tie(NULL);

	next(int, n);
	next(int, k);
	auto a = readVector<int>(n);
	sort(all(a));

	vector<vector<int> > toRemove(1000001);
	for (auto &v : toRemove) v.reserve(5);

	FOR (i, 0, n) FOR (j, i + 1, n)
		if (toRemove[a[j] - a[i]].size() < 5)
		toRemove[a[j] - a[i]].push_back(a[i]);
	FOR (i, 1, toRemove.size()) {
		for (int m = i * 2 ; m < toRemove.size() ; m += i) {
			for (auto c : toRemove[m]) {
				bool contains = false;
				for (auto x : toRemove[i]) if (x == c) contains = true;
				if (!contains && toRemove[i].size() < 5)
				toRemove[i].push_back(c);
			}
		}
	}

	FOR (i, 1, toRemove.size()) if (toRemove[i].size() <= k) {
		cout << i;
		return 0;
	}
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <time.h>
#include <vector>

#if LOCAL
	#define DO_NOT_SEND
#endif

typedef long long LL;

int IntMaxVal = (int) 1e20;
int IntMinVal = (int) -1e20;
LL LongMaxVal = (LL) 1e20;
LL LongMinVal = (LL) -1e20;

#define FOR(i, a, b) for(int i = a; i < b ; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

template<typename T> inline void minimize(T &a, T b) { a = std::min(a, b); }
template<typename T> inline void maximize(T &a, T b) { a = std::max(a, b); }

#define all(v) v.begin(),v.end()

using namespace std;

#define endl '\n'
template<typename T> struct argument_type;
template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
#define next(t, i) argument_type<void(t)>::type i; cin >> i;
#define nextVector(t, v, size) vector< argument_type<void(t)>::type > v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

#define range(name, start, count) vector<int> name(count); { for (int i = 0 ; i < count ; i++) name[i] = i + start; }

template <typename T1, typename T2> istream& operator >>(istream& is, pair<T1, T2>& s) { is >> s.first >> s.second; return is; }
template <typename T> ostream& operator << (ostream& os, const vector<T> &v) { for (int i = 0 ; i < v.size() ; i++) os << v[i] << ' '; os << endl; return os; }
template <typename T1, typename T2> ostream& operator <<(ostream& s, pair<T1, T2>& t) { s << t.first << ' ' << t.second; return s; }
template <typename T> vector<T> readVector(int n) { vector<T> res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }

int main() {
	srand (time(NULL));
    ios_base::sync_with_stdio(false); cin.tie(NULL);

	next(int, n);
	next(int, k);
	auto a = readVector<int>(n);
	sort(all(a));

	vector<int*> toRemove(1000001);
	vector<int> cnt(toRemove.size());
	FOR (i, 0, toRemove.size()) {
		toRemove[i] = new int[5];
		toRemove[i][0] = toRemove[i][1] = toRemove[i][2] = toRemove[i][3] = toRemove[i][4] = -1;
	}

	int ans = IntMaxVal;
	FOR (i, 0, n) FOR (j, i + 1, n) {
		if (cnt[a[j] - a[i]] == 5) continue;
		toRemove[a[j] - a[i]][cnt[a[j] - a[i]]] = a[i];
		cnt[a[j] - a[i]]++;
	}
	FORD (i, toRemove.size() - 1, n - k) {
		for (int m = i * 2 ; m < toRemove.size() && cnt[i] <= k; m += i) {
			FOR (j, 0, cnt[m]) {
				int c = toRemove[m][j];
				bool contains = toRemove[i][0] == c ||
								toRemove[i][1] == c ||
								toRemove[i][2] == c ||
								toRemove[i][3] == c ||
								toRemove[i][4] == c;
				if (!contains && cnt[i] <= k) {
					toRemove[i][cnt[i]] = c;
					cnt[i]++;
				}
			}
		}
		if (cnt[i] <= k) ans = i;
	}
	cout << ans;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <time.h>
#include <vector>

#if LOCAL
	#define DO_NOT_SEND
#endif

typedef long long LL;

int IntMaxVal = (int) 1e20;
int IntMinVal = (int) -1e20;
LL LongMaxVal = (LL) 1e20;
LL LongMinVal = (LL) -1e20;

#define FOR(i, a, b) for(int i = a; i < b ; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

template<typename T> inline void minimize(T &a, T b) { a = std::min(a, b); }
template<typename T> inline void maximize(T &a, T b) { a = std::max(a, b); }

#define all(v) v.begin(),v.end()

using namespace std;

#define endl '\n'
template<typename T> struct argument_type;
template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
#define next(t, i) argument_type<void(t)>::type i; cin >> i;
#define nextVector(t, v, size) vector< argument_type<void(t)>::type > v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

#define range(name, start, count) vector<int> name(count); { for (int i = 0 ; i < count ; i++) name[i] = i + start; }

template <typename T1, typename T2> istream& operator >>(istream& is, pair<T1, T2>& s) { is >> s.first >> s.second; return is; }
template <typename T> ostream& operator << (ostream& os, const vector<T> &v) { for (int i = 0 ; i < v.size() ; i++) os << v[i] << ' '; os << endl; return os; }
template <typename T1, typename T2> ostream& operator <<(ostream& s, pair<T1, T2>& t) { s << t.first << ' ' << t.second; return s; }
template <typename T> vector<T> readVector(int n) { vector<T> res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }

int main() {
	srand (time(NULL));
    ios_base::sync_with_stdio(false); cin.tie(NULL);

	next(int, n);
	next(int, k);
	auto a = readVector<int>(n);
	sort(all(a));

	vector<int*> toRemove(1000001);
	vector<int> cnt(toRemove.size());
	FOR (i, 0, toRemove.size()) {
		toRemove[i] = new int[5];
		toRemove[i][0] = toRemove[i][1] = toRemove[i][2] = toRemove[i][3] = toRemove[i][4] = -1;
	}

	int ans = IntMaxVal;
	FOR (i, 0, n) FOR (j, i + 1, n) {
		if (cnt[a[j] - a[i]] == 5) continue;
		toRemove[a[j] - a[i]][cnt[a[j] - a[i]]] = a[i];
		cnt[a[j] - a[i]]++;
	}
	FOR (i, n - k, toRemove.size()) {
		for (int m = i * 2 ; m < toRemove.size() && cnt[i] <= k; m += i) {
			FOR (j, 0, cnt[m]) {
				int c = toRemove[m][j];
				bool contains = toRemove[i][0] == c ||
								toRemove[i][1] == c ||
								toRemove[i][2] == c ||
								toRemove[i][3] == c ||
								toRemove[i][4] == c;
				if (!contains && cnt[i] <= k) {
					toRemove[i][cnt[i]] = c;
					cnt[i]++;
				}
			}
		}
		if (cnt[i] <= k) {
			cout << i;
			return 0;
		}
	}
}


#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int q[3000000], a[200000], N, K, vis[3000000];
vector <pair<int, int> > E[2000000];
inline int abs(int x) { return (x > 0) ? x : (-x); }
int main()
{
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; ++i)    scanf("%d", &a[i]);
    for (int i = 1; i <= N; ++i)
        for (int j = i + 1; j <= N; ++j)
            E[abs(a[i] - a[j])].push_back(make_pair(i, j));
    for (int ans = 1; ans <= 1000000; ++ans)
    {
        int tmp = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i * ans <= 1000000; ++i)
        {
            for (int j = 0; j < E[ans * i].size(); ++j)
            {
                int x = E[ans * i][j].first, y = E[ans * i][j].second;
                if (!vis[x])    q[++tmp] = x, vis[x] = 1;
                if (!vis[y])    q[++tmp] = y, vis[y] = 1;
                if (tmp > K * 2) break;
            }
            if (tmp > K * 2) break;
        }
        if (tmp <= K * 2)
        {
            int del = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= tmp; ++i)
                if (!vis[i])
                {
                    ++del;
                    for (int j = i + 1; j <= tmp; ++j)
                        if (abs(a[q[i]] - a[q[j]]) % ans == 0) vis[j] = 1;
                }
            if (tmp - del <= K) { printf("%d\n", ans); return 0; }
        }
    }
}


#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int q[200], a[10000], N, K, vis[6000];
vector <pair<int, int> > E[2000000];
inline int abs(int x) { return (x > 0) ? x : (-x); }
int main()
{
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; ++i)    scanf("%d", &a[i]);
    for (int i = 1; i <= N; ++i)
        for (int j = i + 1; j <= N; ++j)
            E[abs(a[i] - a[j])].push_back(make_pair(i, j));
    for (int ans = 1; ans <= 1000000; ++ans)
    {
        int tmp = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i * ans <= 1000000; ++i)
        {
            for (int j = 0; j < E[ans * i].size(); ++j)
            {
                int x = E[ans * i][j].first, y = E[ans * i][j].second;
                if (!vis[x])    q[++tmp] = x, vis[x] = 1;
                if (!vis[y])    q[++tmp] = y, vis[y] = 1;
                if (tmp > K * 2) break;
            }
            if (tmp > K * 2) break;
        }
        if (tmp <= K * 2)
        {
            int del = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= tmp; ++i)
                if (!vis[i])
                {
                    ++del;
                    for (int j = i + 1; j <= tmp; ++j)
                        if (abs(a[q[i]] - a[q[j]]) % ans == 0) vis[j] = 1;
                }
            if (tmp - del <= K) { printf("%d\n", ans); return 0; }
        }
    }
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct POINT{
    int x, y, next;
}p[12500010];
int n, k, cnt, t, s;
int q[10];
int a[5010];
int H[1000010];
int vis[5010], Vis[10];
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for (int i = 1; i <= n-1; ++i)
    for (int j = i+1; j <= n; ++j)
    {
        ++cnt; p[cnt].x = i; p[cnt].y = j; p[cnt].next = H[abs(a[i]-a[j])]; H[abs(a[i]-a[j])] = cnt;
    }
    for (int ans = 1; ans <= 1000001; ++ans)
    {
        t = 0;
        memset(Vis, 0, sizeof(Vis));
        for (int i = 0; i <= 1000000/ans; ++i)
        {
            for (int now = H[ans*i]; now; now = p[now].next)
            {
                if (vis[p[now].x] != ans) { vis[p[now].x] = ans; if (++t > k*2) break; q[t] = p[now].x; }
                if (vis[p[now].y] != ans) { vis[p[now].y] = ans; if (++t > k*2) break; q[t] = p[now].y; }
            }
            if (t > k*2) break;
        }
        if (t <= k*2)
        {
            s = 0;
            memset(Vis, 0, sizeof(Vis));
            for (int i = 1; i <= t; ++i)
            if (!Vis[i])
            {
                ++s;
                for (int j = i+1; j <= t; ++j) if (abs(a[q[i]]-a[q[j]])%ans == 0) Vis[j] = ans;
            }
            if (t-s <= k) { printf("%d\n", ans); return 0; }
        }
    }
    return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int q[3000000], a[200000], N, K, vis[3000000];
vector <pair<int, int> > E[2000000];
inline int abs(int x) { return (x > 0) ? x : (-x); }
int main()
{
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; ++i)    scanf("%d", &a[i]);
    for (int i = 1; i <= N; ++i)
        for (int j = i + 1; j <= N; ++j)
            E[abs(a[i] - a[j])].push_back(make_pair(i, j));
    for (int ans = 1; ans <= 100000; ++ans)
    {
        int tmp = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i * ans <= 100000; ++i)
        {
            for (int j = 0; j < E[ans * i].size(); ++j)
            {
                int x = E[ans * i][j].first, y = E[ans * i][j].second;
                if (!vis[x])    q[++tmp] = x, vis[x] = 1;
                if (!vis[y])    q[++tmp] = y, vis[y] = 1;
            }
            if (tmp > K * 2) break;
        }
        if (tmp <= K * 2)
        {
            int del = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= tmp; ++i)
                if (!vis[i])
                {
                    ++del;
                    for (int j = i + 1; j <= tmp; ++j)
                        if (abs(a[q[i]] - a[q[j]]) % ans == 0) vis[j] = 1;
                }
            if (tmp - del <= K) { printf("%d\n", ans); return 0; }
        }
    }
}


#include <cstdio>
#include <cstring>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int maxn = 1200000, maxm = 5002 * 5002, maxk = 10;
int Head[maxn], Nxt[maxm], x[maxm], y[maxm], N, K, edges, a[maxn], vis[maxn], hash[maxn], ali[maxn],
    top, Index, head[maxk], nxt[maxk * maxk], with[maxk * maxk];
inline void add(int hd, int _x, int _y)
{
	Nxt[++Index] = Head[hd]; x[Index] = _x; y[Index] = _y; Head[hd] = Index;
}
inline void link(int x, int y)
{
	nxt[++edges] = head[x]; with[edges] = y; head[x] = edges;
}
inline int abs(int x) { return (x > 0) ? (x) : (-x); }
int main()
{
	scanf("%d%d", &N, &K);
	rep(i, 1, N) scanf("%d", &a[i]);
	memset(Head, -1, sizeof(Head));
	rep(i, 1, N) rep(j, i + 1, N) add(abs(a[i] - a[j]), i, j);
	for (int ans = 1; ; ans++)
	{
		top = 0; edges = 0;
		for (int i = 0; i <= 1000000; i += ans)
		{
			for (int j = Head[i]; j != -1 && top <= 2 * K; j = Nxt[j])
			{
				if (vis[x[j]] != ans) vis[x[j]] = ans, hash[x[j]] = ++top, head[top] = -1;
				if (vis[y[j]] != ans) vis[y[j]] = ans, hash[y[j]] = ++top, head[top] = -1;
			}
			if (top > 2 * K) break;
			for (int j = Head[i]; j != -1; j = Nxt[j])
				link(hash[x[j]], hash[y[j]]);
		}
		if (top > 2 * K) continue;
		int del = 0;
		rep(i, 1, top)
		if (ali[i] != ans)
		{
			ali[i] = ans;
			for (int j = head[i]; j != -1; j = nxt[j])
				if (ali[with[j]] != ans) ali[with[j]] = ans, del++;

		}
		if (del <= K) { printf("%d\n", ans); return 0; }
	}
    return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int q[3000000], a[200000], N, K, vis[3000000];
vector <pair<int, int> > E[2000000];
inline int abs(int x) { return (x > 0) ? x : (-x); }
int main()
{
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; ++i)    scanf("%d", &a[i]);
    for (int i = 1; i <= N; ++i)
        for (int j = i + 1; j <= N; ++j)
            E[abs(a[i] - a[j])].push_back(make_pair(i, j));
    for (int ans = 1; ans <= 100000; ++ans)
    {
        int tmp = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i * ans <= 100000; ++i)
        {
            for (int j = 0; j < E[ans * i].size(); ++j)
            {
                int x = E[ans * i][j].first, y = E[ans * i][j].second;
                if (!vis[x])    q[++tmp] = x, vis[x] = 1;
                if (!vis[y])    q[++tmp] = y, vis[y] = 1;
            }
            if (tmp > K * 2) break;
        }
        if (tmp <= K * 2)
        {
            int del = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= tmp; ++i)
                if (!vis[i])
                {
                    ++del;
                    for (int j = i + 1; j <= tmp; ++j)
                        if (abs(a[q[i]] - a[q[j]]) % ans == 0) vis[j] = 1;
                }
            if (tmp - del <= K) { printf("%d\n", ans); return 0; }
        }
    }
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001];
int ans=-1;
int inf=0;
void dp(int k,int l,int cnt)//�����l,��
{
	if(cnt+gem[k]<=maxn[k])
		return;
	maxn[k]=max(maxn[k],cnt+gem[k]);
	ans=max(ans,maxn[k]);
	if(l-1>0&&k+l-1<=inf) dp(k+l-1,l-1,maxn[k]);
	if(k+l<=inf) dp(k+l,l,maxn[k]);
	if(k+l+1<=inf)dp(k+l+1,l+1,maxn[k]);
}
int main()
{
	int n,d;
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(maxn,-1,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	dp(d,d,0);
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001];
bool vis[30001];
int ans = -1;
int inf = 0;
void dp(int k, int l, int cnt)//�����l,��
{
	if (cnt + gem[k] <= maxn[k] && vis[k])
		return;
	maxn[k] = cnt + gem[k];
	ans = max(ans, cnt + gem[k]);
	if (l - 1 > 0 && k + l - 1 <= inf) dp(k + l - 1, k + l - 1 - k, maxn[k]);
	if (k + l <= inf) dp(k + l, l, maxn[k]);
	if (k + l + 1 <= inf)dp(k + l + 1, l + 1, maxn[k]);
	vis[k] = 1;
}
int main()
{
	int n, d;
	cin >> n >> d;
	memset(vis, 0, sizeof(vis));
	memset(maxn, 0, sizeof(maxn));
	for (int i = 0; i < n; i++)
	{
		int k;
		cin >> k;
		gem[k]++;
		inf = k;
	}
	dp(d, d, 0);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001];
bool vis[30001];
int ans=-1;
int inf=0;
void dp(int k,int l,int cnt)//�����l,��
{
	if(cnt+gem[k]<=maxn[k]&&vis[k])
		return;
	maxn[k]=max(maxn[k],cnt+gem[k]);
	ans=max(ans,maxn[k]);
	if(l-1>0&&k+l-1<=inf) dp(k+l-1,l-1,maxn[k]);
	if(k+l<=inf) dp(k+l,l,maxn[k]);
	if(k+l+1<=inf)dp(k+l+1,l+1,maxn[k]);
	vis[k]=1;
}
int main()
{
	int n,d;
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(vis,0,sizeof(vis));
	memset(maxn,0,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	dp(d,d,0);
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int gem[30001];
int maxn[30001];
bool vis[30001];
int ans = -1;
int inf = 0;
void dp(int k, int l, int cnt)//�����l,��
{
	if (cnt + gem[k] <= maxn[k] && vis[k])
		return;
	maxn[k] = cnt + gem[k];
	ans = max(ans, cnt + gem[k]);
	if (l - 1 > 0 && k + l - 1 <= inf) dp(k + l - 1, k + l - 1 - k, maxn[k]);
	if (k + l <= inf) dp(k + l, l, maxn[k]);
	if (k + l + 1 <= inf)dp(k + l + 1, l + 1, maxn[k]);
	vis[k] = 1;
}
int main()
{
	int n, d;
	cin >> n >> d;
	memset(vis, 0, sizeof(vis));
	memset(maxn, 0, sizeof(maxn));
	for (int i = 0; i < n; i++)
	{
		int k;
		cin >> k;
		gem[k]++;
		inf = k;
	}
	dp(d, d, 0);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001][3001];
int inf=0;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l];
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	int n,d;
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(maxn,-1,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[40001][1001];
int inf=0;
int n,d;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l-d+500];
	cout<<res<<endl;
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	cout<<res+temp<<endl;
	return res+=temp;
}
int main()
{
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	for(int i=0;i<=30000;i++)
	for(int j=0;j<=1000;j++)
		maxn[i][j]=-1;
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001][501];
int inf=0;
int n,d;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l-d];
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(maxn,-1,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001][301];
int inf=0;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l-d];
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	int n,d;
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(maxn,-1,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[30001][501];
int inf=0;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l];
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	int n,d;
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	memset(maxn,-1,sizeof(maxn));
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[40001][1001];
int inf=0;
int n,d;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l-d+500];
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	for(int i=0;i<=30000;i++)
	for(int j=0;j<=1000;j++)
		maxn[i][j]=-1;
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int gem[30001];
int maxn[40001][1001];
int inf=0;
int n,d;
int dp(int pos,int l)//�����l,��
{
	if(pos>inf)return 0;
	int &res=maxn[pos][l-d+500];
	cout<<res<<endl;
	if(res!=-1)
		return res;
	res=gem[pos];
	int temp=0;
	for(int i=-1;i<=1;i++)
	if(l+i>0&&pos+l+i<=inf)
		temp=max(temp,dp(pos+l+i,l+i));
	return res+=temp;
}
int main()
{
	cin>>n>>d;
	memset(gem,0,sizeof(gem));
	for(int i=0;i<=30000;i++)
	for(int j=0;j<=1000;j++)
		maxn[i][j]=-1;
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		gem[k]++;
		inf=k;
	}
	cout<<dp(d,d)<<endl;;
	return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

ll dp[30005][1000];
int k[30005];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 300, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    ll ans = -1;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

int dp[30050][1801];
int k[30050];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 900, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    int ans = 0;
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                ans = max(ans, dp[i][j]);
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
   /* int ans = 0;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);*/
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

int dp[30050][1500];
int k[30050];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 500, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    int ans = 0;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

ll dp[30005][1000];
int k[30005];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 500, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    ll ans = -1;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

ll dp[30500][2000];
int k[30500];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 500, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    ll ans = 0;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

ll dp[30050][1500];
int k[30050];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 500, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    ll ans = 0;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<map>
#include<deque>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>

#define pb push_back
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;

int dp[30050][1801];
int k[30050];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x;
        k[x]++;
    }
    int t = 900, tt = 30001;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++)
            dp[i][j] = -1;
    dp[d][t] = k[d];
    for (int i = d; i < tt; i++)
        for (int j = 0; j < 2*t+1; j++){
            if (dp[i][j] != -1){
                if (i+j+d-t < tt)
                    dp[i+j+d-t][j] = max(dp[i+j+d-t][j], dp[i][j] + k[i+j+d-t]);
                if (i+j+d-t+1 < tt)
                    dp[i+j+d-t+1][j+1] = max(dp[i+j+d-t+1][j+1], dp[i][j] + k[i+j+d-t+1]);
                if (i+j+d-t-1 < tt)
                    dp[i+j+d-t-1][j-1] = max(dp[i+j+d-t-1][j-1], dp[i][j] + k[i+j+d-t-1]);
            }
        }
    int ans = 0;
    for (int i = 0; i < tt; i++)
        for (int j = 0; j < t+1; j++)
            ans = max(ans, dp[i][j]);
    cout << ans;
    return 0;
}


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

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30001

int v[mxx],d,m;

int main()
{
	int n,i,x;		m=0;
	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	//memset(dp,-1,sizeof(dp));

	//out(solve(d,d));

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<cstdlib>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 300010

int a[mxx],v[mxx],ans[mxx];
bool vis[mxx]={0};
queue<int> q;

int main()
{
	int n,d,i,x,cur,res=0,x1,m=0;

	inn(n,d);

	fo(i,n)			in(a[i]),v[a[i]]++,m=max(m,a[i]);

	q.push(d);		q.push(v[d]);		q.push(0);

	int pa;

	while(!q.empty())
	{
		x=q.front();		q.pop();		vis[x]=true;
		cur=q.front();		q.pop();

		pa=q.front();		q.pop();

		ans[x]=cur;

		//cout<<x<<"  "<<pa<<endl;

		res=max(res,cur);

		x1=x+x-pa-1;

		if(x1>=1 && x1<=m && ans[x1]<=cur+v[x1])
		if(vis[x1] && ans[x1]>=cur+v[x1] ) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa;

		if(x1>=1 && x1<=m && ans[x1]<=cur+v[x1])
		if(vis[x1] && ans[x1]>=cur+v[x1] ) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa+1;

		if(x1>=1 && x1<=m && ans[x1]<=cur+v[x1])
		if(vis[x1] && ans[x1]>=cur+v[x1] ) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		//break;
	}

	cout<<res<<endl;

	return 0;
}


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

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30001

int v[mxx],d,m;
int dp[mxx][500]={-1};

int solve(int i,int j)
{
	int jj=j-(d-250);        if(i>m)	return 0;

	if(dp[i][jj]!=-1)		return dp[i][jj];

	if(j==1)
		dp[i][jj]=v[i]+max(solve(i+j,j),solve(i+j+1,j+1));
	else
		dp[i][jj]=v[i]+max( solve(i+j-1,j-1) , max(solve(i+j,j),solve(i+j+1,j+1)) );

	return dp[i][jj];
}

int main()
{
	int n,i,x;		m=0;
	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	memset(dp,-1,sizeof(dp));

	out(solve(d,d));

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<cstdlib>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30010

int v[mxx],ans[mxx];
bool vis[mxx]={0};
queue<int> q;

int main()
{
	int n,d,i,x,cur,res=0,x1,m=0;

	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	q.push(d);		q.push(v[d]);		q.push(0);

	int pa;

	while(!q.empty())
	{
		x=q.front();		q.pop();		vis[x]=true;
		cur=q.front();		q.pop();

		pa=q.front();		q.pop();

		ans[x]=cur;

		//cout<<x<<"  "<<pa<<endl;

		res=max(res,cur);

		x1=x+x-pa-1;

		if(x1>=1 && x1<=m)
		if(!vis[x1])
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa;

		if(x1>=1 && x1<=m)
		if(!vis[x1])
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa+1;

		if(x1>=1 && x1<=m)
		if(!vis[x1])
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		//break;
	}

	cout<<res<<endl;

	return 0;
}


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

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30001

int v[mxx],d,m;
int dp[mxx][500]={-1};

int solve(int i,int j)
{
	int jj=j-(d-250);
	return 0;
}

int main()
{
	int n,i,x;		m=0;
	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	memset(dp,-1,sizeof(dp));

	out(solve(d,d));

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<cstdlib>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30001

int v[mxx],d,m;
int dp[mxx][500]={-1};

int solve(int i,int j)
{
	//	Reducing j in range d-250 to d+250

	//cout<<i<<"  "<<j<<endl;

	int jj=j-(d-250);

	if(i>m)	return 0;

	if(dp[i][jj]!=-1)		return dp[i][jj];

	if(j==1)
		dp[i][jj]=v[i]+max(solve(i+j,j),solve(i+j+1,j+1));
	else
		dp[i][jj]=v[i]+max( solve(i+j-1,j-1) , max(solve(i+j,j),solve(i+j+1,j+1)) );

	return dp[i][jj];
}

int main()
{
	int n,i,x;

	m=0;

	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	memset(dp,-1,sizeof(dp));

	out(solve(d,d));

	return 0;
}


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

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30001

int v[mxx],d,m;
int dp[mxx][500];

int solve(int i,int j)
{
	int jj=j-(d-250);        if(i>m)	return 0;

	if(dp[i][jj]!=-1)		return dp[i][jj];

	if(j==1)
		dp[i][jj]=v[i]+max(solve(i+j,j),solve(i+j+1,j+1));
	else
		dp[i][jj]=v[i]+max( solve(i+j-1,j-1) , max(solve(i+j,j),solve(i+j+1,j+1)) );

	return dp[i][jj];
}

int main()
{
	int n,i,x;		m=0;
	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	memset(dp,-1,sizeof(dp));

	int ans=solve(d,d);

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<cstdlib>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 300010

int a[mxx],v[mxx],ans[mxx];
bool vis[mxx]={0};
queue<int> q;

int main()
{
	int n,d,i,x,cur,res=0,x1,m=0;

	inn(n,d);

	fo(i,n)			in(a[i]),v[a[i]]++,m=max(m,a[i]);

	q.push(d);		q.push(v[d]);		q.push(0);

	int pa;

	while(!q.empty())
	{
		x=q.front();		q.pop();		vis[x]=true;
		cur=q.front();		q.pop();

		pa=q.front();		q.pop();

		ans[x]=cur;

		//cout<<x<<"  "<<pa<<endl;

		res=max(res,cur);

		x1=x+x-pa-1;

		if(x1>=1 && x1<=m)
		if(vis[x1]) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa;

		if(x1>=1 && x1<=m)
		if(vis[x1]) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		x1=x+x-pa+1;

		if(x1>=1 && x1<=m)
		if(vis[x1]) ;
		else
		{	q.push(x1);		q.push(cur+v[x1]);	  q.push(x);	}

		//break;
	}

	cout<<res<<endl;

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<cstdlib>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;

//	GC

#define in(a) scanf("%d",&a)
#define inn(a,b) scanf("%d%d",&a,&b)
#define inll(a) scanf("%lld",&a)
#define ins(a) scanf("%s",a)
#define out(a) printf("%d\n",a)
#define fo(i,n) for(i=0;i<n;i++)
#define pb push_back
#define outll(a) printf("%lld\n",a)
#define vec vector<int>
#define mod 1000000007
#define sortt(a) sort(a.begin(),a.end())
#define sz size()
#define mem(a,b) memset(a,(b),sizeof(a))
#define mp make_pair
#define ll long long
#define pah pair<int,int >
#define vecp vector<pa>
#define mx 300000
#define mxx 30010

int v[mxx],ans[mxx];
bool vis[mxx]={0};
queue<int> q;

int main()
{
	int n,d,i,x,cur,l,res=0,x1,m=0;

	inn(n,d);

	fo(i,n)			in(x),v[x]++,m=max(m,x);

	q.push(d);		q.push(v[d]);		q.push(0);

	int pa;

	while(!q.empty())
	{
		x=q.front();		q.pop();		vis[x]=true;
		cur=q.front();		q.pop();

		pa=q.front();		q.pop();

		ans[x]=cur;

		//cout<<x<<"  "<<pa<<endl;

		res=max(res,cur);

		l=x-pa;
		x1=x+l-1;

		if(l>=1 && l<=m)
		if(x1>=1 && x1<=m)
		if(!vis[x1] || cur+v[x1]>ans[x1])
		{	q.push(x1);		q.push(cur+v[x1]),vis[x1]=true;	  q.push(x);	}

		l=x-pa;
		x1=x+l;

		if(l>=1 && l<=m)
		if(x1>=1 && x1<=m)
		if(!vis[x1] || cur+v[x1]>ans[x1])
		{	q.push(x1);		q.push(cur+v[x1]),vis[x1]=true;	  q.push(x);	}

		l=x-pa;
		x1=x+l+1;

		if(l>=1 && l<=m)
		if(x1>=1 && x1<=m)
		if(!vis[x1] || cur+v[x1]>ans[x1])
		{	q.push(x1);		q.push(cur+v[x1]),vis[x1]=true;	  q.push(x);	}

		//break;
	}

	cout<<res<<endl;

	return 0;
}


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

int n,d;
int arr[30005];
int dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    if (l < 500) dp[curr][l] = ans;
    return ans;
}

int main()
{
    cin >> n >> d;
    int i,x;
    memset(dp,-1,sizeof(dp));
    for (i = 0; i < n; i++) {
        cin >> x;
        arr[x]++;
    }
    cout << func(d,d) << endl;
    //system("pause");
    return 0;
}


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

using namespace std;

int n,d;
short arr[30005];
short dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    if (l < 500) dp[curr][l] = ans;
    return ans;
}

int main()
{
    int i,x;
    memset(dp,-1,sizeof(dp));
    scanf("%d %d",&n,&d);
    for (i = 0; i < n; i++) {
        scanf("%d",&x);
        arr[x]++;
    }
    cout << func(d,d) << endl;
    //system("pause");
    return 0;
}


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

int n,d;
short arr[30005];
short dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    if (l < 500) dp[curr][l] = ans;
    return ans;
}

int main()
{
    cin >> n >> d;
    int i,x;
    memset(dp,-1,sizeof(dp));
    for (i = 0; i < n; i++) {
        cin >> x;
        arr[x]++;
    }
    cout << func(d,d) << endl;
   // system("pause");
    return 0;
}


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

int n,d;
int arr[30005];
int dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans = 0;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    return dp[curr][l] = ans;
}

int main()
{
    cin >> n >> d;
    int i,x;
    memset(dp,-1,sizeof(dp));
    for (i = 0; i < n; i++) {
        cin >> x;
        arr[x]++;
    }
    cout << func(d,d) << endl;
   // system("pause");
    return 0;
}


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

using namespace std;

int n,d;
int arr[30005];
int dp[30005][2000];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 2000 && dp[curr][l] != -1) return dp[curr][l];
    int ans;
    int a =func(curr+l-1,l-1);
    int b =func(curr+l,l);
    int c =func(curr+l+1,l+1);
    ans = max(a,max(b,c));
    ans += arr[curr];
    return dp[curr][l] = ans;
}

int main()
{
    int i,x;
    memset(dp,-1,sizeof(dp));
    scanf("%d %d",&n,&d);
    for (i = 0; i < n; i++) {
        scanf("%d",&x);
        arr[x]++;
    }
    cout << func(d,d) << endl;
   // system("pause");
    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int n,d;
int arr[30005];
int dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans = 0;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    return dp[curr][l] = ans;
}

int main()
{
    cin >> n >> d;
    int i,x;
    memset(dp,-1,sizeof(dp));
    for (i = 0; i < n; i++) {
        cin >> x;
        arr[x]++;
    }
    cout << func(d,d) << endl;
   // system("pause");
    return 0;
}


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

using namespace std;

int n,d;
short arr[30005];
short dp[30005][2000];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 2000 && dp[curr][l] != -1) return dp[curr][l];
    int ans;
    int a =func(curr+l-1,l-1);
    int b =func(curr+l,l);
    int c =func(curr+l+1,l+1);
    ans = max(a,max(b,c));
    ans += arr[curr];
    if (l < 2000) dp[curr][l] = ans;
    return ans;
}

int main()
{
    int i,x;
    memset(dp,-1,sizeof(dp));
    scanf("%d %d",&n,&d);
    for (i = 0; i < n; i++) {
        scanf("%d",&x);
        arr[x]++;
    }
    cout << func(d,d) << endl;
    //system("pause");
    return 0;
}


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

int n,d;
int arr[30005];
int dp[30005][500];

int func(int curr, int l)
{
    if (curr > 30000 || l <= 0) return 0;
    if (l < 500 && dp[curr][l] != -1) return dp[curr][l];
    int ans = 0;
    ans = arr[curr] + max(func(curr+l-1,l-1),max(func(curr+l,l),func(curr+l+1,l+1)));
    return dp[curr][l] = ans;
}

int main()
{
    cin >> n >> d;
    int i,x;
    memset(dp,-1,sizeof(dp));
    for (i = 0; i < n; i++) {
        cin >> x;
        arr[x]++;
    }
    cout << func(d,d) << endl;
    //system("pause");
    return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

int n, d;
int A[LMax];
int G[LMax];
vector<int> L[LMax];
int upperlimit = 0;

inline int maxim(int a, int b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (int i=d;i<=LMax-5;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (int j=0;j<L[i].size();j++) {
				int l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-5 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].resize(0);
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-5 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].resize(0);
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-5 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].resize(0);
						L[i+l].push_back(l);
					} else if (G[i+l] < G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	int sol = -1;
	for (int i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (int i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (int i=1;i<=n;i++) {
		int x; cin>>x;
		upperlimit = maxim(upperlimit, x);
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

short n, d;
short A[LMax];
short G[LMax];
vector<short> L[LMax];
short upperlimit = 0;

inline short maxim(short a, short b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (short i=d;i<=LMax-5;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (short j=0;j<L[i].size();j++) {
				short l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-5 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].erase(L[i+l-1].begin(), L[i+l-1].end());
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-5 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].erase(L[i+l+1].begin(), L[i+l+1].end());
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-5 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].erase(L[i+l].begin(), L[i+l].end());
						L[i+l].push_back(l);
					} else if (G[i+l] == G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	short sol = -1;
	for (short i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (short i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (short i=1;i<=n;i++) {
		short x; cin>>x;
		upperlimit = maxim(upperlimit, x);
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

int n, d;
int A[LMax];
int G[LMax];
int L[LMax];

inline int maxim(int a, int b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d] = d;
	for (int i=d;i<=LMax-5;i++) {
		if (G[i] != -1) {
			bool elone = true, eltwo = true,elthree = true;

			if (i+L[i]-1 <= LMax-5 && i+L[i]-1 >= 0) {
				if (G[i+L[i]-1] < G[i]+A[i+L[i]-1]) {
					G[i+L[i]-1] = G[i]+A[i+L[i]-1];
					L[i+L[i]-1] = L[i]-1;
				}
			} else
				elone = false;
			if (i+L[i]+1 <= LMax-5 && i+L[i]+1 >= 0) {
				if (G[i+L[i]+1] < G[i]+A[i+L[i]+1]) {
					G[i+L[i]+1] = G[i]+A[i+L[i]+1];
					L[i+L[i]+1] = L[i]+1;
				}
			} else
				eltwo = false;
			if (i+L[i] <= LMax-5 && i+L[i] >= 0) {
				if (G[i+L[i]] < G[i]+A[i+L[i]]) {
					G[i+L[i]] = G[i]+A[i+L[i]];
					L[i+L[i]] = L[i];
				}
			} else
				elthree = false;

			if (!elone && !eltwo && !elthree)
				break;
		}
	}

	int sol = -1;
	for (int i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (int i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (int i=1;i<=n;i++) {
		int x; cin>>x;
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


#include <cstring>
#include <iostream>

using namespace std;

int gems[30003], memo[30003][491], n, d;

int DP(int i, int c) {
    if (i > 30001 or !(d + c))
        return 0;
    int &ret = memo[i][c];
    if (ret == -1) {
        ret = gems[i] + max(max(DP(i + d + c - 1, c - 1), DP(i + d + c, c)), DP(i + d + c + 1, c + 1));
    }
    return ret;
}

int main() {
    memset(memo, -1, sizeof memo);

    cin >> n >> d;
    while (n--) {
        int tmp;
        cin >> tmp;
        gems[tmp]++;
    }

    cout << DP(d, 0);
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30001
using namespace std;

short n, d;
short A[LMax];
short G[LMax];
vector<short> L[LMax];
short upperlimit = 0;

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (short i=d;i<=LMax-1;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (short j=0;j<L[i].size();j++) {
				short l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-1 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].erase(L[i+l-1].begin(), L[i+l-1].end());
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-1 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].erase(L[i+l+1].begin(), L[i+l+1].end());
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-1 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].erase(L[i+l].begin(), L[i+l].end());
						L[i+l].push_back(l);
					} else if (G[i+l] == G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	short sol = -1;
	for (short i=d;i<=LMax-1;i++)
		if (G[i] > sol)
			sol = G[i];

	cout << sol <<endl;
}

void read() {
	for (short i=0;i<=LMax-1;i++)
		G[i] = -1;
	cin>>n>>d;
	for (short i=1;i<=n;i++) {
		short x; cin>>x;
		if (upperlimit < x)
			upperlimit = x;
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

short n, d;
short A[LMax];
short G[LMax];
vector<short> L[LMax];
short upperlimit = 0;

inline short maxim(short a, short b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (short i=d;i<=LMax-5;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (short j=0;j<L[i].size();j++) {
				short l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-5 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].resize(0);
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-5 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].resize(0);
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-5 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].resize(0);
						L[i+l].push_back(l);
					} else if (G[i+l] == G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	short sol = -1;
	for (short i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (short i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (short i=1;i<=n;i++) {
		short x; cin>>x;
		upperlimit = maxim(upperlimit, x);
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

int n, d;
int A[LMax];
int G[LMax];
int L[LMax];

inline int maxim(int a, int b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d] = d;
	for (int i=d;i<=LMax-5;i++) {
		if (G[i] != -1) {
			if (i+L[i]-1 <= LMax-5 && i+L[i]-1 >= 0) {
				if (G[i+L[i]-1] < G[i]+A[i+L[i]-1]) {
					G[i+L[i]-1] = G[i]+A[i+L[i]-1];
					L[i+L[i]-1] = L[i]-1;
				}
			}
			if (i+L[i]+1 <= LMax-5 && i+L[i]+1 >= 0) {
				if (G[i+L[i]+1] < G[i]+A[i+L[i]+1]) {
					G[i+L[i]+1] = G[i]+A[i+L[i]+1];
					L[i+L[i]+1] = L[i]+1;
				}
			}
			if (i+L[i] <= LMax-5 && i+L[i] >= 0) {
				if (G[i+L[i]] < G[i]+A[i+L[i]]) {
					G[i+L[i]] = G[i]+A[i+L[i]];
					L[i+L[i]] = L[i];
				}
			}
		}
	}

	int sol = -1;
	for (int i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (int i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (int i=1;i<=n;i++) {
		int x; cin>>x;
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30005
using namespace std;

int n, d;
int A[LMax];
int G[LMax];
vector<int> L[LMax];
int upperlimit = 0;

inline int maxim(int a, int b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (int i=d;i<=LMax-5;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (int j=0;j<L[i].size();j++) {
				int l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-5 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].resize(0);
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-5 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].resize(0);
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-5 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].resize(0);
						L[i+l].push_back(l);
					} else if (G[i+l] == G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	int sol = -1;
	for (int i=d;i<=LMax-5;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (int i=0;i<=LMax-5;i++)
		G[i] = -1;
	cin>>n>>d;
	for (int i=1;i<=n;i++) {
		int x; cin>>x;
		upperlimit = maxim(upperlimit, x);
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


// c
#include <iostream>
#include <vector>
#include <iomanip>
#define LMax 30001
using namespace std;

short n, d;
short A[LMax];
short G[LMax];
vector<short> L[LMax];
short upperlimit = 0;

inline short maxim(short a, short b) {
	if (a>b)
		return a;
	return b;
}

void solve() {
	G[d] = A[d];
	L[d].push_back(d);
	for (short i=d;i<=LMax-1;i++) {
		if (i > upperlimit)
			break;
		if (G[i] != -1) {
			for (short j=0;j<L[i].size();j++) {
				short l = L[i][j];
				bool elone = true, eltwo = true,elthree = true;

				if (i+l-1 <= LMax-1 && i+l-1 >= 0) {
					if (G[i+l-1] < G[i]+A[i+l-1]) {
						G[i+l-1] = G[i]+A[i+l-1];
						L[i+l-1].erase(L[i+l-1].begin(), L[i+l-1].end());
						L[i+l-1].push_back(l-1);
					} else if (G[i+l-1] == G[i]+A[i+l-1]) {
						L[i+l-1].push_back(l-1);
					}
				} else
					elone = false;
				if (i+l+1 <= LMax-1 && i+l+1 >= 0) {
					if (G[i+l+1] < G[i]+A[i+l+1]) {
						G[i+l+1] = G[i]+A[i+l+1];
						L[i+l+1].erase(L[i+l+1].begin(), L[i+l+1].end());
						L[i+l+1].push_back(l+1);
					} else if (G[i+l+1] == G[i]+A[i+l+1]) {
						L[i+l+1].push_back(l+1);
					}
				} else
					eltwo = false;
				if (i+l <= LMax-1 && i+l >= 0) {
					if (G[i+l] < G[i]+A[i+l]) {
						G[i+l] = G[i]+A[i+l];
						L[i+l].erase(L[i+l].begin(), L[i+l].end());
						L[i+l].push_back(l);
					} else if (G[i+l] == G[i]+A[i+l]) {
						L[i+l].push_back(l);
					}
				} else
					elthree = false;

				if (!elone && !eltwo && !elthree)
					break;
			}
		}
	}

	short sol = -1;
	for (short i=d;i<=LMax-1;i++)
		sol = maxim(sol, G[i]);

	cout << sol <<endl;
}

void read() {
	for (short i=0;i<=LMax-1;i++)
		G[i] = -1;
	cin>>n>>d;
	for (short i=1;i<=n;i++) {
		short x; cin>>x;
		upperlimit = maxim(upperlimit, x);
		A[x]++;
	}
}

int main() {

	read();
	solve();

	return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][1000];
int gems[30001];
map< pair<int, int>, int > m;

int doRec(int cur, int last) {
    if ((last < 1000 && dp[cur][last] != 0) || last == 2)
        return max(0, dp[cur][last]);
    pair<int, int> p = make_pair(cur, last);
    if(m.find(p) != m.end())
        return max(0, m[p]);
    int a, b, c;
    a = b = c = gems[cur];
    if (cur + last - 1 <= 30000 && last > 1)
        a += doRec(cur + last - 1, last - 1);
    if (cur + last <= 30000)
        b += doRec(cur + last, last);
    if (cur + last + 1 <= 30000)
        c += doRec(cur + last + 1, last + 1);
    if (last < 1000) {
        dp[cur][last] = max(max(a, b), c);
        if (dp[cur][last] == 0)
            dp[cur][last] = -1;
    }
    else {
        m[p] = max(max(a, b), c);
        if (m[p] == 0)
            m[p] = -1;
    }
    return max(0, max(a, max(b, c)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) {
        int gem;
        scanf("%d", &gem);
        gems[gem]++;
    }
    int sum = 0;
    for (int i = 30000; i >= 0; i--) {
        sum += gems[i];
        dp[i][2] = dp[i][1] = sum;
    }
    int ans = doRec(d, d);
    printf("%d", ans);
    return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][1000];
int gems[30001];
map<pair<int, int> > m;

int doRec(int cur, int last) {
    if ((last < 1000 && dp[cur][last] != 0) || last == 2)
        return max(0, dp[cur][last]);
    pair<int, int> p = make_pair(cur, last);
    if(m.find(p) != m.end())
        return max(0, m[p]);
    int a, b, c;
    a = b = c = gems[cur];
    if (cur + last - 1 <= 30000 && last > 1)
        a += doRec(cur + last - 1, last - 1);
    if (cur + last <= 30000)
        b += doRec(cur + last, last);
    if (cur + last + 1 <= 30000)
        c += doRec(cur + last + 1, last + 1);
    if (last < 1000) {
        dp[cur][last] = max(max(a, b), c);
        if (dp[cur][last] == 0)
            dp[cur][last] = -1;
    }
    else {
        m[p] = max(max(a, b), c);
        if (m[p] == 0)
            m[p] = -1;
    }
    return max(0, max(a, max(b, c)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) {
        int gem;
        scanf("%d", &gem);
        gems[gem]++;
    }
    int sum = 0;
    for (int i = 30000; i >= 0; i--) {
        sum += gems[i];
        dp[i][2] = dp[i][1] = sum;
    }
    int ans = doRec(d, d);
    printf("%d", ans);
    return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][4000];
int gems[30001];

int doRec(int cur, int last) {
    if (last < 4000 && dp[cur][last] != 0 || last == 2)
        return max(0, dp[cur][last]);
    int a, b, c;
    a = b = c = gems[cur];
    if (cur + last - 1 <= 30000 && last > 1)
        a += doRec(cur + last - 1, last - 1);
    if (cur + last <= 30000)
        b += doRec(cur + last, last);
    if (cur + last + 1 <= 30000)
        c += doRec(cur + last + 1, last + 1);
    if (last < 4000) {
        dp[cur][last] = max(max(a, b), c);
        if (dp[cur][last] == 0)
            dp[cur][last] = -1;
    }
    return max(0, max(a, max(b, c)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) {
        int gem;
        scanf("%d", &gem);
        gems[gem]++;
    }
    int sum = 0;
    for (int i = 30000; i >= 0; i--) {
        sum += gems[i];
        dp[i][2] = dp[i][1] = sum;
    }
    int ans = doRec(d, d);
    printf("%d", ans);
    return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][5000];
int gems[30001];

int doRec(int cur, int last) {
    if (last < 5000 && dp[cur][last] != 0 || last == 2)
        return max(0, dp[cur][last]);
    int a, b, c;
    a = b = c = gems[cur];
    if (cur + last - 1 <= 30000 && last > 1)
        a += doRec(cur + last - 1, last - 1);
    if (cur + last <= 30000)
        b += doRec(cur + last, last);
    if (cur + last + 1 <= 30000)
        c += doRec(cur + last + 1, last + 1);
    if (last < 5000) {
        dp[cur][last] = max(max(a, b), c);
        if (dp[cur][last] == 0)
            dp[cur][last] = -1;
    }
    return max(0, max(a, max(b, c)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) {
        int gem;
        scanf("%d", &gem);
        gems[gem]++;
    }
    int sum = 0;
    for (int i = 30000; i >= 0; i--) {
        sum += gems[i];
        dp[i][2] = dp[i][1] = sum;
    }
    int ans = doRec(d, d);
    printf("%d", ans);
    return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][15000];
int gems[30001];

int doRec(int cur, int last) {
	if (dp[cur][last] != 0 || last == 2)
		return max(0, dp[cur][last]);
	int a, b, c;
	a = b = c = gems[cur];
	if (cur + last - 1 <= 30000 && last > 1)
		a += doRec(cur + last - 1, last - 1);
	if (cur + last <= 30000)
		b += doRec(cur + last, last);
	if (cur + last + 1 <= 30000)
		c += doRec(cur + last + 1, last + 1);
	dp[cur][last] = max(max(a, b), c);
	if (dp[cur][last] == 0)
		dp[cur][last] = -1;
	return max(0, dp[cur][last]);
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		int gem;
		scanf("%d", &gem);
		gems[gem]++;
	}
	int sum = 0;
	for (int i = 30000; i >= 0; i--) {
		sum += gems[i];
		dp[i][2] = dp[i][1] = sum;
	}
	int ans = doRec(d, d);
	cout << max(ans, 0);
	return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

map< pair<int, int>, int > m;
int gems[30001];

int doRec(int cur, int last) {
	pair<int, int> p = make_pair(cur, last);
	if (m.find(p) != m.end())
		return m[p];
	int a = 0, b = 0, c = 0;
	if (cur + last - 1 <= 30000 && last > 1)
		a = doRec(cur + last - 1, last - 1) + gems[cur + last - 1];
	if (cur + last <= 30000)
		b = doRec(cur + last, last) + gems[cur + last];
	if (cur + last + 1 <= 30000)
		c = doRec(cur + last + 1, last + 1) + gems[cur + last + 1];
	m[p] = max(max(a, b), c);
	return m[p];
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		int gem;
		scanf("%d", &gem);
		gems[gem]++;
	}
	cout << doRec(d, d) + gems[d];
	return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][5000];
int gems[30001];

int doRec(int cur, int last) {
	if (last < 5000 && dp[cur][last] != 0 || last == 2)
		return max(0, dp[cur][last]);
	int a, b, c;
	a = b = c = gems[cur];
	if (cur + last - 1 <= 30000 && last > 1)
		a += doRec(cur + last - 1, last - 1);
	if (cur + last <= 30000)
		b += doRec(cur + last, last);
	if (cur + last + 1 <= 30000)
		c += doRec(cur + last + 1, last + 1);
	if (last < 5000) {
		dp[cur][last] = max(max(a, b), c);
		if (dp[cur][last] == 0)
			dp[cur][last] = -1;
	}
	return max(0, max(a, max(b, c)));
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		int gem;
		scanf("%d", &gem);
		gems[gem]++;
	}
	int sum = 0;
	for (int i = 30000; i >= 0; i--) {
		sum += gems[i];
		dp[i][2] = dp[i][1] = sum;
	}
	int ans = doRec(d, d);
	printf("%d", ans);
	return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][1000];
int gems[30001];

int doRec(int cur, int last) {
	if (last < 1000 && dp[cur][last] != 0 || last == 2)
		return max(0, dp[cur][last]);
	int a, b, c;
	a = b = c = gems[cur];
	if (cur + last - 1 <= 30000 && last > 1)
		a += doRec(cur + last - 1, last - 1);
	if (cur + last <= 30000)
		b += doRec(cur + last, last);
	if (cur + last + 1 <= 30000)
		c += doRec(cur + last + 1, last + 1);
	if (last < 1000) {
		dp[cur][last] = max(max(a, b), c);
		if (dp[cur][last] == 0)
			dp[cur][last] = -1;
	}
	return max(0, max(a, max(b, c)));
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		int gem;
		scanf("%d", &gem);
		gems[gem]++;
	}
	int sum = 0;
	for (int i = 30000; i >= 0; i--) {
		sum += gems[i];
		dp[i][2] = dp[i][1] = sum;
	}
	int ans = doRec(d, d);
	printf("%d", ans);
	return 0;
}


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

#define MOD 1000000007
#define INF 2000000000

using namespace std;

int dp[30001][3000];
int gems[30001];

int doRec(int cur, int last) {
    if (last < 3000 && dp[cur][last] != 0 || last == 2)
        return max(0, dp[cur][last]);
    int a, b, c;
    a = b = c = gems[cur];
    if (cur + last - 1 <= 30000 && last > 1)
        a += doRec(cur + last - 1, last - 1);
    if (cur + last <= 30000)
        b += doRec(cur + last, last);
    if (cur + last + 1 <= 30000)
        c += doRec(cur + last + 1, last + 1);
    if (last < 3000) {
        dp[cur][last] = max(max(a, b), c);
        if (dp[cur][last] == 0)
            dp[cur][last] = -1;
    }
    return max(0, max(a, max(b, c)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) {
        int gem;
        scanf("%d", &gem);
        gems[gem]++;
    }
    int sum = 0;
    for (int i = 30000; i >= 0; i--) {
        sum += gems[i];
        dp[i][2] = dp[i][1] = sum;
    }
    int ans = doRec(d, d);
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int gt[MX][500];
bool ok[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    int cur = g + gem[vt];
    ok[vt][l] = 1;
    gt[vt][l] = cur;
    ans = max(cur,ans);
    if (cur + sum[vt+1] <= ans) return 0;
    For(i,-1,1){
        int v = vt + d + l + i;
        if (v > 30000 || v <= vt) continue;
        dfs(v,l+i,cur);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    ms(gt,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,0,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
map<int , map< int, bool> > ok;
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000) return 0;
    if (ok[vt][l]) return 0;
    g += gem[vt];
    ans = max(g,ans);
    ok[vt][l] = 1;
    if (sum[vt+1] == 0) return 0;
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ok.clear();
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000) return 0;
    g += gem[vt];
    ans = max(g,ans);
    if (sum[vt+1] == 0) return 0;
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int gt[MX][500];
bool ok[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000 || l > 30000) return 0;
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    g += gem[vt];
    ok[vt][l] = 1;
    gt[vt][l] = g;
    ans = max(g,ans);
    if (g + sum[vt+1] <= ans) return 0;
    For(i,-1,1){
        int v = vt + d + l + i;
        if (v > 30000 || v <= vt) continue;
        dfs(v,l+i,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,0,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
map<int , map< int, bool> > ok;
map<int , map< int, int> > gt;
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000 || l > 30000) return 0;
    g += gem[vt];
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    ok[vt][l] = 1;
    gt[vt][l] = g;
    ans = max(g,ans);
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ok.clear();
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}