#include <bits/stdc++.h>
using namespace std;
vector<bool> prime(10000,true);
typedef __int128 Int;
bool bfs(int w,int n){
    vector<bool> used(30000,false);
    used[1]=true;
    used[0]=true;
    queue<int> que;
    que.push(1);
    while(que.empty()==false){
        int v=que.front();que.pop();
        if(v%w!=1&&used[v-1]==false&&prime[v-1]==false){
            que.push(v-1);
            used[v-1]=true;
        }
        if(v%w!=0&&used[v+1]==false&&v+1<=n&&prime[v+1]==false){
            que.push(v+1);
            used[v+1]=true;
        }
        if(v+w<=n&&used[v+w]==false&&prime[v+w]==false){
            que.push(v+w);
            used[v+w]=true;
        }
        if(v-w>=1&&used[v-w]==false&&prime[v-w]==false){
            que.push(v-w);
            used[v-w]=true;
        }
    }
    return used[n];
}

void eratosthenes(){
    for(int i=2;i<10000;i++){
        if(prime[i]){
            for(int j=i*2;j<10000;j+=i){
                    prime[j]=false;
            }
        }
    }
}

Int powmod(Int a,Int n,Int Mod){
    Int res=1;
    while(n>0){
        if((n&1)>0){
            res*=a;
            res%=Mod;
        }
        a=a*a;
        a%=Mod;
        n>>=1;
    }
    return res;
}

const Int mask=(1<<30);
bool miller_rabin(Int n){
    Int d=n-1,s=0;
    while(d%2==0){
        d/=2;
        s+=1;
    }
    for(int k=0;k<10000;k++){
        Int a=0;
        for(int i=0;i<2;i++){
            a<<=30;
            Int rnd=rand();
            a+=rnd%mask;
        }
        a=a%(n-1)+1;
        //cout<<(int)a<<endl;
        if(powmod(a,d,n)!=1){
            bool b=true;
            for(Int r=0,_r=1;r<s;r++,_r<<=1){
                Int _d=d*_r;
                b&=(powmod(a,_d,n)!=n-1);
            }
            if(b)return false;
        }
    }
    return true;
}

int main(){
    string S;
    cin>>S;
    if(S.size()<4){
        eratosthenes();
        int a=stoi(S);
        for(int w = 2; w < a; w++){
            if(bfs(w,a)){
                cout<<w<<endl;
                return 0;
            }
        }
    }
    else {
        Int a=0;
        for(auto& it : S){
            a*=10;
            a+=(it-'0');
        }
        a-=8;
        if((a%8==1)&&miller_rabin(a))cout<<14<<endl;
        else cout<<8<<endl;
    }
    return 0;
}