#include <cstdio>
#include <cstring>
#define N 10000000
#define mod_32(n) (n&((1<<5)-1))
int prime[(N>>5)+1];

void init(){
	memset(prime,-1,sizeof(prime));
	prime[0] = prime[0] - (1<<0) - (1<<1);
	for(int i=2;i*i <= N;i++){
		if( prime[i>>5] & (1 << mod_32(i)) ){
			for(int j=i*i;j<=N;j+=i){
				prime[j>>5] -= (prime[j>>5] & (1<<mod_32(j)));
			}
		}
	}
}
inline bool isprime(int n){
	return !!(prime[n>>5] & (1<<mod_32(n)));
}
int main(){
	int n;
	init();
	while(~scanf("%d",&n)){
		while(n){
			if( isprime(n) & isprime(n-2) & isprime(n-6) & isprime(n-8) ){printf("%d\n",n);break;}
			n--;
		}
	}
}