<filename>VJudge/356109 J.cpp
/*************************************************************************
>>> Author: WindCry1
>>> Mail: <EMAIL>
>>> Website: https://windcry1.com
>>> Date: 12/30/2019 11:03:37 PM
*************************************************************************/
//#pragma GCC optimize(3)
//#pragma GCC diagnostic error "-std=c++11"
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <iostream>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <complex>
#include <stack>
#include <bitset>
#include <iomanip>
#include <list>
#include <sstream>
#include <fstream>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define endl '\n'
#define ALL(x) x.begin(),x.end()
#define MP(x,y) make_pair(x,y)
#define ll long long
#define ull unsigned long long
#ifdef WindCry1
#define DEBUG(x) cout<<#x<<" : "<<x<<endl;
#endif
#define lowbit(x) x&(-x)
#define ls u<<1
#define rs u<<1|1
using namespace std;
template<typename T> inline T MIN(const T &a,const T &b) {return a<b?a:b;}
template<typename T> inline T MAX(const T &a,const T &b) {return a>b?a:b;}
template<typename T,typename ...Args> inline T MIN(const T &a,const T &b,Args ...args) {return MIN(MIN(a,b),args...);}
template<typename T,typename ...Args> inline T MAX(const T &a,const T &b,Args ...args) {return MAX(MAX(a,b),args...);}
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const double eps = 1e-8;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
int n;
vector<ll> a;
bool isprime(ll x){
	for(ll i=2;i<=sqrt(x);i++) if(x%i==0) return false;
	return true;
}
ll getres(ll x){
	ll res=0;
	for(int i=0;i<n;i++) res+=abs(x-a[i]);
	return res;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef WindCry1
	freopen("C:/Users/LENOVO/Desktop/in.txt","r",stdin);
#endif
	cin>>n;
	a.resize(n);
	for(auto &i:a) cin>>i;
	sort(ALL(a));
	ll res=INF;
	ll mid=n&1?a[n/2]:(a[n/2]+a[n/2-1])/2;
	if(mid<2) mid=2;
	for(ll i=mid;;i++){
		if(isprime(i)){
			res=min(res,getres(i));
			break;
		}
	}
	for(ll i=mid;i>=2;i--){
		if(isprime(i)){
			res=min(res,getres(i));
			break;
		}
	}
	cout<<res<<endl;
	return 0;
}


