
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>
#include<iterator>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define rep(i,n) for(int i=0; i<n; i++)
#define INF (1<<20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

ll n;
ll arr[10000010],ans[10000010];

void Eratosthenes() {
	for (ll i = 0; i <= 10000010; i++)arr[i] = 1;
	
	for (ll i = 2; i <= sqrt(10000010); i++) {
		if (arr[i]) {
			for (ll j = 0; i*(j + 2) <= 10000010; j++)
				arr[i*(j + 2)] = 0;
		}
	}

	ll pre_prime = 0;
	for (ll i = 13; i <= 10000010; i++) {
		if (arr[i] && arr[i-2] && arr[i-6] && arr[i-8]) {
			ans[i] = i;
			pre_prime = i;
		}
		else ans[i] = pre_prime;
	}
}