#include #define REP(i,n) for(int i=0;i<(int)(n);i++) #define ALL(x) (x).begin(),(x).end() using namespace std; typedef long long ll; const int SIZE = 2048; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; bool p[SIZE]; bool dfs(int y, int x, int t, int w, vector &visited) { if (y < 0 || x < 0 || x >= w) return false; int n = y * w + x + 1; if (n > t) return false; if (n == t) return true; if (p[n]) return false; if (visited[n]) return false; visited[n] = true; REP(dir,4) if (dfs(y + dy[dir], x + dx[dir], t, w, visited)) return true; return false; } int solve(string str) { if (str.size() >= 8) return 8; int N = stoi(str); for (int i = 1; i < N; ++i) { vector visited(N+1, false); if (dfs(0, 0, N, i, visited)) return i; } return -1; } int main() { for (int i = 2; i < SIZE; ++i) p[i] = true; for (int i = 2; i*i <= SIZE; ++i) if (p[i]) for (int j = i*i; j <= SIZE; j += i) p[j] = false; string str; cin >> str; cout << solve(str) << endl; return 0; }