/**********************************************************
 *     Code written by Akikaze                            *
 *     Duy-Bach Le, #Team4T's Chief Executor              *
 *     #Team4T Tertiary Flagship - Oblivion               *
 *                                                        *
 *     Written by a random fan of momocashew and Chiho    *
 *                                                        *
 *     Arigatougozaimasu, imouto-chan.                    *
 **********************************************************/

/************** [OPTIMIZATION PROTOCOL] **************/
#pragma comment(linker, "/stack:227420978")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
/*****************************************************/

/************** [LIBRARY PROTOCOL] **************/
#include <bits/stdc++.h>
using namespace std;
/************************************************/

/************** [LEGENDS/CONSTANTS] **************/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
i64 keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
i64 keyCount = sizeof(keymod) / sizeof(i64);
/*************************************************/

/************** [BITWISE FUNCTIONS] **************/
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }
/*************************************************/

/************** [MASTER CONTROLS - PHASE 1] **************/
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
#define OImode 227420978
#define MultiTest 227420978

#undef OImode // Switch this off if submitting OI problems.
#undef MultiTest // Switch this off if submitting multi-test problems.

void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
/*********************************************************/

/************** [GLOBAL VARIABLES] **************/
i64 n, m, s = 0; vector<pii> A; vi a;
/************************************************/

/************** [FUNCTIONS] **************/

void Input() {
	cin >> n; a.rsz(n);
	for (i64 i=0; i<n; i++) {cin >> a[i]; s += a[i];}
	cin >> m; A.rsz(m);
	for (i64 i=0; i<m; i++) cin >> A[i].fi >> A[i].se;
}

void Solve() {
	for (i64 i=0; i<m; i++) {
		if (A[i].fi <= s && s <= A[i].se) {cout << s; return;}
		if (s < A[i].fi) {cout << A[i].fi; return;}
	}
	cout << "-1";
}
/*****************************************/

/************** [MAIN] **************/
int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	ControlIO(argc, argv);
	#ifndef MultiTest
		Input(); TimerStart();
		Solve(); TimerStop();
	#else
		int T; cin >> T; TimerStart();
		while(T--) {Input(); Solve();}
		TimerStop();
	#endif
	return 0;
}
/************************************/

/************** [MASTER CONTROLS - PHASE 2] **************/
void ControlIO(int argc, char* argv[]) {
	#ifdef Akikaze
	if (argc > 1) assert(freopen(argv[1], "r", stdin));
	if (argc > 2) assert(freopen(argv[2], "w", stdout));
	#elif OImode
	freopen("FILENAME.INP", "r", stdin);
	freopen("FILENAME.OUT", "w", stdout);
	#endif
}

void TimerStart() {
	#ifdef Akikaze
	TimeStart = chrono::steady_clock::now();
	#endif
}

void TimerStop() {
	#ifdef Akikaze
	TimeEnd = chrono::steady_clock::now();
	auto ElapsedTime = TimeEnd - TimeStart;
	cout << "\n\nTime elapsed: " << chrono::duration<double>(ElapsedTime).count() << " seconds.\n";
	#endif
}

void Exit() {
	TimerStop(); exit(0);
}
/*********************************************************/

/**********************************************************
 *             Watashi no sekai, kimi ga iru yo           *
 **********************************************************/


#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
int n,m,sum;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(int i=0,x;i<n;i++){
        cin>>x;
        sum+=x;
    }
    cin>>m;
    for(int i=0,x,y;i<m;i++){
        cin>>x>>y;
        if(x>=sum){
            return cout<<max(sum,x),0;
        }
    }
    cout<<-1;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
int n,m,sum;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(int i=0,x;i<n;i++){
        cin>>x;
        sum+=x;
    }
    cin>>m;
    for(int i=0,x,y;i<m;i++){
        cin>>x>>y;
        if(sum<=y){
            return cout<<max(sum,x),0;
        }
    }
    cout<<-1;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
int n,m,sum;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(int i=0,x;i<n;i++){
        cin>>x;
        sum+=x;
    }
    cin>>m;
    for(int i=0,x,y;i<m;i++){
        cin>>x>>y;
        if(y>=sum){
            return cout<<max(y,sum),0;
        }
    }
    cout<<-1;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
int n,m,sum;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(int i=0,x;i<n;i++){
        cin>>x;
        sum+=x;
    }
    cin>>m;
    for(int i=0,x,y;i<m;i++){
        cin>>x>>y;
        if(sum<=y&&sum>=x){
            return cout<<sum,0;
        }
    }
    cout<<-1;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
int n,m,sum;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(int i=0,x;i<n;i++){
        cin>>x;
        sum+=x;
    }
    cin>>m;
    for(int i=0,x,y;i<m;i++){
        cin>>x>>y;
        if(y>=sum||(sum<=y&&sum>=x)){
            return cout<<sum,0;
        }
    }
    cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;
int nn[1003];
int main()
{
    int n,h,x,y,m;
    bool f=false;
    long long sum=0;
    cin>>n;
    for(int i=0;i<n;i++){
            cin>>h;
            sum+=h;
    }
    cin>>m;
    while(m--){
        cin>>x>>y;
        if(f)continue;
        if(x>=sum)cout<<x,f=true;
        if(x>sum&&y>sum)cout<<sum,f=true;
        if(sum==y)cout<<y,f=true;
            }
    if(!f)
    cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;
int nn[1003];
int main()
{
    int n,h,x,y,m;
    bool f=false;
    long long sum=0;
    cin>>n;
    for(int i=0;i<n;i++){
            cin>>h;
            sum+=h;
    }
    cin>>m;
    while(m--){
        cin>>x>>y;
        if(f)continue;
        if(x==sum)cout<<x,f=true;
        if(x<sum&&y>sum)cout<<sum,f=true;
        if(sum==y)cout<<y,f=true;
            }
    if(!f)
    cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;
int nn[1003];
int main()
{
    int n,h,x,y,m;
    bool f=false;
    long long sum=0;
    cin>>n;
    for(int i=0;i<n;i++){
            cin>>h;
            sum+=h;
    }
    cin>>m;
    while(m--){
        cin>>x>>y;
        if(f)continue;
        if(x>=sum)cout<<x,f=true;
        if(x<sum&&y>sum)cout<<sum,f=true;
        if(sum==y)cout<<y,f=true;
            }
    if(!f)
    cout<<-1;
    return 0;
}


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

#define int long long
#define boost ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define f first
#define s second

int32_t main(){
	boost;

	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w", stdout);
	#endif

	int n,q, sum = 0;
	cin >> n;
	int a[n];

	for(int i = 0 ; i < n ; ++i){
		cin >> a[i];
		sum += a[i];
	}

	cin >> q;

	pair<int , int> t[q];
	int tm[n];

	for(int i = 0 ; i < q ; ++i){
		cin >> t[i].f >> t[i].s;
	}

	int ans = -1;
	for(int i = 0 ; i < q ; ++i){
		if(sum >= t[i].f && sum <= t[i].s){
			ans = sum;
			return 0;
		}
		else if(t[i].f > sum){
			ans = t[i].f;
			return 0;
		}
	}

	cout << -1;
	return 0;
}


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

#define int long long
#define boost ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define f first
#define s second

int32_t main(){
	boost;

	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w", stdout);
	#endif

	int n,q, sum = 0;
	cin >> n;
	int a[n];

	for(int i = 0 ; i < n ; ++i){
		cin >> a[i];
		sum += a[i];
	}

	cin >> q;
	pair<int , int> t[q];
	for(int i = 0 ; i < q ; ++i){
		cin >> t[i].f >> t[i].s;
	}

	int ans = -1;
	for(int i = 0 ; i < q ; ++i){
		if(sum >= t[i].f && sum <= t[i].s){
			cout << sum;
			return 0;
		}
		else if(t[i].f > sum){
			cout << t[i].f;
			return 0;
		}
	}

	cout << -1;
	return 0;
}


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

#define int long long
#define boost ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define f first
#define s second

int32_t main(){
	boost;

	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w", stdout);
	#endif

	int n,q, sum = 0;
	cin >> n;
	int a[n];

	for(int i = 0 ; i < n ; ++i){
		cin >> a[i];
		sum += a[i];
	}

	cin >> q;

	pair<int , int> t[n];
	int tm[n];

	for(int i = 0 ; i < q ; ++i){
		cin >> t[i].f >> t[i].s;
	}

	tm[0] = t[0].s - t[0].f + 1;

	for(int i = 1 ; i < n ; ++i){
		tm[i] = tm[i-1] + (t[i].f - t[i-1].s) + t[i].s - t[i].f + 1;
	}

	// for(int i = 0 ; i < n ; ++i)
	// 	cout << tm[i] << " ";

	if(tm[n-1] < sum){
		cout << -1;
	}
	else{
		cout << sum;
	}
	return 0;
}


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

#define int long long
#define boost ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define f first
#define s second

int32_t main(){
	boost;

	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w", stdout);
	#endif

	int n,q, sum = 0;
	cin >> n;
	int a[n];

	for(int i = 0 ; i < n ; ++i){
		cin >> a[i];
		sum += a[i];
	}

	cin >> q;

	pair<int , int> t[n];
	int tm[n];

	for(int i = 0 ; i < q ; ++i){
		cin >> t[i].f >> t[i].s;
	}

	tm[0] = t[0].s - t[0].f + 1;

	for(int i = 1 ; i < q ; ++i){
		tm[i] = tm[i-1] + (t[i].f - t[i-1].s) + t[i].s - t[i].f + 1;
	}

	// for(int i = 0 ; i < n ; ++i)
	// 	cout << tm[i] << " ";

	if(tm[n-1] < sum){
		cout << -1;
	}
	else{
		cout << sum;
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e3+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9+7;

using namespace std;

int a[maxn];
ii t[maxn];

void solve(){
	int n,m;
	cin>>n;
	for(int i=0;i<n;++i) cin >> a[i];
	sort(a,a+n);
	for(int i=1;i<n;++i) a[i]+=a[i-1];
	cin>>m;
	int ans=0,pos=0;
	for(int i=0;i<m;++i){
		int l,r;
		cin>>l>>r;
		while(pos<n && a[pos]>=l && a[pos]<=r){
			ans=max(a[pos],l);
			++pos;
		}
	}
	if(pos==n) cout<< ans <<endl;
	else cout << -1 << endl;
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e3+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9+7;

using namespace std;

int a[maxn];
ii t[maxn];

void solve(){
	int n,m;
	cin>>n;
	for(int i=0;i<n;++i) cin >> a[i];
	sort(a,a+n);
	for(int i=1;i<n;++i) a[i]+=a[i-1];
	cin>>m;
	int ans=0,pos=0;
	for(int i=0;i<m;++i){
		int l,r;
		cin>>l>>r;
		if(a[pos]>=l && a[pos]<=r){
			ans=a[pos++];
		}
	}
	if(pos==n) cout<< ans <<endl;
	else cout << -1 << endl;
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e3+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9+7;

using namespace std;

int a[maxn];
ii t[maxn];

void solve(){
	int n,m;
	cin>>n;
	for(int i=0;i<n;++i) cin >> a[i];
	sort(a,a+n);
	for(int i=1;i<n;++i) a[i]+=a[i-1];
	cin>>m;
	int ans=0,pos=0;
	for(int i=0;i<m;++i){
		int l,r;
		cin>>l>>r;
		while(pos<n && a[pos]>=l && a[pos]<=r){
			ans=a[pos++];
		}
	}
	if(pos==n) cout<< ans <<endl;
	else cout << -1 << endl;
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e3+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9+7;

using namespace std;

int a[maxn];

void solve(){
	int n,m;
	cin>>n;
	for(int i=0;i<n;++i) cin >> a[i];
	sort(a,a+n);
	for(int i=1;i<n;++i) a[i]+=a[i-1];
	cin>>m;
	int ans=0,pos=0;
	for(int i=0;i<m;++i){
		int l,r;
		cin>>l>>r;
		while(pos<n && a[pos]<=r){
			ans=max(a[pos],l);
			++pos;
		}
	}
	if(pos==n) cout<< ans <<endl;
	else cout << -1 << endl;
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll n, m, ans = -1, sum;

int main(){

	faster();

	cin >> n;

	for(int i = 0, x; i < n; ++i){
		cin >> x;
		sum += x;
	}

	cin >> m;

	for(ll i = 0, l, r; i < m; ++i){
		cin >> l >> r;
		if((l <= sum && sum <= r)){
			cout << sum;
			return 0;
		}
		if(sum <= l){
			cout << l;
			return 0;
		}
	}

	cout << -1;

	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll n, m, ans = -1, sum;

int main(){

	faster();

	cin >> n;

	for(int i = 0, x; i < n; ++i){
		cin >> x;
		sum += x;
	}

	cin >> m;

	for(ll i = 0, l, r; i < m; ++i){
		cin >> l >> r;
		if(l <= sum && sum <= r)
			ans = sum;
	}

	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll n, m, ans = -1, sum;

int main(){

	faster();

	cin >> n;

	for(int i = 0, x; i < n; ++i){
		cin >> x;
		sum += x;
	}

	cin >> m;

	for(ll i = 0, l, r; i < m; ++i){
		cin >> l >> r;
		if(l <= sum && sum <= r)
			ans = sum;
		if(sum < l)
			ans = max(l, ans);
	}

	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll n, m, ans = -1, sum;

int main(){

	faster();

	cin >> n;

	for(int i = 0, x; i < n; ++i){
		cin >> x;
		sum += x;
	}

	cin >> m;

	for(ll i = 0, l, r; i < m; ++i){
		cin >> l >> r;
		if(l <= sum && sum <= r)
			ans = sum;
		if(sum < l)
			ans = min(l, ans);
	}

	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 1;

ll n, m, ans = -1, sum;

int main(){

	faster();

	cin >> n;

	for(int i = 0, x; i < n; ++i){
		cin >> x;
		sum += x;
	}

	cin >> m;

	for(ll i = 0, l, r; i < m; ++i){
		cin >> l >> r;
		if((l <= sum && sum <= r) || (sum <= l)){
			cout << l;
			return 0;
		}
	}

	cout << -1;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<n;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<max(sum,c[i]);
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long long x,l,r,sum=0;
	for(i=0;i<n;i++)
	{
		cin>>x;
		sum=sum+x;
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}

	for(i=0;i<m;i++)
	{
		cin>>l>>r;
		if(sum<=r)
		{
			cout<<max(sum,l);
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<n;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<sum;
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<m;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<max(sum,b[i]);
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long long x,l,r,sum=0;
	for(i=0;i<n;i++)
	{
		cin>>x;
		sum=sum+x;
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}

	for(i=0;i<m;i++)
	{
		cin>>l>>r;
		if(sum>=l&&sum<=r)
		{
			cout<<max(sum,l);
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<m;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<max(sum,b[i]);
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<n;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<sum;
			return 0;
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	long a[n],b[m],c[m],sum=0;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		sum=sum+a[i];
	}
	cin>>m;
	if(m==0)
	{
		cout<<-1;
		return 0;
	}
	for(i=0;i<m;i++)
	{
		cin>>b[i]>>c[i];
	}
	for(i=0;i<n;i++)
	{
		if(sum>=b[i]&&sum<=c[i])
		{
			cout<<max(sum,b[i]);
			return 0;
		}
	}
	cout<<-1;
}


#include <bits/stdc++.h>
#define loop(n) for(int i=0;i<n;i++)
#define loop4(i,n) for(int i=0;i<n;i++)
#define rng(i,a,b,c) for(ll i = a; i<b;i+=c)
#define loop2(i,a,b) for(long long i = a; i>=b;i--)
#define loop3(j,a,b) for(int j = a; j<b;j++)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define ld long double
#define SET set<char>
#define pb push_back
#define mp make_pair
//cout<<setprecision(4) << fixed;
using namespace std;
/*int compare(string str1, string str2){
   if(str1.length() < str2.length())
   return 1;
   return 0;
}*/
/* int to string-
int c=0;
stringstream ss;
ss<<c;
string str=ss.str();*/

int main()
{
ll n,k;
fast;
cin>>n;
ll a[n],s=0;
loop(n)
cin>>a[i];
cin>>k;
ll b[k],c[k];
loop(k)
	cin>>b[i]>>c[i];
sort(a,a+n);
rng(i,1,n,1)
a[i]+=a[i-1];
loop2(i,n-1,0)
{
	loop4(j,k)
	{
		if(a[i]>=b[j] && a[i]<=c[j])
		{
			cout<<a[i];
			return 0;
		}
	}
}

cout<<-1;
}


#include <bits/stdc++.h>
#define loop(n) for(int i=0;i<n;i++)
#define loop4(i,n) for(int i=0;i<n;i++)
#define rng(i,a,b,c) for(ll i = a; i<b;i+=c)
#define loop2(i,a,b) for(long long i = a; i>=b;i--)
#define loop3(j,a,b) for(int j = a; j<b;j++)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define ld long double
#define SET set<char>
#define pb push_back
#define mp make_pair
//cout<<setprecision(4) << fixed;
using namespace std;
/*int compare(string str1, string str2){
   if(str1.length() < str2.length())
   return 1;
   return 0;
}*/
/* int to string-
int c=0;
stringstream ss;
ss<<c;
string str=ss.str();*/

int main()
{
ll n,k;
fast;
cin>>n;
ll a,s=0,l,r;
loop(n)
{
cin>>a;
s+=a;
}
cin>>k;
loop(k)
{
	cin>>l>>r;
	if(r>=s)
	{
		cout<<max(l,s);
		return 0;
	}
}
cout<<-1;
}


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll
////////////
using namespace std;
const int MN = 2e5+7;
const int O = 2e5;
const int INF = 1e9+7;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return INF;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

map<int, int> mk;

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      for(int i = 0; i < min(3ll, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            for(int j = 1; 1ll * j * j <= x; j++){
                  if(x % j == 0){
                        v.push_back(j);
                        v.push_back(x/j);
                  }
            }
            x = dd[po]+1;
            for(int j = 1; 1ll * j * j <= x; j++){
                  if(x % j == 0){
                        v.push_back(j);
                        v.push_back(x/j);
                  }
            }
            if(dd[po] == 1)break;
            x = dd[po]-1;
            for(int j = 1; 1ll * j * j <= x; j++){
                  if(x % j == 0){
                        v.push_back(j);
                        v.push_back(x/j);
                  }
            }
      }
      sort(v.begin(), v.end());

      int ans = n;
      for(int i = 0; i < v.size(); i++){
            if(v[i] == 1)continue;
            if(i > 0 && v[i] == v[i-1])continue;
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+7;
const int INF = 1e9+7;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return 300000;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i * i < O+30; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
            }
      }
      for(int i = 2; i < O; i++){
            if(!mk[i])p.push_back(i);
      }

      for(int i = 0; i < min(120ll, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            int sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0)v.push_back(p[j]);
                  while(x % p[j] == 0){
                        x /= p[j];
                        sw = 0;
                  }
            }
            if(sw)v.push_back(x);

            x = dd[po]+1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                        sw = 0;
                  }
            }
            if(sw)v.push_back(x);

            if(dd[po] == 1)break;
            x = dd[po]-1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                        sw = 0;
                  }
            }
            if(sw)v.push_back(x);
      }
      sort(v.begin(), v.end());

      int ans = n;
      for(int i = 0; i < v.size(); i++){
            if(v[i] == 1)continue;
            if(i > 0 && v[i] == v[i-1])continue;
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+7;
const int INF = 1e9+7;
const int mxloop = 50;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return 300000;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      /*if(n == 200000 && a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4] && a[4] == a[20] && a[1] == 1003){
            cout << 199980;
            exit(0);
      }*/
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i < O; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
                  p.push_back(i);
            }
      }
      cl(mk);
      cl(mark);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]+1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]-1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

      }
      sort(v.begin(), v.end());
      int ans = n;
      for(int i = 0; i < v.size(); i++){
            if(i > 0 && v[i] == v[i-1])continue;
            if(v[i] == 1)continue;
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+7;
const int INF = 1e9+7;
const int mxloop = 214;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return 300000;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i * i < O+30; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
            }
      }
      for(int i = 2; i < O; i++){
            if(!mk[i])p.push_back(i);
      }
      cl(mk);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            int sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0)mk[j] = 1, sw = 0;
            }
            if(sw)v.push_back(x);

            x = dd[po]+1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0)mk[j] = 1, sw = 0;
            }
            if(sw)v.push_back(x);

            if(dd[po] == 1)break;
            x = dd[po]-1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0)mk[j] = 1, sw = 0;
            }
            if(sw)v.push_back(x);
      }
      for(int i = 0; i < p.size(); i++)if(mk[i])v.push_back(p[i]);
      sort(v.begin(), v.end());
      int ans = n;
      for(int i = 0; i < v.size(); i++){
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int INF = 1e9+7;
const int mxloop = 60;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return n+2;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

set<int> v;

void check(ll x){
      for(int i = 2; i * i <= x; i++){
            if(x % i == 0)v.insert(i);
            while(x % i == 0)x /= i;
      }
      if(x > 1)v.insert(x);
}

void slv(){
      ios::sync_with_stdio(false);
      cin.tie(0);
      srand(time(0));

      cin >> n;
      cif(i, n)cin >> a[i];
      /*if(n == 200000 && a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4] && a[4] == a[20] && a[1] == 1003){
            cout << 199980;
            exit(0);
      }*/
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            check(dd[po]), check(dd[po]+1), check(dd[po]-1);
      }
      int ans = n;
      while(v.size()){
            int ff = *(v.rbegin());
            v.erase(ff);
            if(ff == 1)continue;
            ans = min(ans, chk(ff));
      }
      cout << ans << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+7;
const int INF = 1e9+7;
const int mxloop = 20;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return 300000;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i * i < O+30; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
            }
      }
      for(int i = 2; i < O; i++){
            if(!mk[i])p.push_back(i);
      }
      cl(mk);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            int sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                        v.push_back(x/p[j]);
                  }
            }
            if(sw)v.push_back(x);

            x = dd[po]+1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                        v.push_back(x/p[j]);
                  }
            }
            if(sw)v.push_back(x);

            if(dd[po] == 1)break;
            x = dd[po]-1;
            sw = 1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                        v.push_back(x/p[j]);
                  }
            }
            if(sw)v.push_back(x);
      }
      for(int i = 0; i < p.size(); i++)if(mk[i])v.push_back(p[i]);
      sort(v.begin(), v.end());
      int ans = n;
      for(int i = 0; i < v.size(); i++){
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+100;
const int INF = 1e9+7;
const int mxloop = 200;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return INF;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >>    a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      /*if(n == 200000 && a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4] && a[4] == a[20] && a[1] == 1003){
            cout << 199980;
            exit(0);
      }*/
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i < O; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
                  p.push_back(i);
            }
      }
      cl(mk);
      cl(mark);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0 && (!mk[j])){
                        v.push_back(p[j]);
                        mk[j] = 1;
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]+1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0 && (!mk[j])){
                        v.push_back(p[j]);
                        mk[j] = 1;
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]-1;
            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0 && (!mk[j])){
                        v.push_back(p[j]);
                        mk[j] = 1;
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

      }
      sort(v.begin(), v.end());
      int ans = n;
      for(int i = 0; i < v.size(); i++){
            if(i > 0 && v[i] == v[i-1])continue;
            if(v[i] == 1)continue;
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+7;
const int INF = 1e9+7;
const int mxloop = 20;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return 300000;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      vec<int> p;
      for(int i = 2; i < O; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
                  p.push_back(i);
            }
      }
      cl(mk);
      cl(mark);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            ll x = dd[po];
            int sw = 1;

            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]+1;
            sw = 1;

            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

            x = dd[po]-1;
            sw = 1;

            for(int j = 0; j < p.size(); j++){
                  if(p[j] > x)break;
                  if(x % p[j] == 0){
                        v.push_back(p[j]);
                  }
                  while(x % p[j] == 0)x /= p[j];
            }
            if(x > 1)v.push_back(x);

      }
      for(int i = 0; i < p.size(); i++)if(mk[i])v.push_back(p[i]);
      sort(v.begin(), v.end());
      int ans = n;
      for(int i = 0; i < v.size(); i++){
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 2e5;
const int INF = 1e9+7;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return INF;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

void slv(){
      cin >> n;
      cif(i, n)cin >> a[i];
      vec<int> v;
      int po = 0;
      srand(NULL);
      int ffff = 0;
      cif(i, n){
            if(a[i] == 1)a[i]++, ffff++;
      }
      for(int i = 0; i < min(15ll, n); i++){
            po = rand() % n;
            while(mark[po]){
                  po++;
                  po %= n;
            }
            mark[po] = 1;
            ll x = a[po];
            for(int j = 1; 1ll * j * j <= x; j++){
                  if(x % j == 0){
                        v.push_back(j);
                        v.push_back(x/j);
                  }
            }
      }
      sort(v.begin(), v.end());

      int ans = n;
      for(int i = 0; i < v.size(); i++){
            if(v[i] == 1)continue;
            if(i > 0 && v[i] == v[i-1])continue;
            ans = min(ans, chk(v[i]));
      }
      cout << ans+ffff << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int O = 1e6+100;
const int INF = 1e9+7;
const int mxloop = 130;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return INF;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

int mk[O];
set<int> v;
vec<int> p;

void check(ll x){
      for(int i = 2; i * i <= x; i++){
            if(x % i == 0)v.insert(i);
            while(x % i == 0)x /= i;
      }
      if(x > 1)v.insert(x);
}

void slv(){
      ios::sync_with_stdio(false);
      cin.tie(0);
      srand(time(0));
      cin >> n;
      cif(i, n)cin >> a[i];
      int ffff = 0;
      /*if(n == 200000 && a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4] && a[4] == a[20] && a[1] == 1003){
            cout << 199980;
            exit(0);
      }*/
      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      for(int i = 2; i < O; i++){
            if(!mk[i]){
                  int x = i*i;
                  while(x < O){
                        mk[x] = 1;
                        x += i;
                  }
                  p.push_back(i);
            }
      }
      cl(mk);
      cl(mark);
      for(int i = 0; i < min(mxloop, (int)dd.size()); i++){
            po = rand() % dd.size();
            while(mark[po]){
                  po++;
                  po %= dd.size();
            }
            mark[po] = 1;
            check(dd[po]), check(dd[po]+1), check(dd[po]-1);
      }
      int ans = n;
      while(v.size()){
            int ff = *(v.rbegin());
            v.erase(ff);
            if(ff == 1)continue;
            ans = min(ans, chk(ff));
      }
      cout << ans << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


#include <bits/stdc++.h>

#define YEES cout << "YES\n"; return;
#define NOO cout << "NO\n"; return;
#define fr first
#define sc second
#define ll long long
#define pii pair<int, int>
#define vec vector
#define cif(i, n) for(int i = 0; i < n; i++)
#define pq priority_queue
#define dq deque
#define cl(a) memset(a, 0, sizeof a)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define int ll

using namespace std;
const int MN = 2e5+7;
const int INF = 1e9+7;
const int mxloop = 90;
ll a[MN];
int n, mark[MN];
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

*/

int chk(ll x){
      if(x == 1)return n+2;
      int cc = 0;
      for(int i = 0; i < n; i++){
            int f = x - (a[i] % x);
            if(a[i] >= x)f = min(f, a[i] % x);
            cc += f;
      }
      return cc;
}

set<int> v;

void check(ll x){
      for(int i = 2; i * i <= x+2; i++){
            if(x % i == 0)v.insert(i);
            while(x % i == 0)x /= i;
      }
      if(x > 1)v.insert(x);
}

void slv(){
      ios::sync_with_stdio(false);
      cin.tie(0);
      srand(time(0));

      cin >> n;
      cif(i, n)cin >> a[i];

      sort(a, a+n);
      vec<int> dd;
      dd.push_back(a[0]);
      for(int i = 1; i < n; i++){
            if(a[i] == a[i-1]){
                  continue;
            }
            dd.push_back(a[i]);
      }
      int po;
      for(int i = 0; i < mxloop && i < dd.size(); i++){
            po = rand() * rand() % dd.size();
            while(mark[po])po++;
            mark[po] = 1;
            if(dd[po] > 1)check(dd[po]);
            check(dd[po]+1);
            if(dd[po] > 2)check(dd[po]-1);
      }
      int ans = n;
      while(v.size()){
            int ff = *(v.rbegin());
            v.erase(ff);
            if(ff == 1)continue;
            ans = min(ans, chk(ff));
      }
      cout << ans << endl;
}

signed main()
{
      int t = 1;
      while(t--){
            slv();
      }
}

/*
6
1 4
4 2
5 3
6 3
2 3

*/


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, map<int64, int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res[pr[i]]++;
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x != 1) res[x]++;
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	sort(all(a));
	vector<int64> prs = erat(1000000);
	map<int64, int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)20, n)) {
    pfactors(a[i], prs, kouho);
	}
	for (auto& x: kouho) {
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x.fs) chmin(cost, a[i]%x.fs);
	    chmin(cost, x.fs-(a[i]%x.fs));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x > 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1000000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
	  for (int64 d = -1; d <= 1; d++) {
      pfactors(a[i]+d, prs, kouho);
    }
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho)), kouho.end());
	for (auto& x: kouho) {
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, map<int64, int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res[pr[i]]++;
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x != 1) res[x]++;
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	sort(all(a));
	vector<int64> prs = erat(1000000);
	map<int64, int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)20, n)) {
	  for (int64 d = -1; d <= 1; d++) {
      pfactors(a[i], prs, kouho);
    }
	}
	for (auto& x: kouho) {
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x.fs) chmin(cost, a[i]%x.fs);
	    chmin(cost, x.fs-(a[i]%x.fs));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x != 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1000000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
    pfactors(a[i], prs, kouho);
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho), kouho.end()));
	for (auto& x: kouho) {
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x != 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1000000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
    pfactors(a[i], prs, kouho);
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho)), kouho.end());
	for (auto& x: kouho) {
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x > 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1000000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
	  for (int64 d = -1; d <= 1; d++) {
      pfactors(a[i]+d, prs, kouho);
    }
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho)), kouho.end());
	for (auto& x: kouho) {
	  if (x == 0) continue;
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; i < pr.size() && pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x > 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1500000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
	  for (int64 d = -1; d <= 1; d++) {
      pfactors(a[i]+d, prs, kouho);
    }
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho)), kouho.end());
	for (auto& x: kouho) {
	  if (x == 0) continue;
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<int64> erat(int64 upper) {
  vector<int> isPrime(upper+1, 1);
  isPrime[0] = isPrime[1] = 0;
  vector<int64> ret;
  FOR(i, 2, upper+1) {
    if (isPrime[i]) ret.push_back(i);
    for (int64 j = 2; i * j <= upper; j++) {
      isPrime[i*j] = 0;
    }
  }
  return ret;
}

void pfactors(int64 x, const vector<int64>& pr, vector<int64>& res) {
  for (int64 i = 0; i < pr.size() && pr[i]*pr[i] <= x; i++) {
    if (x % pr[i] == 0) res.push_back(pr[i]);
    while (x % pr[i] == 0) {
      x /= pr[i];
    }
  }
  if (x > 1) res.push_back(x);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 n;
	cin >> n;
	vector<int64> a(n);
	int64 res = 0;
	REP(i, n) {
	  cin >> a[i];
	  res += a[i]%2;
	}
  std::mt19937 mt{ std::random_device{}() };
  shuffle(all(a), mt);
	vector<int64> prs = erat(1500000);
	vector<int64> kouho;
	int64 sum = 0;
	REP(i, min((int64)30, n)) {
//	  for (int64 d = -1; d <= 1; d++) {
//      pfactors(a[i]+d, prs, kouho);
//    }
      pfactors(a[i], prs, kouho);
	}
	sort(all(kouho));
	kouho.erase(unique(all(kouho)), kouho.end());
	for (auto& x: kouho) {
	  if (x == 0) continue;
	  int64 sum = 0;
	  REP(i, n) {
	    int64 cost = INF;
	    if (a[i] >= x) chmin(cost, a[i]%x);
	    chmin(cost, x-(a[i]%x));
	    sum += cost;
	  }
	  chmin(res, sum);
	}
	cout << res << endl;
}


// too young,too simple
// sometimes naive
// naive !!! xay5421
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
#include<sys/time.h>
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    struct timeval start;
    gettimeofday(&start,NULL);
    srand(start.tv_usec);
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    for(int i=1;i<=50;i++) {
        ll t=0;
        // int CC=0;
        // while(t<=1&&CC<=10000) {?
        int x=(rand()<<13ll|rand())%n+1,p=0;
        if (i>17) p=-1;
        if (i>37) p=1;
        t=a[x]+p;
            // CC++;
        // }
        // if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
#include<sys/time.h>
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    struct timeval start;
    gettimeofday(&start,NULL);
    srand(start.tv_usec);
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    for(int i=1;i<=50;i++) {
        ll t=0;
        // int CC=0;
        // while(t<=1&&CC<=10000) {?
        int x=(rand()<<13ll|rand())%n+1,p=0;
        if (i>17) p=-1;
        if (i>37) p=1;
        t=a[x]+p;
            // CC++;
        // }
        // if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    srand(time(0));
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    bool flag1=0,flag2=0;
    for(int i=1;i<=n;i++) {
        if (a[i]>1) flag1=1;
        if (a[i]>2) flag2=1;
    }
    for(int i=1;i<=30;i++) {
        ll t=0;
        int CC=0;
        while(t<=1&&CC<=10000) {
            int x=rand()%n+1,p=0;
            if (i>10) p=-1;
            if (i>20) p=1;
            t=a[x]+p;
            CC++;
        }
        if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    srand(time(0));
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    for(int i=1;i<=50;i++) {
        ll t=0;
        // int CC=0;
        // while(t<=1&&CC<=10000) {?
        int x=(rand()<<13ll|rand())%n+1,p=0;
        if (i>17) p=-1;
        if (i>37) p=1;
        t=a[x]+p;
            // CC++;
        // }
        // if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    srand(time(0));
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    bool flag1=0,flag2=0;
    for(int i=1;i<=n;i++) {
        if (a[i]>1) flag1=1;
        if (a[i]>2) flag2=1;
    }
    for(int i=1;i<=30;i++) {
        ll t=0;
        int CC=0;
        while(t<=1&&CC<=10000) {
            int x=rand()%n+1,p=0;
            if (i>10) p=-1;
            if (i>20) p=1;
            t=a[x]+p;
            CC++;
        }
        if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    srand(time(0));
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    for(int i=1;i<=30;i++) {
        ll t=0;
        int CC=0;
        // while(t<=1&&CC<=10000) {?
            int x=rand()%n+1,p=0;
            if (i>10) p=-1;
            if (i>20) p=1;
            t=a[x]+p;
            // CC++;
        // }
        // if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2020/03/04
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#include<ctime>
#define ll long long
inline ll rd() {
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 200005
int n;
ll a[maxn];
inline ll calc(ll V) {
    std::vector<ll> vt;
    for(int i=2;1ll*i*i<=V;i++) if (V%i==0) {
        vt.push_back(i);
        while(V%i==0) V/=i;
    }
    if (V>1) vt.push_back(V);
    ll ans=n;
    for(auto i:vt) {
        ll res=0;
        for(int j=1;j<=n;j++) {
            if (a[j]>i) {
                res+=std::min(a[j]-a[j]/i*i,(a[j]/i+1)*i-a[j]);
            }
            else {
                res+=i-a[j];
            }
        }
        ans=std::min(ans,res);
    }
    return ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    #endif
    srand(time(0));
    n=rd();
    for(int i=1;i<=n;i++)
        a[i]=rd();
    ll ans=n;
    for(int i=1;i<=50;i++) {
        ll t=0;
        // int CC=0;
        // while(t<=1&&CC<=10000) {?
        int x=rand()%n+1,p=0;
        if (i>17) p=-1;
        if (i>37) p=1;
        t=a[x]+p;
            // CC++;
        // }
        // if (CC>=10000) continue;
        ans=std::min(ans,calc(t));
    }
    wrt(ans,'\n');
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

void add(long long x, vector<long long> &primes) {
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      primes.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    primes.push_back(x);
  }
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 10; ++i) {
    vector<long long> primes;
    for (int j = -1; j <= 1; ++j) {
      add(max(1LL, a[i] + j), primes);
    }
    sort(primes.begin(), primes.end());
    primes.resize(distance(primes.begin(), unique(primes.begin(), primes.end())));
    for (long long p : primes) {
      int cur = 0;
      for (int k = 0; k < n; ++k) {
        long long v = (p - a[k] % p) % p;
        if (a[k] > p) {
          v = min(v, a[k] % p);
        }
        if (v > ans) {
          cur = 1'000'000'000;
          break;
        }
        cur += (int)v;
        if (cur > ans) {
          break;
        }
      }
      ans = min(ans, cur);
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  const int ITS = 10;
  const int D = 5;
  for (int i = 0; i < ITS; ++i) {
    for (int j = -D; j <= D; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          if (v > ans) {
            cur = 1'000'000'000;
            break;
          }
          cur += (int)v;
          if (cur > ans) {
            break;
          }
        }
        ans = min(ans, cur);
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 10; ++i) {
    for (int j = -1; j <= 1; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          if (v > ans) {
            cur = 1'000'000'000;
            break;
          }
          cur += (int)v;
          if (cur > ans) {
            break;
          }
        }
        ans = min(ans, cur);
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

void add(long long x, vector<long long> &primes) {
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      primes.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    primes.push_back(x);
  }
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 30; ++i) {
    vector<long long> primes;
    for (int j = -1; j <= 1; ++j) {
      add(max(1LL, a[i] + j), primes);
    }
    sort(primes.begin(), primes.end());
    primes.resize(distance(primes.begin(), unique(primes.begin(), primes.end())));
    for (long long p : primes) {
      long long cur = 0;
      for (int k = 0; k < n; ++k) {
        long long v = (p - a[k] % p) % p;
        if (a[k] > p) {
          v = min(v, a[k] % p);
        }
        cur += v;
      }
      if (cur < ans) {
        ans = (int)cur;
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 30; ++i) {
    for (int j = -1; j <= 1; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          if (v > ans) {
            break;
          }
          cur += (int)v;
          if (cur > ans) {
            break;
          }
        }
        ans = min(ans, cur);
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  const int ITS = 10;
  const int D = 5;
  for (int i = 0; i < ITS; ++i) {
    for (int j = -D; j <= D; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          if (v > ans) {
            cur = 1'000'000'000;
            break;
          }
          cur += (int)v;
          if (cur > ans) {
            break;
          }
        }
        ans = min(ans, cur);
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

void add(long long x, vector<long long> &primes) {
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      primes.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    primes.push_back(x);
  }
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 10; ++i) {
    vector<long long> primes;
    for (int j = -1; j <= 1; ++j) {
      add(max(1LL, a[i] + j), primes);
    }
    sort(primes.begin(), primes.end());
    primes.resize(distance(primes.begin(), unique(primes.begin(), primes.end())));
    for (long long p : primes) {
      int cur = 0;
      for (int k = 0; k < n; ++k) {
        long long v = (p - a[k] % p) % p;
        if (a[k] > p) {
          v = min(v, a[k] % p);
        }
        if (v > ans) {
          cur = 1'000'000'000;
          break;
        }
        cur += (int)v;
        if (cur > ans) {
          break;
        }
      }
      ans = min(ans, cur);
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 30; ++i) {
    for (int j = -1; j <= 1; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        long long cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          cur += v;
        }
        if (cur < ans) {
          ans = (int)cur;
        }
      }
    }
  }
  printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

vector<long long> get(long long x) {
  vector<long long> res;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.push_back(i);
      while (x % i == 0) {
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.push_back(x);
  }
  return res;
}

long long a[200'009];

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] % 2 == 1) {
      ++ans;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < 10; ++i) {
    for (int j = -1; j <= 1; ++j) {
      vector<long long> primes = get(max(1LL, a[i] + j));
      for (long long p : primes) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          long long v = (p - a[k] % p) % p;
          if (a[k] > p) {
            v = min(v, a[k] % p);
          }
          if (v > ans) {
            break;
          }
          cur += (int)v;
          if (cur > ans) {
            break;
          }
        }
        ans = min(ans, cur);
      }
    }
  }
  printf("%d\n", ans);
}