#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


bool isPrime(const int& x)
{
	if(x == 2) return true;
	if(x % 2 == 0 || x < 2) return false;

	for(int i = 3; i*i <= x; i += 2)
	{
		if(x % i == 0)
		{
			return false;
		}
	}

	return true;
}

int ans[] = {9973219, 9950539, 9950209, 9933619, 9923989, 9917059, 9910759, 9892039, 9876109, 9835759, 9792709, 9783889, 9778039, 9774469, 9750079, 9744409, 9743479, 9739909, 9733819, 9725959, 9723019, 9721429, 9640489, 9627469, 9617989, 9600589, 9600559, 9589429, 9585889, 9550399, 9549109, 9541999, 9538099, 9511429, 9508069, 9503959, 9494209, 9479419, 9463249, 9446869, 9440449, 9439279, 9426499, 9394969, 9369559, 9365599, 9353299, 9324439, 9320329, 9299449, 9291649, 9280549, 9276859, 9220249, 9165259, 9154969, 9146989, 9138139, 9127639, 9083029, 9081889, 9081469, 9020989, 8926459, 8914189, 8898319, 8878279, 8868529, 8844379, 8833249, 8831989, 8827549, 8799619, 8773159, 8748589, 8741149, 8729059, 8717749, 8691589, 8685379, 8677399, 8669629, 8658109, 8648839, 8623039, 8621869, 8619439, 8616919, 8604649, 8594689, 8576599, 8573449, 8568409, 8561809, 8557729, 8540509, 8519479, 8513179, 8497039, 8487379, 8467639, 8460589, 8445979, 8431069, 8385109, 8381539, 8348539, 8344159, 8339629, 8336599, 8326609, 8318419, 8313919, 8312419, 8281579, 8248069, 8208919, 8194939, 8183899, 8178259, 8166079, 8108539, 8101789, 8070919, 8062009, 8059069, 8023669, 7989469, 7961959, 7960159, 7945039, 7934659, 7913239, 7882879, 7879849, 7861039, 7846549, 7814299, 7813369, 7806439, 7802239, 7774309, 7754899, 7734829, 7703239, 7701769, 7681369, 7669819, 7659019, 7645669, 7641379, 7610929, 7599049, 7583509, 7580389, 7545919, 7541929, 7540459, 7537339, 7534609, 7534069, 7527259, 7499209, 7483789, 7477999, 7475389, 7465489, 7464559, 7448479, 7433059, 7422229, 7417519, 7371649, 7369339, 7367029, 7356079, 7348639, 7340239, 7335949, 7328779, 7316299, 7305379, 7286689, 7273999, 7267459, 7235959, 7201639, 7187779, 7177069, 7166149, 7161859, 7159519, 7151149, 7130869, 7097809, 7081189, 7079719, 7051399, 7045699, 7039849, 7027459, 7025659, 7016749, 7014619, 6949339, 6945559, 6942499, 6937969, 6925159, 6918349, 6918019, 6915199, 6900919, 6884749, 6880879, 6843379, 6796639, 6787639, 6760009, 6758959, 6753829, 6740479, 6735019, 6727549, 6715999, 6715279, 6708859, 6700699, 6699619, 6692269, 6655639, 6655549, 6641359, 6631909, 6621289, 6616789, 6613339, 6602209, 6601789, 6600499, 6595579, 6582469, 6578659, 6561019, 6556609, 6528679, 6512089, 6510199, 6509689, 6503599, 6501799, 6498229, 6495949, 6484069, 6474829, 6471139, 6464209, 6438199, 6419299, 6406579, 6404779, 6402169, 6378139, 6359239, 6334879, 6332869, 6329509, 6324559, 6299149, 6297589, 6287929, 6261379, 6250429, 6213799, 6201499, 6193309, 6187459, 6177889, 6156979, 6153319, 6144379, 6136219, 6130549, 6106489, 6102919, 6066589, 6056209, 6054289, 6024049, 6023659, 6016099, 6013159, 6005899, 5986039, 5977339, 5969659, 5938789, 5937859, 5930929, 5928829, 5922229, 5908459, 5892709, 5860249, 5851429, 5841469, 5839579, 5805259, 5774149, 5760109, 5734699, 5733529, 5732149, 5698579, 5675149, 5651749, 5614159, 5610469, 5527009, 5499079, 5484139, 5465359, 5461999, 5450119, 5436289, 5425759, 5417389, 5382109, 5381869, 5327899, 5318899, 5278579, 5274679, 5274169, 5261749, 5253769, 5251789, 5248099, 5239189, 5234689, 5229409, 5205469, 5201299, 5184799, 5168929, 5158039, 5154769, 5146489, 5074879, 5073379, 5047129, 5037919, 5025079, 4997389, 4977439, 4972069, 4959049, 4956829, 4950139, 4900459, 4875259, 4868659, 4852459, 4833139, 4832419, 4809949, 4796089, 4743709, 4740649, 4734679, 4707979, 4706419, 4693699, 4675249, 4660549, 4647289, 4635979, 4627879, 4615609, 4609309, 4606579, 4596079, 4579879, 4575289, 4561639, 4544209, 4543249, 4540099, 4529389, 4510489, 4487809, 4467019, 4453489, 4415449, 4413589, 4405699, 4404559, 4396789, 4377679, 4370089, 4361479, 4353319, 4336099, 4332619, 4326409, 4316779, 4300189, 4285669, 4265719, 4265089, 4262179, 4257529, 4234429, 4224379, 4218289, 4208629, 4185919, 4174609, 4157899, 4155919, 4144969, 4138249, 4110349, 4098469, 4092709, 4059199, 4058479, 4042609, 4039669, 4032409, 4016569, 4015939, 3996529, 3995449, 3975709, 3974689, 3974359, 3951559, 3919249, 3919219, 3894049, 3854119, 3849829, 3837859, 3837139, 3817819, 3798079, 3741169, 3735169, 3727729, 3713449, 3690529, 3669769, 3652939, 3621469, 3593509, 3586909, 3584929, 3550699, 3518329, 3514549, 3513079, 3512989, 3512239, 3512059, 3479899, 3467539, 3437719, 3436249, 3416059, 3400219, 3378049, 3374479, 3371449, 3366829, 3340879, 3328909, 3326629, 3308959, 3295549, 3277699, 3243349, 3224869, 3215749, 3208819, 3207439, 3200209, 3187609, 3171739, 3157579, 3153589, 3129619, 3103279, 3101479, 3092569, 3089329, 3076399, 3066829, 3063499, 3062209, 3058879, 3047419, 2990839, 2989039, 2954689, 2952799, 2927809, 2927599, 2918569, 2893489, 2879299, 2875339, 2849689, 2846869, 2840269, 2839939, 2822719, 2805169, 2790259, 2764129, 2761729, 2759299, 2731909, 2731699, 2728549, 2715289, 2712379, 2704909, 2696929, 2683789, 2679499, 2674549, 2673529, 2668249, 2635489, 2614069, 2604739, 2596669, 2594989, 2594959, 2576599, 2564329, 2561269, 2552659, 2552119, 2546239, 2541949, 2535109, 2508049, 2499949, 2479669, 2478529, 2470339, 2458669, 2443789, 2423419, 2418679, 2413429, 2403889, 2381089, 2376169, 2373409, 2362279, 2342779, 2340259, 2303599, 2294059, 2290039, 2289649, 2266639, 2262979, 2256349, 2248249, 2246149, 2225059, 2223679, 2203969, 2202799, 2193889, 2177509, 2176639, 2168659, 2159239, 2157739, 2144509, 2143489, 2141809, 2136139, 2116579, 2107669, 2087389, 2084449, 2083519, 2079199, 2075839, 2065579, 2063779, 2062009, 2050339, 2020729, 2016409, 2007919, 2007619, 2002339, 1979899, 1979149, 1959319, 1954369, 1954159, 1943659, 1925389, 1917739, 1915939, 1912459, 1912069, 1910269, 1904479, 1902619, 1890529, 1857679, 1837399, 1834039, 1833529, 1830349, 1819849, 1802659, 1798639, 1797379, 1791739, 1780489, 1764229, 1755829, 1749499, 1748479, 1747729, 1727779, 1718869, 1711819, 1691869, 1685449, 1681879, 1678759, 1670569, 1659109, 1653109, 1631059, 1627609, 1621729, 1616809, 1615849, 1603339, 1592869, 1588759, 1584439, 1577299, 1573939, 1573549, 1571749, 1540969, 1525969, 1524079, 1514329, 1508629, 1468639, 1461409, 1451839, 1447009, 1440589, 1410979, 1402369, 1400809, 1381279, 1368469, 1360789, 1358809, 1352209, 1351249, 1340329, 1339909, 1337269, 1329709, 1324579, 1322599, 1322179, 1298119, 1285519, 1272289, 1257079, 1246369, 1246249, 1233439, 1230379, 1228399, 1210879, 1182289, 1173589, 1172539, 1172029, 1170139, 1168249, 1167709, 1163719, 1157839, 1156039, 1155619, 1146799, 1137889, 1126669, 1122139, 1121839, 1120549, 1118869, 1117819, 1117609, 1100839, 1093999, 1093069, 1091269, 1068709, 1068259, 1065019, 1063969, 1043599, 1027759, 1023949, 1022509, 1022389, 1015369, 1008859, 1006339, 1006309, 1003369, 1002349, 983449, 978079, 976309, 959479, 958549, 954979, 946669, 938059, 930079, 907399, 889879, 881479, 876019, 875269, 857959, 855739, 854929, 845989, 837079, 833719, 829729, 823729, 822169, 814069, 803449, 795799, 773029, 768199, 739399, 736369, 734479, 715159, 705169, 691729, 681259, 680299, 666439, 664669, 663589, 661099, 657499, 654169, 633799, 633469, 632329, 632089, 626629, 622249, 607309, 597679, 594829, 585919, 572659, 570049, 563419, 559219, 549169, 539509, 536779, 536449, 518809, 510619, 500239, 495619, 490579, 477019, 470089, 467479, 465169, 463459, 452539, 444349, 442579, 427249, 420859, 419059, 412039, 402769, 402139, 397759, 397549, 394819, 392269, 389569, 388699, 375259, 361219, 358909, 354259, 347989, 346399, 340939, 334429, 326149, 301999, 300499, 299479, 295879, 294319, 285289, 284749, 276049, 268819, 266689, 260419, 257869, 247999, 247609, 243709, 240049, 225349, 217369, 201829, 201499, 195739, 194869, 187639, 171169, 166849, 165709, 157279, 144169, 135469, 122209, 119299, 116539, 109849, 101119, 99139, 97849, 88819, 82729, 81049, 79699, 77269, 72229, 69499, 67219, 62989, 55339, 51349, 43789, 34849, 31729, 25309, 22279, 21019, 19429, 18919, 18049, 16069, 15739, 15649, 13009, 9439, 5659, 3469, 3259, 2089, 1879, 1489, 829, 199, 109, 19, 13};
int main()
{
	int n;
	while(cin >> n && n)
	{
		for(int i = 0; ; i++)
		{
			if(ans[i] <= n)
			{
				cout << ans[i] << endl;
				break;
			}
		}
		continue;

		// 0 : false, 1 : true, 2 : unknown
		vint table(5, 2);
		int dx[] = {0, 2, 6, 8};

		if(n % 2 == 0) n--;
		for(int i = n - 8; 0 <= i; i -= 2)
		{
			rep(j, 4)
			{
				int prv = j + (1 < j);
				if(table[prv] == 0 || (table[prv] == 2 && (table[prv] = isPrime(i + dx[j])) == 0))
					break;
				else if(j == 3)
				{
					cout << i + 8 << endl;
					goto ng;
				}
			}

			rotate(table.begin(), table.end() - 1, table.end());
			table[0] = 2;
		}

ng:
		;
	}
}

Measured memory_bytes: 0