using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace No308 { public class Program { void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- var N = ss.Next(BigInteger.Parse); if (N < 100) { sw.WriteLine(BruteForce((int)N)); return; } if (N % 8 != 1) { sw.WriteLine(8); return; } sw.WriteLine(MillerRabin.IsPrime(N - 8) ? 14 : 8); //--------------------------------- } int BruteForce(int n) { for (var w = 1; w <= n; w++) { var uf = new UnionFind(n + 1); for (var i = 1; i <= n; i++) { if (MillerRabin.IsPrime(i)) continue; if (i + 1 <= n && i % w != 0 && !MillerRabin.IsPrime(i + 1)) uf.Unite(i, i + 1); if (i + w <= n && !MillerRabin.IsPrime(i + w)) uf.Unite(i, i + w); } if (uf.IsSameSet(1, n)) return w; } return -1; } static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false}; new Program().Solve(ss, sw); sw.Flush(); } } public static class MillerRabin { const int RandomTestCount = 30; static readonly BigInteger[] BasePrimes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,}; static readonly BigInteger[] WitnessRanges = { 2047, 1373653, 25326001, 118670087467, //3215031751が例外 2152302898747, 3474749660383, 341550071728321, 341550071728321, 3825123056546413051, 3825123056546413051, 3825123056546413051, BigInteger.Parse("318665857834031151167461"), BigInteger.Parse("3317044064679887385961981"), }; public static bool IsPrime(BigInteger n) { if (n < 2 || n == 3215031751) return false; if (n < 42) return BasePrimes.Contains(n); if (BasePrimes.Any(p => n % p == 0)) return false; var d = n - 1; while ((d & 1) == 0) d >>= 1; for (var i = 0; i < BasePrimes.Length; i++) { if (!MillerRabinTest(n, d, BasePrimes[i])) return false; if (n < WitnessRanges[i]) return true; } //n > 3.3e24 var random = new Random(); for (var i = 0; i < RandomTestCount; i++) { if (!MillerRabinTest(n, d, random.Next(1, int.MaxValue))) return false; } return true; } static bool MillerRabinTest(BigInteger n, BigInteger d, BigInteger a) { a = BigInteger.ModPow(a, d, n); if (a == 1 || a == n - 1) return true; while ((d <<= 1) < n - 1) { a = a * a % n; if (a == n - 1) return true; } return false; } } public class UnionFind { readonly int[] p; public UnionFind(int n) { p = new int[n]; for (var i = 0; i < n; i++) p[i] = -1; } public int Root(int x) { return p[x] < 0 ? x : (p[x] = Root(p[x])); } public bool IsSameSet(int x, int y) { return Root(x) == Root(y); } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x == y) return false; if (p[x] > p[y]) { var t = x; x = y; y = t; } p[x] += p[y]; p[y] = x; return true; } public int Size(int x) { return -p[Root(x)]; } } public class StreamScanner { static readonly char[] Sep = {' '}; readonly Queue buffer = new Queue(); readonly TextReader textReader; public StreamScanner(TextReader textReader) { this.textReader = textReader; } public T Next(Func parser) { if (buffer.Count != 0) return parser(buffer.Dequeue()); var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries); foreach (var nextString in nextStrings) buffer.Enqueue(nextString); return Next(parser); } public T[] Next(Func parser, int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next(parser); return ret; } public T[][] Next(Func parser, int x, int y) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next(parser, x); return ret; } } }