// #includes {{{
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define RREP(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define LET(x,a) __typeof(a) x(a)
//#define IFOR(i,it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair

#define EXIST(e,s) ((s).find(e)!=(s).end())

#define RESET(a) memset((a),0,sizeof(a))
#define SET(a) memset((a),-1,sizeof(a))
#define PB push_back
#define DEC(it,command) __typeof(command) it=command

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define debug2(x) cerr << #x << " = [";REP(__ind,(x).size()){cerr << (x)[__ind] << ", ";}cerr << "] (L" << __LINE__ << ")" << endl;

const int INF=0x3f3f3f3f;

typedef long long Int;
typedef unsigned long long uInt;
typedef long double rn;

typedef pair<int,int> pii;

/*
#ifdef MYDEBUG
#include"debug.h"
#include"print.h"
#endif
*/
// }}}

//{{{ io
FILE *file_in=stdin,*file_out=stdout;
#define fin normal_in
#define fout normal_out
//const char fname[]="";
//FILE *fin=fopen(fname,"r"),*fout=fopen(fname,"w");
#ifdef __MINGW32__
#define LLD "%I64d"
#define LLU "%I64u"
#else
#define LLD "%lld"
#define LLU "%llu"
#endif
struct NORMAL_IN{
	bool cnt;
	NORMAL_IN():cnt(true){}
	operator int() const {return cnt;}
#define endl "\n"
	NORMAL_IN& operator>>(int &n){cnt=fscanf(file_in,"%d",&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(unsigned int &n){cnt=fscanf(file_in,"%u",&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(long long &n){cnt=fscanf(file_in,LLD,&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(unsigned long long &n){cnt=fscanf(file_in,LLU,&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(double &n){cnt=fscanf(file_in,"%lf",&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(long double &n){cnt=fscanf(file_in,"%Lf",&n)!=EOF;return *this;}
	NORMAL_IN& operator>>(char *c){cnt=fscanf(file_in,"%s",c)!=EOF;return *this;}
	NORMAL_IN& operator>>(string &s){
		s.clear();
		for(bool r=false;;){
			const char c=getchar();
			if(c==EOF){ cnt=false; break;}
			const int t=isspace(c);
			if(!r and !t)r=true;
			if(r){
				if(!t)s.push_back(c);
				else break;
			}
		}
		return *this;
	}
	template<class T>
		NORMAL_IN& operator>>(vector<T> &v){
			int n;fscanf(file_in,"%d",&n);
			REP(i,n){
				T t;*this>>t;
				v.push_back(t);
			}
		}
} normal_in;

struct NORMAL_OUT{
	NORMAL_OUT& operator<<(const int &n){fprintf(file_out,"%d",n);return *this;}
	NORMAL_OUT& operator<<(const unsigned int &n){fprintf(file_out,"%u",n);return *this;}
	NORMAL_OUT& operator<<(const long long &n){fprintf(file_out,LLD,n);return *this;}
	NORMAL_OUT& operator<<(const unsigned long long &n){fprintf(file_out,LLU,n);return *this;}
	NORMAL_OUT& operator<<(const double &n){fprintf(file_out,"%lf",n);return *this;}
	NORMAL_OUT& operator<<(const long double &n){fprintf(file_out,"%Lf",n);return *this;}
	NORMAL_OUT& operator<<(const char c[]){fprintf(file_out,"%s",c);return *this;}
	NORMAL_OUT& operator<<(const string &s){fprintf(file_out,"%s",s.c_str());return *this;}
} normal_out;
//}}}

//{{{ Graph
typedef int Weight;
struct Edge {
	int src, dst, rev;
	Weight weight;
	Edge(int src, int dst, Weight weight=1,int rev=-1) :
		src(src), dst(dst), weight(weight), rev(rev) { }
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
		e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
//typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

struct Graph:vector<Edges>{
	Graph(){}
	Graph(const int &n){this->assign(n,Edges());}
	//add bi-directional edge
	void addBiEdge(int from ,int to, Weight w=1){
		while(this->size()<max(from,to)+1)this->push_back(Edges());
		this->at(from).push_back(Edge(from,to,w,this->at(to).size()));
		this->at(to).push_back(Edge(to,from,w,this->at(from).size()-1));
	}
	//add directional edge
	void addEdge(int from ,int to, Weight w=1){
		while(this->size()<from+1)this->push_back(Edges());
		this->at(from).push_back(Edge(from,to,w));
	}
};
#ifdef DEBUG
#include"graph/graphviz.h"
#endif
//}}}

typedef Weight Flow;
typedef long long Cost;

const Cost CINF=1e+18;
const Flow FINF=1e+9;

//{{{ addFlowEdge(Graph,vector<vector<Cost> >, from,to,Flow,Cost)
void addFlowEdge(Graph &g,vector<vector<Cost> > &costv,int from,int to,Flow cap,Cost cost){
	g[from].push_back(Edge(from,to,cap,g[to].size()));
	g[to].push_back(Edge(to,from,0,g[from].size()-1));
	costv[from].push_back(cost);
	costv[to].push_back(-cost);
}
//}}}
//{{{ minCostFlow(Graph g,vector<vector<Cost> > cost,int s,int t,int f)
vector<vector<Flow> > flow;

Cost minCostFlow(const Graph &g,const vector<vector<Cost> > &cost,int s,int t,Flow f){
	const int n=g.size();
	flow.assign(n,vector<Flow>());
	REP(i,n)REP(j,g[i].size())flow[i].push_back(g[i][j].weight);
	vector<Cost> dist(n);
	vector<int> prevv(n),preve(n);
	Int res=0;
	while(f>0){
		fill(ALL(dist),CINF);
		dist[s]=0;
		bool update=true;
		while(update){
			update=false;
			for(int v=0;v<n;v++){
				if(dist[v]==CINF)continue;
				for(int i=0;i<g[v].size();i++){
					const Edge &e=g[v][i];
					if(flow[v][i]>0 && dist[e.dst]>dist[v]+cost[v][i]){
						dist[e.dst]=dist[v]+cost[v][i];
						prevv[e.dst]=v;preve[e.dst]=i;
						update=true;
					}
				}
			}
		}
		if(dist[t]==CINF){
			return -1;
		}
		Flow d=f;
		for(int v=t;v!=s;v=prevv[v]){
			d=min(d,flow[prevv[v]][preve[v]]);
		}
		f-=d;
		res+=d*dist[t];
		for(int v=t;v!=s;v=prevv[v]){
			const Edge &e=g[prevv[v]][preve[v]];
			flow[prevv[v]][preve[v]]-=d;
			flow[v][e.rev]+=d;
		}
	}
	return res;
}
//}}}

const int SMAX=10000;
int pdiv[SMAX];
vector<int> primes;
//{{{ eratosthenes
void sieve_of_eratosthenes(int n) {
	for (int i = 2; i < n; ++i)
		pdiv[i] = i;
	for (int i = 2; i*i < n; ++i)
		if (pdiv[i]==i)
			for (int j = i*i; j < n; j+=i)
				pdiv[j] = i;
}
//}}}
bool is_prime(int n){
	if(n==1)return false;
	for(int i=0;;i++){
		int p = primes[i];
		if(p*p>n)break;
		if(n%p==0)return false;
	}
	return true;
}

void solve(long long N, vector<long long> x){
	vector<int> v0,v1;
	for(int i=0;i<x.size();){
		int j;
		for(j=i+1;j<x.size();j++){
			if(x[j-1]+1!=x[j])break;
		}
		//[ x[i], x[j-1]+1 )
		auto v({x[i],x[j-1]+1});
		for(auto u:v){
			if(u%2==0)v0.push_back(u);
			else v1.push_back(u);
		}
		i = j;
	}
	Graph g(v0.size()+v1.size()+2);
	vector<vector<Cost> > cost(g.size(),vector<Cost>());
	int s = v0.size() + v1.size(), t = s + 1;
	REP(i,v0.size())REP(j,v1.size()){
		if(is_prime(abs(v0[i]-v1[j]))){
			addFlowEdge(g,cost,i,j+v0.size(),10000,1);
		}else{
			addFlowEdge(g,cost,i,j+v0.size(),10000,3);
		}
	}
	REP(i,v0.size())addFlowEdge(g,cost,s,i,1,0);
	REP(j,v1.size())addFlowEdge(g,cost,j+v0.size(),t,1,0);
	Int ans = INF;
	int r = v0.size()%2;
	for(int f=r;;f+=2){
		if(v0.size()<f or v1.size()<f)break;
		Cost c = minCostFlow(g,cost,s,t,f);
		c+=v0.size()+v1.size()-f*2;
		ans = min(c,ans);
	}
	fout<<ans<<endl;
}

int main(){	
	sieve_of_eratosthenes(SMAX);
	for(int p=2;p<SMAX;p++)if(pdiv[p]==p)primes.push_back(p);
	long long N;
	fin >> N;
	vector<long long> x(N-1+1);
	for(int i = 0 ; i <= N-1 ; i++){
		fin >> x[i];
	}
	solve(N, x);
	return 0;
}



Measured memory_bytes: 1024