#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class PrimeSequences { public:
	int getLargestGenerator(int N, int D) 
	{
		int result = -1;

		vector<int> p(N+1, 1);
		p[0] = p[1] = 0;
		for(int q=2; q<=N; ++q)
			if( p[q] ) {
				if(q<=N/q)
					for(int qq=q*q; qq<=N; qq+=q)
						p[qq] = 0;
				p[q] = p[q/2]+1;
				if( p[q] >= D )
					result = q;
			}

		return result;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, PrimeSequences().getLargestGenerator(N, D));}
int main(){

CASE(0)
	int N = 10; 
	int D = 2; 
	int _ = 7; 
END
CASE(1)
	int N = 42; 
	int D = 3; 
	int _ = 23; 
END
CASE(2)
	int N = 666; 
	int D = 7; 
	int _ = -1; 
END
CASE(3)
	int N = 1337; 
	int D = 5; 
	int _ = 47; 
END
CASE(4)
	int N = 100000; 
	int D = 5; 
	int _ = 2879; 
END
CASE(5)
	int N = 40000; 
	int D = 1; 
	int _ = 39989; 
END
CASE(6)
	int N = 10000000; 
	int D = 10; 
	int _ = -1; 
END
CASE(7)
	int N = 2; 
	int D = 1; 
	int _ = 2; 
END
CASE(7)
	int N = 3; 
	int D = 1; 
	int _ = 3; 
END
CASE(7)
	int N = 4; 
	int D = 1; 
	int _ = 3; 
END
CASE(7)
	int N = 5; 
	int D = 1; 
	int _ = 5; 
END
CASE(7)
	int N = 6; 
	int D = 1; 
	int _ = 5; 
END
CASE(7)
	int N = 6; 
	int D = 2; 
	int _ = 5; 
END
CASE(7)
	int N = 6; 
	int D = 3; 
	int _ = -1; 
END
CASE(7)
	int N = 7; 
	int D = 2; 
	int _ = 7; 
END

}
// END CUT HERE
