#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef ll li; typedef pair PI; #define rep(i,n) for(int i=0;i<(int)(n);++i) #define REP(i, n) rep (i, n) #define F first #define S second #define mp(a,b) make_pair(a,b) #define pb(a) push_back(a) #define SZ(a) (int)((a).size()) #define ALL(a) (a).begin(),(a).end() #define FLL(a,b) memset((a),b,sizeof(a)) #define CLR(a) memset((a),0,sizeof(a)) #define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it) #define FORR(it,a) for(__typeof(a.rbegin())it=a.rbegin();it!=a.rend();++it) template ostream& operator<< (ostream& out, const pair& val){return out << "(" << val.F << ", " << val.S << ")";} template ostream& operator<< (ostream& out, const vector& val){out << "{";rep(i,SZ(val)) out << (i?", ":"") << val[i];return out << "}";} #define declare(a,it) __typeof(a) it=a const double EPS = 1e-8; const int dx[]={1,0,-1,0,1,1,-1,-1,0}; const int dy[]={0,1,0,-1,-1,1,-1,1,0}; const int MAX_P = 5100000; bool npr[MAX_P]; int main(int argc, char *argv[]) { int n; cin >> n; vector a(n); rep(i,n) cin >> a[i]; int appa[10] = {}; rep(i,n) appa[a[i]] = 1; npr[0] = npr[1] = 1; vector app[10]; int vis[10] = {}; for(int i = 2; i < MAX_P; ++i){ if(npr[i]) continue; int t = i; if(i<=5000000){ while(t){ int mo = t%10; if(vis[mo] != i) app[mo].pb(i); vis[mo] = i; t /= 10; } } for(int j = i*2; j < MAX_P; j += i) npr[j] = true; } //cerr << "hoge" << endl; int ans = -1; int lim = 5e6; for(int k = 1; k <= lim; ++k){ int l = lim; bool ok = true; rep(i,10){ if(appa[i]) continue; auto it = lower_bound(ALL(app[i]),k); if(app[i].end() == it) continue; l = min(l, *it-1); } rep(i,10){ if(!appa[i]) continue; auto it = upper_bound(ALL(app[i]),k-1); if(app[i].end() == it || *it > l){ ok = false; break; } } if(ok){ //cout << k << " " << l << endl; ans = max(ans, l - k); } } cout << ans << endl; return 0; }