import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random; import std.string, std.conv, std.stdio, std.typecons; const auto maxPeriod = 5000000; void main() { auto n = readln.chomp.to!int; auto ai = readln.split.map!(to!int); auto b = 0; foreach (a; ai) b |= (1 << a); auto pi = primes(maxPeriod); auto qi = pi.map!(digits); auto maxLen = 0; auto s = -1; foreach (t, p; pi) { if ((qi[t] & ~b) != 0) { if (s != -1 && qi[s..t].reduce!("a | b") == b) { auto p1 = s == 0 ? 1 : pi[s - 1] + 1; auto p2 = t == pi.length - 1 ? maxPeriod : pi[t] - 1; auto len = p2 - p1; maxLen = max(maxLen, len); } s = -1; } else { if (s == -1) s = t.to!int; } } if (s != -1) { auto t = pi.length; auto p1 = s == 0 ? 1 : pi[s - 1] + 1; auto p2 = maxPeriod; auto len = p2 - p1; maxLen = max(maxLen, len); } writeln(maxLen == 0 ? -1 : maxLen); } int digits(int n) { auto r = 0; for (; n > 0; n /= 10) r |= (1 << (n % 10)); return r; } int[] primes(int n) { auto sieve = new bool[](n + 1); sieve[0..2] = false; sieve[2..$] = true; foreach (p; 2..n.to!real.sqrt.to!int + 1) if (sieve[p]) foreach (q; iota(p * 2, n + 1, p)) sieve[q] = false; return sieve.enumerate .filter!("a[1]") .map!("a[0]").map!(to!int).array; }