// #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
template<class 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) { }
};
template<class Weight>
bool operator < (const Edge<Weight> &e, const Edge<Weight> &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;
 
template<class Weight=int>
struct Graph:vector<vector<Edge<Weight> > >{
	Graph(){}
	Graph(const int &n){this->assign(n,vector<Edge<Weight> >());}
	void _add_edge(int from, int to, Weight w, int rev=-1){
		if(this->size() < from + 1)this->resize(from + 1);
		this->at(from).push_back(Edge<Weight>(from,to,w,rev));
	}
};
//add bi-directional edge
template<class Weight>
void addBiEdge(Graph<Weight> &g, const pair<int,int> &e, Weight w=1){
	const int &from = e.first, &to = e.second;
	g._add_edge(from,to,w,g[to].size());
	g._add_edge(to,from,w,g[from].size()-1);
//	while(this->size()<max(from,to)+1)this->push_back(vector<Edge<Weight> >());
//	this->at(from).push_back(Edge<Weight>(from,to,w,this->at(to).size()));
//	this->at(to).push_back(Edge<Weight>(to,from,w,this->at(from).size()-1));
}
//add directional edge
template<class Weight>
void addEdge(Graph<Weight> &g, const pair<int,int> &e, Weight w=1){
	const int &from = e.first, &to = e.second;
	g._add_edge(from,to,w);
//	while(this->size()<from+1)this->push_back(vector<Edge<Weight> >());
//	this->at(from).push_back(Edge<Weight>(from,to,w));
}

#ifdef DEBUG
#include"graph/graphviz.h"
#endif
//}}}

//typedef Weight Flow;
//typedef long long Cost;


//{{{ addFlowEdge(Graph<Flow,Cost>, {from,to},{Flow,Cost})
template<class Flow,class Cost>
void addFlowEdge(Graph<pair<Flow,Cost> > &g,const pair<int,int> &e,const pair<Flow,Cost> &p){
	int from = e.first, to = e.second;
	Flow cap = p.first;Cost cost = p.second;
	g._add_edge(from,to,{cap,cost},g[to].size());
	g._add_edge(to,from,{0,-cost},g[from].size()-1);
//	g[from].push_back(Edge<pair<Flow,Cost> >(from,to,{cap,cost},g[to].size()));
//	g[to].push_back(Edge<pair<Flow,Cost> >(to,from,{0,-cost},g[from].size()-1));
}
//}}}
//{{{ minCostFlow(Graph<Flow,Cost> g,{s,t},f)
template<class Flow,class Cost>
struct minCostFlow_t{
	vector<vector<Flow> > flow;
	bool feasible;
	Cost val;
	operator bool(){return feasible;}
	Cost operator*(){return val;}
};

template<class Flow,class Cost>
minCostFlow_t<Flow,Cost> minCostFlow(const Graph<pair<Flow,Cost> > &g,const pair<int,int> &st,Flow f){
	const int n = g.size();
	const int s = st.first, t = st.second;
	minCostFlow_t<Flow,Cost> ret;
	vector<vector<Flow> > &flow = ret.flow;
	flow.assign(n,vector<Flow>());
	const Flow FINF=1e+9;
	const Cost CINF=1e+18;

	REP(i,n)REP(j,g[i].size())flow[i].push_back(g[i][j].weight.first);
	vector<Cost> dist(n);
	vector<int> prevv(n),preve(n);
	Cost 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 auto &e=g[v][i];
					if(flow[v][i]>0 && dist[e.dst]>dist[v]+e.weight.second){
						dist[e.dst]=dist[v]+e.weight.second;
						prevv[e.dst]=v;preve[e.dst]=i;
						update=true;
					}
				}
			}
		}
		if(dist[t]==CINF){
			ret.feasible = false;
			return ret;
		}
		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 auto &e=g[prevv[v]][preve[v]];
			flow[prevv[v]][preve[v]]-=d;
			flow[v][e.rev]+=d;
		}
	}
	ret.feasible = true;
	ret.val = res;
	return ret;
}
//}}}

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 )
		vector<Int> 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<pair<int,Int> > g(v0.size()+v1.size()+2);
	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,{i,j+v0.size()},{10000,1});
		}else{
			addFlowEdge(g,{i,j+v0.size()},{10000,3});
		}
	}
	REP(i,v0.size())addFlowEdge(g,{s,i},{1,0});
	REP(j,v1.size())addFlowEdge(g,{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;
		auto mcf(minCostFlow(g,{s,t},f));
		assert(mcf);
		auto c = *mcf;
		c+=v0.size()+v1.size()-f*2;
		ans = min(c,ans);
	}
	fout<<ans<<endl;
}

int main(){
	Graph<> g;
	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: 1280