import java.util.Scanner;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int n = 0,maxPQ = 0;
	
	while(sc.hasNext()){
	    n = sc.nextInt();
	    if(n==0) break;
	    
	    boolean[] check = new boolean[n];

	    System.out.println(checkPrime(check));
	}
    }

    static int checkPrime(boolean[] check){ // テ・ツ崢崚」ツ?、テ・ツュツ静ァツエツ?ヲツ閉ーテ」ツつ津ィツソツ氾」ツ??
	
	int max=0;
	for(int i = 1;i*i<check.length;i++){ // テ」ツ?敕」ツつ古」ツ?古ァツエツ?ヲツ閉ーテ」ツ??」ツ?」テ」ツ?淌」ツつ嘉」ツ?敕」ツ?ョテ・ツ?催ヲツ閉ーテ」ツつ稚rueテ」ツ?ォテ」ツ?凖」ツつ?
	    if(!check[i]) notPrime(check,i+1);
	    if(i>=13){
		if(!check[i] && !check[i-2] && !check[i-6] && !check[i-8]){
		    max = i;
		}
	    }

	}
	return max;
    }

    static void notPrime(boolean[] check,int n){ // テァツエツ?ヲツ閉ーテ」ツ??」ツ?」テ」ツ?淌ヲツ閉ーテ」ツ?ョテ・ツ?催ヲツ閉ーテ」ツつ稚rueテ」ツ?ォ
	for(int i = 2;i*n<check.length;i++){
	    check[(n*i)-1] = true;
	}
    }
}