void main() { const N = readInt; const M = 5000000; auto ok = new bool[10]; foreach (i; 0 .. N) { const a = readInt; ok[a] = true; } int[] A; A ~= 0; { auto S = new bool[M]; foreach (i; 2 .. M) { if (S[i]) continue; A ~= i; for (int j = i * 2; j < M; j += i) S[j] = true; } } A ~= M + 1; int AL = cast(int)(A.length); while (A[AL - 1] > M) AL--; auto cnt = new int[10]; int L = 1, V = N, W; void add(int x) { while (x) { int y = x % 10; if (ok[y] && ++cnt[y] == 1) V--; if (!ok[y] && ++cnt[y] == 1) W++; x /= 10; } } void del(int x) { while (x) { int y = x % 10; if (ok[y] && !--cnt[y]) V++; if (!ok[y] && !--cnt[y]) W--; x /= 10; } } int ans = -1; foreach (R; 1 .. AL) { add(A[R]); while (L <= R && W) del(A[L++]); if (L > R || V) continue; chmax(ans, A[R + 1] - A[L - 1] - 2); } ans.writeln; } import std,core.bitop; string[]_R; string readString(){while(_R.empty){_R=readln.chomp.split;}auto ret=_R.front;_R.popFront;return ret;} int readInt(){return readString.to!int;} long readLong(){return readString.to!long;} ulong readULong(){return readString.to!ulong;} real readReal(){return readString.to!real;} bool chmin(T)(ref T A,T B){if(A>B){A=B;return true;}else{return false;}} bool chmax(T)(ref T A,T B){if(A1){int mid=(L+R)/2;(A[mid]1){int mid=(L+R)/2;(A[mid]<=x?L:R)=mid;}return R;}