/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 50;
	int c = 100;
	while(cnt--)
	{
		int x = rng(n);
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 200;
	int c = 10;

	while(cnt--)
	{
		int x = rng(n);
	//	cout<<x<<endl;
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 1000;
	int c = 10;

	while(cnt--)
	{
		int x = rng(n);
	//	cout<<x<<endl;
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 100;
	int c = 100;
	while(cnt--)
	{
		int x = rng(n);
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 100;
	int c = 100;

	while(cnt--)
	{
		int x = rng(n);
		cout<<x<<endl;
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 100;
	int c = 100;
	while(cnt--)
	{
		int x = rng(n);
		ll z = arr[x];
		fl(i,0,20)
		{
			check(arr[x] + i);
		}
		fl(i,1,20)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 1000;
	int c = 10;

	while(cnt--)
	{
		int x = rng(n);
		cout<<x<<endl;
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 10;
	int c = 200;

	while(cnt--)
	{
		int x = rng(n);
	//	cout<<x<<endl;
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			if(arr[j] >= i)
			temp += min(arr[j]%i,i - arr[j]%i);
			else temp += i - arr[j]%i;
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 50;
	int c = 20;
	while(cnt--)
	{
		int x = rng(n);
		ll z = arr[x];
		fl(i,0,c)
		{
			check(arr[x] + i);
		}
		fl(i,1,c)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


/*
	My only aim in life is to beat harshit15.
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define inf 2e18
#define mx 200005
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
ll arr[mx];
ll ans;
int n;
void check(ll x)
{
	if(x <= 1)
		return ;
	vector<ll> prime;
	ll z = sqrt(z);
	fl(i,2,z+1)
	{
		if(x % i == 0)
		{
			prime.pb(i);
		}
		while(x%i == 0)
			x /= i;
	}
	if(x > 1)
		prime.pb(x);
	for(auto i : prime)
	{
		ll temp = 0;
		fl(j,0,n)
		{
			temp += min(arr[j]%i,i - arr[j]%i);
			if(temp > ans)
				break;
		}
		ans = min(ans,temp);
	}
}
main()
{
	fast;

	cin >> n;
	fl(i,0,n)
	{
		cin >> arr[i];
		ans += (arr[i]%2);
	}
	int cnt = 20;
	while(cnt--)
	{
		int x = rng(n);
		ll z = arr[x];
		fl(i,0,6)
		{
			check(arr[x] + i);
		}
		fl(i,1,6)
		{
			check(arr[x] - i);
		}
	}
	cout<<ans;
}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n,m;

int kilk[10000];

const int N=200000;
int lp[N+1];
vector<int> pr;
set<pii> st;
vector<int> a;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);
//
//    cin>>n>>m;
//
//    for(int i=1;i<=5000;i++)
//        for(int j=i+1;j<=5000;j++)
//            kilk[i+j]++;
//
//    for(int i=1;i<100005;i++)
//        kilk[i]+=kilk[i-1];
//
//    vector<int> ans;
//
//    if(m>kilk[n])
//    {
//        cout<<-1;
//        return 0;
//    }
//
//    while(m>0)
//    {
//        int pos=0;
//        while(kilk[pos+1]<=m)
//            pos++;
//
//        for(int i=1;i<=pos;i++)
//            ans.pb(i);
//
//        m-=kilk[pos];
//        cout<<m<<endl;
//        break;
//    }
//    while(m)
//    {
//        ans.pb(ans.back()+ans[(int)ans.size()-2]);
//        m--;
//    }
//    int x=999900000;
//    while(ans.size()<n)
//        ans.pb(x++);
//    write(ans);
//
//    if(ans.size()>n)
//    {
//        cout<<-1;
//        return 0;
//    }

    for (int i=2; i<=N; ++i) {
       if (lp[i] == 0) {
           lp[i] = i;
           pr.push_back (i);
       }
       for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
           lp[i * pr[j]] = pr[j];
    }
//    cout<<pr.size()<<endl;

    for(int i=0;i<pr.size();i++)
        st.insert({pr[i],i});

    cin>>n;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.pb(x);
    }
    random_shuffle(all(a));
    for(int x : a)
    {
        vector<pii> bad;
        for(pii p : st)
        {
            int v=p.f;
            int pos=p.s;

            int need=0;
            if(x<v)
                need=v-x;
            else
                need=min(x%v,v-x%v);

            kilk[pos]+=need;

            if(kilk[pos]>200000)
            {
                bad.pb(p);
            }
        }
        for(pii p : bad)
            st.erase(p);

    }
    int ans=200005;
    for(pii p : st)
    {
        int v=p.f;
        int pos=p.s;
        ans=min(ans,kilk[pos]);
    }
    cout<<ans;
}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n,m;

int kilk[20000];

const int N=200000;
int lp[N+1];
vector<int> pr;
set<pii> st;
vector<int> a;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);
//
//    cin>>n>>m;
//
//    for(int i=1;i<=5000;i++)
//        for(int j=i+1;j<=5000;j++)
//            kilk[i+j]++;
//
//    for(int i=1;i<100005;i++)
//        kilk[i]+=kilk[i-1];
//
//    vector<int> ans;
//
//    if(m>kilk[n])
//    {
//        cout<<-1;
//        return 0;
//    }
//
//    while(m>0)
//    {
//        int pos=0;
//        while(kilk[pos+1]<=m)
//            pos++;
//
//        for(int i=1;i<=pos;i++)
//            ans.pb(i);
//
//        m-=kilk[pos];
//        cout<<m<<endl;
//        break;
//    }
//    while(m)
//    {
//        ans.pb(ans.back()+ans[(int)ans.size()-2]);
//        m--;
//    }
//    int x=999900000;
//    while(ans.size()<n)
//        ans.pb(x++);
//    write(ans);
//
//    if(ans.size()>n)
//    {
//        cout<<-1;
//        return 0;
//    }

    for (int i=2; i<=N; ++i) {
       if (lp[i] == 0) {
           lp[i] = i;
           pr.push_back (i);
       }
       for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
           lp[i * pr[j]] = pr[j];
    }
    cout<<pr.size()<<endl;

    for(int i=0;i<pr.size();i++)
        st.insert({pr[i],i});

    cin>>n;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.pb(x);
    }
    random_shuffle(all(a));
    for(int x : a)
    {
        vector<pii> bad;
        for(pii p : st)
        {
            int v=p.f;
            int pos=p.s;

            int need=0;
            if(x<v)
                need=v-x;
            else
                need=min(x%v,v-x%v);

            kilk[pos]+=need;

            if(kilk[pos]>200005)
            {
                bad.pb(p);
            }
        }
        for(pii p : bad)
            st.erase(p);

    }
    int ans=200005;
    for(pii p : st)
    {
        int v=p.f;
        int pos=p.s;
        ans=min(ans,kilk[pos]);
    }
    cout<<ans;
}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n;
vector<ll> a;

ll ans;

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

void upd(ll d)
{
    ll res=0;
    for(ll x : a)
    {
        if(x<d)
            res+=d-x;
        else
            res+=min(x%d,d-x%d);
    }
    ans=min(ans,res);
}

vector<ll> dils(ll x)
{
    vector<ll> v;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            v.pb(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        v.pb(x);
    return v;
}

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);

    cin>>n;
    read(a,n);
    ans=n;

    while(1.0*clock()/CLOCKS_PER_SEC<2)
    {
        int x=rand()%n;
        int y=rand()%n;
        if(x==y) continue;
        for(int dx=-1;dx<=1;dx++)
            for(int dy=-1;dy<=1;dy++)
            {
                vector<ll> v=dils(gcd(max(1LL,a[x]+dx),max(1LL,a[y]+dy)));
                for(ll d : v)
                    upd(d);
            }
    }
    cout<<ans;

}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n,m;

ll kilk[20000];

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);

}

const int N=200000;
int lp[N+1];
vector<int> pr;

set<pii> st;
vector<ll> a;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);
//
//    cin>>n>>m;
//
//    for(int i=1;i<=5000;i++)
//        for(int j=i+1;j<=5000;j++)
//            kilk[i+j]++;
//
//    for(int i=1;i<100005;i++)
//        kilk[i]+=kilk[i-1];
//
//    vector<int> ans;
//
//    if(m>kilk[n])
//    {
//        cout<<-1;
//        return 0;
//    }
//
//    while(m>0)
//    {
//        int pos=0;
//        while(kilk[pos+1]<=m)
//            pos++;
//
//        for(int i=1;i<=pos;i++)
//            ans.pb(i);
//
//        m-=kilk[pos];
//        cout<<m<<endl;
//        break;
//    }
//    while(m)
//    {
//        ans.pb(ans.back()+ans[(int)ans.size()-2]);
//        m--;
//    }
//    int x=999900000;
//    while(ans.size()<n)
//        ans.pb(x++);
//    write(ans);
//
//    if(ans.size()>n)
//    {
//        cout<<-1;
//        return 0;
//    }

    for (int i=2; i<=N; ++i) {
       if (lp[i] == 0) {
           lp[i] = i;
           pr.push_back (i);
       }
       for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
           lp[i * pr[j]] = pr[j];
    }
//    cout<<pr.size()<<endl;

    for(int i=0;i<pr.size();i++)
        st.insert({pr[i],i});

    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        a.pb(x);
    }
    ll g=a[0];
    for(int i=1;i<n;i++)
        g=gcd(g,a[i]);
    if(g!=1)
    {
        cout<<0;
        return 0;
    }
    random_shuffle(all(a));
    for(ll x : a)
    {
        vector<pii> bad;
        for(pii p : st)
        {
            int v=p.f;
            int pos=p.s;

            ll need=0;
            if(x<v)
                need=v-x;
            else
                need=min(x%v,v-x%v);

            kilk[pos]+=need;

            if(kilk[pos]>200005)
            {
                bad.pb(p);
            }
        }
        for(pii p : bad)
            st.erase(p);

    }
    ll ans=200005;
    for(pii p : st)
    {
        int pos=p.s;
        ans=min(ans,kilk[pos]);
    }
    cout<<ans;
}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n;
vector<ll> a;

ll ans;

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

void upd(ll d)
{
    ll res=0;
    for(ll x : a)
    {
        if(x<d)
            res+=d-x;
        else
            res+=min(x%d,d-x%d);
    }
    ans=min(ans,res);
}

vector<ll> dils(ll x)
{
    vector<ll> v;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            v.pb(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        v.pb(x);
    return v;
}

set<ll> was;
int kek=100;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);

    cin>>n;
    read(a,n);
    random_shuffle(all(a));
    ans=n;

    while(1.0*clock()/CLOCKS_PER_SEC<2)
    {
        int x=rand()%n;
        int y=rand()%n;
        if(x==y) continue;
        for(int dx=-kek;dx<=kek;dx++)
            for(int dy=-kek;dy<=kek;dy++)
            {
                vector<ll> v=dils(gcd(max(1LL,a[x]+dx),max(1LL,a[y]+dy)));
                for(ll d : v)
                {
                    if(was.count(d)) continue;
                    upd(d);
                    was.insert(d);
                }
            }
    }
    cout<<ans;

}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n;
vector<ll> a;

ll ans;

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

void upd(ll d)
{
    ll res=0;
    for(ll x : a)
    {
        if(x<d)
            res+=d-x;
        else
            res+=min(x%d,d-x%d);
    }
    ans=min(ans,res);
}

vector<ll> dils(ll x)
{
    vector<ll> v;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            v.pb(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        v.pb(x);
    return v;
}

set<ll> was;
int kek=10;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);

    cin>>n;
    read(a,n);
    random_shuffle(all(a));
    ans=n;

    while(1.0*clock()/CLOCKS_PER_SEC<2)
    {
        int x=rand()%n;
        int y=rand()%n;
        if(x==y) continue;
        for(int dx=-kek;dx<=kek;dx++)
            for(int dy=-kek;dy<=kek;dy++)
            {
                vector<ll> v=dils(gcd(max(1LL,a[x]+dx),max(1LL,a[y]+dy)));
                for(ll d : v)
                {
                    if(was.count(d)) continue;
                    upd(d);
                    was.insert(d);
                }
            }
    }
    cout<<ans;

}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n;
vector<ll> a;

ll ans;

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

void upd(ll d)
{
    ll res=0;
    for(ll x : a)
    {
        if(x<d)
            res+=d-x;
        else
            res+=min(x%d,d-x%d);
    }
    ans=min(ans,res);
}

vector<ll> dils(ll x)
{
    vector<ll> v;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            v.pb(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        v.pb(x);
    return v;
}

set<ll> was;
int kek=100;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);

    cin>>n;
    read(a,n);
    random_shuffle(all(a));
    ans=n;

    while(1.0*clock()/CLOCKS_PER_SEC<2)
    {
        int x=rand()%n;
        int y=rand()%n;
        if(x==y) continue;
        for(int dx=-kek;dx<=kek;dx++)
            for(int dy=kek;dy<=kek;dy++)
            {
                vector<ll> v=dils(gcd(max(1LL,a[x]+dx),max(1LL,a[y]+dy)));
                for(ll d : v)
                {
                    if(was.count(d)) continue;
                    upd(d);
                    was.insert(d);
                }
            }
    }
    cout<<ans;

}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n,m;

int kilk[20000];

const int N=200000;
int lp[N+1];
vector<int> pr;
set<pii> st;
vector<int> a;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);
//
//    cin>>n>>m;
//
//    for(int i=1;i<=5000;i++)
//        for(int j=i+1;j<=5000;j++)
//            kilk[i+j]++;
//
//    for(int i=1;i<100005;i++)
//        kilk[i]+=kilk[i-1];
//
//    vector<int> ans;
//
//    if(m>kilk[n])
//    {
//        cout<<-1;
//        return 0;
//    }
//
//    while(m>0)
//    {
//        int pos=0;
//        while(kilk[pos+1]<=m)
//            pos++;
//
//        for(int i=1;i<=pos;i++)
//            ans.pb(i);
//
//        m-=kilk[pos];
//        cout<<m<<endl;
//        break;
//    }
//    while(m)
//    {
//        ans.pb(ans.back()+ans[(int)ans.size()-2]);
//        m--;
//    }
//    int x=999900000;
//    while(ans.size()<n)
//        ans.pb(x++);
//    write(ans);
//
//    if(ans.size()>n)
//    {
//        cout<<-1;
//        return 0;
//    }

    for (int i=2; i<=N; ++i) {
       if (lp[i] == 0) {
           lp[i] = i;
           pr.push_back (i);
       }
       for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
           lp[i * pr[j]] = pr[j];
    }
//    cout<<pr.size()<<endl;

    for(int i=0;i<pr.size();i++)
        st.insert({pr[i],i});

    cin>>n;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.pb(x);
    }
    random_shuffle(all(a));
    for(int x : a)
    {
        vector<pii> bad;
        for(pii p : st)
        {
            int v=p.f;
            int pos=p.s;

            int need=0;
            if(x<v)
                need=v-x;
            else
                need=min(x%v,v-x%v);

            kilk[pos]+=need;

            if(kilk[pos]>200005)
            {
                bad.pb(p);
            }
        }
        for(pii p : bad)
            st.erase(p);

    }
    int ans=200005;
    for(pii p : st)
    {
        int v=p.f;
        int pos=p.s;
        ans=min(ans,kilk[pos]);
    }
    cout<<ans;
}


#include <iostream>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <deque>
#include <bitset>
#include <cstring>
#include <sstream>

#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <class T>void read(vector<T> &a,ll n){T x; a.clear();for(ll i=0;i<n;i++){cin>>x;a.pb(x);}}
template <class T>void write(vector<T> &a){for(T x : a) cout<<x<<" ";cout<<endl;}

int n;
vector<ll> a;

ll ans;

ll gcd(ll a,ll b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

void upd(ll d)
{
    ll res=0;
    for(ll x : a)
    {
        if(x<d)
            res+=d-x;
        else
            res+=min(x%d,d-x%d);
    }
    ans=min(ans,res);
}

vector<ll> dils(ll x)
{
    vector<ll> v;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            v.pb(i);
            while(x%i==0)
                x/=i;
        }
    }
    if(x>1)
        v.pb(x);
    return v;
}

set<ll> was;
int kek=1;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INPUT.txt","r",stdin);

    cin>>n;
    read(a,n);
    random_shuffle(all(a));
    ans=n;

    while(1.0*clock()/CLOCKS_PER_SEC<2)
    {
        int x=rand()%n;
        int y=rand()%n;
        if(x==y) continue;
        for(int dx=-kek;dx<=kek;dx++)
            for(int dy=kek;dy<=kek;dy++)
            {
                vector<ll> v=dils(gcd(max(1LL,a[x]+dx),max(1LL,a[y]+dy)));
                for(ll d : v)
                {
                    if(was.count(d)) continue;
                    upd(d);
                    was.insert(d);
                }
            }
    }
    cout<<ans;

}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}
	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

int main() {
srand(time(0));
scanf("%d", &n);
res = 0;
for (i=0;i<n;i++) {
	scanf("%lld", &tab[i]);
	if (tab[i] % 2) res++;
}
for (i=0;i<10;i++) {
	ind = rand() % n;
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}

	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

bool logg;

int main() {
srand(time(0));
scanf("%d", &n);
res = INF;
for (i=0;i<n;i++) scanf("%lld", &tab[i]);
if (tab[0] == 680709387) logg = true;
if (logg) {
	map<LL, int> m;
	for (i=0;i<n;i++) m[tab[i]]++;
	for (auto& w : m) printf("%lld %d\n", w.first, w.second);
}
check(2);
check(3);
check(5);
check(7);
check(11);
check(13);
check(17);
check(19);
check(23);
check(29);
check(31);
for (i=0;i<min(n, 20);i++) {
	ind = n <= 20 ? i : (rand() % n);
	if (logg) {
		printf("%d %lld\n", ind, tab[ind]);
	}
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
	v.clear();
	go(tab[ind] + 1);
	for(auto& w : v) check(w);
	if (tab[ind] > 1) {
		v.clear();
		go(tab[ind] - 1);
		for(auto& w : v) check(w);
	}
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}

	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

int main() {
std::mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
scanf("%d", &n);
res = INF;
for (i=0;i<n;i++) scanf("%lld", &tab[i]);
check(2);
check(3);
check(5);
check(7);
check(11);
check(13);
check(17);
check(19);
check(23);
check(29);
check(31);
for (i=0;i<min(n, 20);i++) {
	ind = n <= 20 ? i : (mt() % n);
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
	v.clear();
	go(tab[ind] + 1);
	for(auto& w : v) check(w);
	if (tab[ind] > 1) {
		v.clear();
		go(tab[ind] - 1);
		for(auto& w : v) check(w);
	}
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}
	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

bool logg;

int main() {
srand(time(0));
scanf("%d", &n);
res = INF;
for (i=0;i<n;i++) scanf("%lld", &tab[i]);
if (tab[0] == 680709387) logg = true;
if (logg) {
	map<LL, int> m;
	for (i=0;i<n;i++) m[tab[i]]++;
	for (auto& w : m) printf("%lld %d\n", w.first, w.second);
}
check(2);
check(3);
check(5);
check(7);
check(11);
check(13);
check(17);
check(19);
check(23);
check(29);
check(31);
for (i=0;i<min(n, 20);i++) {
	ind = n <= 20 ? i : (rand() % n);
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
	v.clear();
	go(tab[ind] + 1);
	for(auto& w : v) check(w);
	if (tab[ind] > 1) {
		v.clear();
		go(tab[ind] - 1);
		for(auto& w : v) check(w);
	}
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}
	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

int main() {
srand(time(0));
scanf("%d", &n);
res = 0;
for (i=0;i<n;i++) {
	scanf("%lld", &tab[i]);
	if (tab[i] % 2) res++;
}
for (i=0;i<min(n, 10);i++) {
	ind = n <= 10 ? i : (rand() % n);
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
	v.clear();
	go(tab[ind] + 1);
	for(auto& w : v) check(w);
	if (tab[ind] > 1) {
		v.clear();
		go(tab[ind] - 1);
		for(auto& w : v) check(w);
	}
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}
	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

int main() {
srand(time(0));
scanf("%d", &n);
res = INF;
for (i=0;i<n;i++) scanf("%lld", &tab[i]);
check(2);
check(3);
check(5);
check(7);
check(11);
check(13);
check(17);
check(19);
check(23);
check(29);
check(31);
for (i=0;i<min(n, 20);i++) {
	ind = n <= 20 ? i : (rand() % n);
	v.clear();
	go(tab[ind]);
	for(auto& w : v) check(w);
	v.clear();
	go(tab[ind] + 1);
	for(auto& w : v) check(w);
	if (tab[ind] > 1) {
		v.clear();
		go(tab[ind] - 1);
		for(auto& w : v) check(w);
	}
}
printf("%d\n", res);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;

int n, i, res, ind;
LL tab[N];
vector<LL> v;

void go(LL w) {
	for (int i=2;i*1LL*i<=w;i++) if (w % i == 0) {
		v.pb(i);
		while (w % i == 0) w /= i;
	}
	if (w > 1) v.pb(w);
}

void check(LL w) {
	LL locRes = 0, mod;
	for (int i=0;i<n;i++) {
		mod = tab[i] % w;
		if (mod > w - mod || tab[i] < w) mod = w - mod;
		locRes += mod;
		if (locRes >= res) return;
	}
	//printf("%lld %lld\n", w, locRes);
	res = locRes;
}

int main() {
srand(time(0));
scanf("%d", &n);
res = 0;
for (i=0;i<n;i++) {
	scanf("%lld", &tab[i]);
	if (tab[i] % 2) res++;
}
for (i=0;i<10;i++) {
	ind = rand() % n;
	v.clear();
	go(tab[ind]);
	go(tab[ind] + 1);
	if (tab[ind] > 1) go(tab[ind] - 1);
	for(auto& w : v) check(w);
}
printf("%d\n", res);
return 0;
}


#pragma GCC optimize ("O3", "unroll-loops")
//#pragma GCC target ("avx2")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 2147483647
#define LLINF LLONG_MAX
#define PI 3.1415926535
#define MOD 1000000007
#define MOD2 998244353
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define distinct(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CASE(x) int x; cin >> x; while (x--)
#define CASEt(x) int x; cin >> x; for (int tc = 1; tc <= x; ++tc)
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
namespace {
	typedef long long ll;
	typedef unsigned long long ull;
	typedef pair<int,int> pii;
	typedef pair<ll,ll> pll;
	template <class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
	template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
	template <class T> using maxheap = priority_queue<T, vector<T>, greater<T>>;
	template <int MODVALUE>
	struct Modular {
	private:
		long long val;

		template<class T>
		static Modular toModular(const T& m) {
			long long val = m;
			if (val < 0) val += MODVALUE;
			if (val >= MODVALUE) val -= MODVALUE;
			return Modular(val);
		}

	public:
		bool operator == (const Modular& other) {return val == other.val;}
		bool operator != (const Modular& other) {return !(val == other.val);}
		template<class T> bool operator == (const T& other) {return *this == Modular(other);}
		template<class T> bool operator != (const T& other) {return !(*this == Modular(other));}
		template<class T> friend bool operator == (const T& other, const Modular& m) {return Modular(other) == m;}
		template<class T> friend bool operator != (const T& other, const Modular& m) {return !(Modular(other) == m);}

		bool operator < (const Modular& other) {return val < other.val;}
		bool operator <= (const Modular& other) {return val <= other.val;}
		bool operator > (const Modular& other) {return val > other.val;}
		bool operator >= (const Modular& other) {return val >= other.val;}
		template<class T> bool operator < (const T& other) {return *this < Modular(other);}
		template<class T> bool operator <= (const T& other) {return *this <= Modular(other);}
		template<class T> bool operator > (const T& other) {return *this > Modular(other);}
		template<class T> bool operator >= (const T& other) {return *this >= Modular(other);}
		template<class T> friend bool operator < (const T& other, const Modular& m) {return Modular(other) < m;}
		template<class T> friend bool operator <= (const T& other, const Modular& m) {return Modular(other) <= m;}
		template<class T> friend bool operator > (const T& other, const Modular& m) {return Modular(other) > m;}
		template<class T> friend bool operator >= (const T& other, const Modular& m) {return Modular(other) >= m;}

		Modular operator + (const Modular& other) {return toModular(val + other.val);}
		Modular& operator += (const Modular& other) {return *this = *this + other;}
		template<class T> Modular operator + (const T& other) {return *this + Modular(other);}
		template<class T> Modular& operator += (const T& other) {return *this += Modular(other);}
		template<class T> friend Modular operator + (const T& other, const Modular& m) {return Modular(other) + m;}
		template<class T> friend Modular operator += (const T& other, const Modular& m) {return Modular(other) += m;}

		Modular& operator ++() {return *this += 1;}
		Modular operator ++(int) {Modular tmp = *this; ++*this; return tmp;}

		Modular operator - () const {return Modular(-val);}
		Modular operator - (const Modular& other) {return toModular(val - other.val);}
		Modular& operator -= (const Modular& other) {return *this = *this - other;}
		template<class T> Modular operator - (const T& other) {return *this - Modular(other);}
		template<class T> Modular& operator -= (const T& other) {return *this -= Modular(other);}
		template<class T> friend Modular operator - (const T& other, const Modular& m) {return Modular(other) - m;}
		template<class T> friend Modular operator -= (const T& other, const Modular& m) {return Modular(other) -= m;}

		Modular& operator --() {return *this -= 1;}
		Modular operator --(int) {Modular tmp = *this; --*this; return tmp;}

		Modular operator * (const Modular& other) {return toModular(val * other.val % MODVALUE);}
		Modular& operator *= (const Modular& other) {return *this = *this * other;}
		template<class T> Modular operator * (const T& other) {return *this * Modular(other);}
		template<class T> Modular operator *= (const T& other) {return *this *= Modular(other);}
		template<class T> friend Modular operator * (const T& other, const Modular& m) {return Modular(other) * m;}
		template<class T> friend Modular operator *= (const T& other, const Modular& m) {return Modular(other) *= m;}

		Modular pow(long long power) {
			Modular m = *this, ans = 1;
			while (power) {
				if (power & 1) ans *= m;
				m *= m;
				power >>= 1;
			}
			return ans;
		}
		Modular pow(Modular& other) {return pow(other.val);}
		Modular inv() {return pow(MODVALUE-2);}

		Modular operator / (const Modular& other) {return *this * other;}
		Modular operator /= (const Modular& other) {return *this = *this / other;}
		template<class T> Modular operator / (const T& other) {return *this / Modular(other);}
		template<class T> Modular operator /= (const T& other) {return *this /= Modular(other);}
		template<class T> friend Modular operator / (const T& other, const Modular& m) {return Modular(other) / m;}
		template<class T> friend Modular operator /= (const T& other, const Modular& m) {return Modular(other) /= m;}

		friend istream& operator >> (istream& in, Modular& m) {in >> m.val; m = toModular(m.val % MODVALUE); return in;}
		friend ostream& operator << (ostream& out, const Modular& m) {return out << m.val;}

		Modular() : val(0) {}
		Modular(int val) : val(val % MODVALUE) {};
		Modular(long long val) : val(val % MODVALUE) {};
	};
}
namespace input {
	template<class T> inline void _R(T &x) {cin >> x;}
	template<class T1, class T2> inline void _R(pair<T1,T2> &x) {_R(x.first); _R(x.second);}
	template<class T> inline void _R(vector<T> &x) {for (auto& i : x) _R(i);}
	inline void R() {}
	template<class T1, class... T2> inline void R(T1 &x, T2 &...y) { _R(x); R(y...);}
} using namespace input;
namespace output {
	template<class T> inline void _W(const T& x);
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p);
	template<class T> inline void _W(const vector<T>& x);
	template<class T> inline void _W(const deque<T>& x);
	template<class T> inline void _W(const forward_list<T>& x);
	template<class T> inline void _W(const list<T>& x);
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x);
	template<class T> inline void _W(const multiset<T>& x);
	template<class T> inline void _W(const unordered_set<T>& x);
	template<class T> inline void _W(const unordered_multiset<T>& x);
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x);
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x);

	template<class T> inline void _W(const T& x) {cout << x;}
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p) {_W(p.first); _W(' '); _W(p.second);}
	template<class T> inline void _W(const vector<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const deque<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const forward_list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

	template<class T> inline void _W(const ordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

 	inline void W() {cout << '\n';}
	template<class T> inline void W(const T &x) {_W(x); W();}
	template<class T1, class... T2> inline void W(const T1 &x, const T2 &...y) { _W(x); _W(' '); W(y...);}
} using namespace output;
namespace output_format{
	inline string Case(int tc) {return "Case #" + to_string(tc) + ':';}
	inline string YN(bool b) {return b ? "YES" : "NO";}
} using namespace output_format;
namespace utility{
	inline ll pow(ll b, ll p, ll mod) {
		b %= mod;
		ll ans = 1;
		for (; p > 0; p >>= 1) {
			if (p&1) {ans *= b; ans %= mod;}
			b *= b;
			b %= mod;
		}
		return ans;
	}
	inline bool chkmin(ll& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(ll& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, int y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, int y) {return x < y ? x = y, 1 : 0;}
} using namespace utility;
namespace std {
    template <class T1, class T2> struct hash<pair<T1,T2>> {
    	std::size_t operator()(pair<T1,T2> _) const {
        	return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
    	}
    };
}
using Mint = Modular<MOD>;

int main() {
	int n; R(n);
	vector<ll> a(n); R(a);

	map<ll, int> last, now;
	if (a[0] > 1)
		last[a[0] - 1] = 1;
	last[a[0]] = 0;
	last[a[0] + 1] = 1;

	for (int i = 1; i < n; i++) {
		for (auto& j : last)
			for (int k = (a[i] == 1 ? 0 : -1); k <= 2; k++) {
				ll gcd = __gcd(j.F, a[i] + k);
				if (gcd == 1) continue;

				auto it = now.find(gcd);
				if (it == now.end())
					now[gcd] = j.S + abs(k);
				else
					chkmin(now[gcd], j.S + abs(k));
			}

		swap(last, now);
		now.clear();
	}

	int ans = INF;
	for (auto& i : last)
		chkmin(ans, i.S);

	W(ans);

	return 0;
}

static auto _batch_ios = [](){
	#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);
	return 42;
}();


#pragma GCC optimize ("O3", "unroll-loops")
//#pragma GCC target ("avx2")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 2147483647
#define LLINF LLONG_MAX
#define PI 3.1415926535
#define MOD 1000000007
#define MOD2 998244353
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define distinct(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CASE(x) int x; cin >> x; while (x--)
#define CASEt(x) int x; cin >> x; for (int tc = 1; tc <= x; ++tc)
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
namespace {
	typedef long long ll;
	typedef unsigned long long ull;
	typedef pair<int,int> pii;
	typedef pair<ll,ll> pll;
	template <class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
	template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
	template <class T> using maxheap = priority_queue<T, vector<T>, greater<T>>;
	template <int MODVALUE>
	struct Modular {
	private:
		long long val;

		template<class T>
		static Modular toModular(const T& m) {
			long long val = m;
			if (val < 0) val += MODVALUE;
			if (val >= MODVALUE) val -= MODVALUE;
			return Modular(val);
		}

	public:
		bool operator == (const Modular& other) {return val == other.val;}
		bool operator != (const Modular& other) {return !(val == other.val);}
		template<class T> bool operator == (const T& other) {return *this == Modular(other);}
		template<class T> bool operator != (const T& other) {return !(*this == Modular(other));}
		template<class T> friend bool operator == (const T& other, const Modular& m) {return Modular(other) == m;}
		template<class T> friend bool operator != (const T& other, const Modular& m) {return !(Modular(other) == m);}

		bool operator < (const Modular& other) {return val < other.val;}
		bool operator <= (const Modular& other) {return val <= other.val;}
		bool operator > (const Modular& other) {return val > other.val;}
		bool operator >= (const Modular& other) {return val >= other.val;}
		template<class T> bool operator < (const T& other) {return *this < Modular(other);}
		template<class T> bool operator <= (const T& other) {return *this <= Modular(other);}
		template<class T> bool operator > (const T& other) {return *this > Modular(other);}
		template<class T> bool operator >= (const T& other) {return *this >= Modular(other);}
		template<class T> friend bool operator < (const T& other, const Modular& m) {return Modular(other) < m;}
		template<class T> friend bool operator <= (const T& other, const Modular& m) {return Modular(other) <= m;}
		template<class T> friend bool operator > (const T& other, const Modular& m) {return Modular(other) > m;}
		template<class T> friend bool operator >= (const T& other, const Modular& m) {return Modular(other) >= m;}

		Modular operator + (const Modular& other) {return toModular(val + other.val);}
		Modular& operator += (const Modular& other) {return *this = *this + other;}
		template<class T> Modular operator + (const T& other) {return *this + Modular(other);}
		template<class T> Modular& operator += (const T& other) {return *this += Modular(other);}
		template<class T> friend Modular operator + (const T& other, const Modular& m) {return Modular(other) + m;}
		template<class T> friend Modular operator += (const T& other, const Modular& m) {return Modular(other) += m;}

		Modular& operator ++() {return *this += 1;}
		Modular operator ++(int) {Modular tmp = *this; ++*this; return tmp;}

		Modular operator - () const {return Modular(-val);}
		Modular operator - (const Modular& other) {return toModular(val - other.val);}
		Modular& operator -= (const Modular& other) {return *this = *this - other;}
		template<class T> Modular operator - (const T& other) {return *this - Modular(other);}
		template<class T> Modular& operator -= (const T& other) {return *this -= Modular(other);}
		template<class T> friend Modular operator - (const T& other, const Modular& m) {return Modular(other) - m;}
		template<class T> friend Modular operator -= (const T& other, const Modular& m) {return Modular(other) -= m;}

		Modular& operator --() {return *this -= 1;}
		Modular operator --(int) {Modular tmp = *this; --*this; return tmp;}

		Modular operator * (const Modular& other) {return toModular(val * other.val % MODVALUE);}
		Modular& operator *= (const Modular& other) {return *this = *this * other;}
		template<class T> Modular operator * (const T& other) {return *this * Modular(other);}
		template<class T> Modular operator *= (const T& other) {return *this *= Modular(other);}
		template<class T> friend Modular operator * (const T& other, const Modular& m) {return Modular(other) * m;}
		template<class T> friend Modular operator *= (const T& other, const Modular& m) {return Modular(other) *= m;}

		Modular pow(long long power) {
			Modular m = *this, ans = 1;
			while (power) {
				if (power & 1) ans *= m;
				m *= m;
				power >>= 1;
			}
			return ans;
		}
		Modular pow(Modular& other) {return pow(other.val);}
		Modular inv() {return pow(MODVALUE-2);}

		Modular operator / (const Modular& other) {return *this * other;}
		Modular operator /= (const Modular& other) {return *this = *this / other;}
		template<class T> Modular operator / (const T& other) {return *this / Modular(other);}
		template<class T> Modular operator /= (const T& other) {return *this /= Modular(other);}
		template<class T> friend Modular operator / (const T& other, const Modular& m) {return Modular(other) / m;}
		template<class T> friend Modular operator /= (const T& other, const Modular& m) {return Modular(other) /= m;}

		friend istream& operator >> (istream& in, Modular& m) {in >> m.val; m = toModular(m.val % MODVALUE); return in;}
		friend ostream& operator << (ostream& out, const Modular& m) {return out << m.val;}

		Modular() : val(0) {}
		Modular(int val) : val(val % MODVALUE) {};
		Modular(long long val) : val(val % MODVALUE) {};
	};
}
namespace input {
	template<class T> inline void _R(T &x) {cin >> x;}
	template<class T1, class T2> inline void _R(pair<T1,T2> &x) {_R(x.first); _R(x.second);}
	template<class T> inline void _R(vector<T> &x) {for (auto& i : x) _R(i);}
	inline void R() {}
	template<class T1, class... T2> inline void R(T1 &x, T2 &...y) { _R(x); R(y...);}
} using namespace input;
namespace output {
	template<class T> inline void _W(const T& x);
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p);
	template<class T> inline void _W(const vector<T>& x);
	template<class T> inline void _W(const deque<T>& x);
	template<class T> inline void _W(const forward_list<T>& x);
	template<class T> inline void _W(const list<T>& x);
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x);
	template<class T> inline void _W(const multiset<T>& x);
	template<class T> inline void _W(const unordered_set<T>& x);
	template<class T> inline void _W(const unordered_multiset<T>& x);
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x);
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x);

	template<class T> inline void _W(const T& x) {cout << x;}
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p) {_W(p.first); _W(' '); _W(p.second);}
	template<class T> inline void _W(const vector<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const deque<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const forward_list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

	template<class T> inline void _W(const ordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

 	inline void W() {cout << '\n';}
	template<class T> inline void W(const T &x) {_W(x); W();}
	template<class T1, class... T2> inline void W(const T1 &x, const T2 &...y) { _W(x); _W(' '); W(y...);}
} using namespace output;
namespace output_format{
	inline string Case(int tc) {return "Case #" + to_string(tc) + ':';}
	inline string YN(bool b) {return b ? "YES" : "NO";}
} using namespace output_format;
namespace utility{
	inline ll pow(ll b, ll p, ll mod) {
		b %= mod;
		ll ans = 1;
		for (; p > 0; p >>= 1) {
			if (p&1) {ans *= b; ans %= mod;}
			b *= b;
			b %= mod;
		}
		return ans;
	}
	inline bool chkmin(ll& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(ll& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, int y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, int y) {return x < y ? x = y, 1 : 0;}
} using namespace utility;
namespace std {
    template <class T1, class T2> struct hash<pair<T1,T2>> {
    	std::size_t operator()(pair<T1,T2> _) const {
        	return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
    	}
    };
}
using Mint = Modular<MOD>;

template<class T>
struct primeSieve{
	int maxN = 0;
	vector<int> primes;
	vector<bool> isComposite;
	vector<int> min_divisor;

	vector<pair<T,int> > factorize(T x) {
		assert(x > 1);

		vector<pair<T,int> > factors;
		if (x <= maxN) {
			int cur = -1, cnt = -1;
			while (x != 1) {
				if (min_divisor[x] == cur) {
					cnt++;
					x /= cur;
				} else {
					if (cur != -1) {
						factors.push_back({cur,cnt});
					}
					cur = min_divisor[x];
					cnt = 0;
				}
			}
			factors.push_back({cur,cnt});
			return factors;
		} else {
			for (auto i : primes) {
				if (x == 1) break;
				int cnt = 0;
				while (x % i == 0) {
					cnt++;
					x /= i;
				}
				if (cnt > 0) factors.push_back({i,cnt});
			}
			if (x != 1) factors.push_back({x,1});
		}

		return factors;
	}

	primeSieve(int n) {
		maxN = n;
		primes.clear();
		isComposite.resize(n+1);
		min_divisor.resize(n+1);

		for (int i = 2; i <= n; i++) {
			if (!isComposite[i]) {
				primes.push_back(i);
				min_divisor[i] = i;
			}
			for (int j = 0; j < primes.size() && i * primes[j] <= n; j++) {
				isComposite[i*primes[j]] = 1;
				min_divisor[i*primes[j]] = primes[j];
				if (i % primes[j] == 0)
					break;
			}
		}
	}
};
primeSieve<long long> sieve(1000000);

vector<ll> a;
ll test(ll p) {
	ll sum = 0;
	for (auto& i : a) {
		ll below = i - i % p;
		if (below == 0)
			sum += p - i;
		else
			sum += min(i - below, p - i + below);
	}
	return sum;
}

int main() {
	int n; R(n);
	a.resize(n); R(a);

	ll ans = LLINF;

	while (clock() <= 2000) {
		ll u = rng() % n;
		ll v = rng() % n;
		while (u == v)
			v = rng() % n;

		u = a[u];
		v = a[v];

		for (ll i = max(1LL, a[u] - 1); i <= a[u] + 1; i++)
			for (ll j = max(1LL, a[v] - 1); j <= a[v] + 1; j++) {
				ll gcd = __gcd(i,j);
				if (gcd == 1) continue;
				for (auto& k : sieve.factorize(gcd))
					chkmin(ans, test(k.F));
			}
	}

	W(ans);

	return 0;
}

static auto _batch_ios = [](){
	#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);
	return 42;
}();


#pragma GCC optimize ("O3", "unroll-loops")
//#pragma GCC target ("avx2")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 2147483647
#define LLINF LLONG_MAX
#define PI 3.1415926535
#define MOD 1000000007
#define MOD2 998244353
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define distinct(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CASE(x) int x; cin >> x; while (x--)
#define CASEt(x) int x; cin >> x; for (int tc = 1; tc <= x; ++tc)
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
namespace {
	typedef long long ll;
	typedef unsigned long long ull;
	typedef pair<int,int> pii;
	typedef pair<ll,ll> pll;
	template <class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
	template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
	template <class T> using maxheap = priority_queue<T, vector<T>, greater<T>>;
	template <int MODVALUE>
	struct Modular {
	private:
		long long val;

		template<class T>
		static Modular toModular(const T& m) {
			long long val = m;
			if (val < 0) val += MODVALUE;
			if (val >= MODVALUE) val -= MODVALUE;
			return Modular(val);
		}

	public:
		bool operator == (const Modular& other) {return val == other.val;}
		bool operator != (const Modular& other) {return !(val == other.val);}
		template<class T> bool operator == (const T& other) {return *this == Modular(other);}
		template<class T> bool operator != (const T& other) {return !(*this == Modular(other));}
		template<class T> friend bool operator == (const T& other, const Modular& m) {return Modular(other) == m;}
		template<class T> friend bool operator != (const T& other, const Modular& m) {return !(Modular(other) == m);}

		bool operator < (const Modular& other) {return val < other.val;}
		bool operator <= (const Modular& other) {return val <= other.val;}
		bool operator > (const Modular& other) {return val > other.val;}
		bool operator >= (const Modular& other) {return val >= other.val;}
		template<class T> bool operator < (const T& other) {return *this < Modular(other);}
		template<class T> bool operator <= (const T& other) {return *this <= Modular(other);}
		template<class T> bool operator > (const T& other) {return *this > Modular(other);}
		template<class T> bool operator >= (const T& other) {return *this >= Modular(other);}
		template<class T> friend bool operator < (const T& other, const Modular& m) {return Modular(other) < m;}
		template<class T> friend bool operator <= (const T& other, const Modular& m) {return Modular(other) <= m;}
		template<class T> friend bool operator > (const T& other, const Modular& m) {return Modular(other) > m;}
		template<class T> friend bool operator >= (const T& other, const Modular& m) {return Modular(other) >= m;}

		Modular operator + (const Modular& other) {return toModular(val + other.val);}
		Modular& operator += (const Modular& other) {return *this = *this + other;}
		template<class T> Modular operator + (const T& other) {return *this + Modular(other);}
		template<class T> Modular& operator += (const T& other) {return *this += Modular(other);}
		template<class T> friend Modular operator + (const T& other, const Modular& m) {return Modular(other) + m;}
		template<class T> friend Modular operator += (const T& other, const Modular& m) {return Modular(other) += m;}

		Modular& operator ++() {return *this += 1;}
		Modular operator ++(int) {Modular tmp = *this; ++*this; return tmp;}

		Modular operator - () const {return Modular(-val);}
		Modular operator - (const Modular& other) {return toModular(val - other.val);}
		Modular& operator -= (const Modular& other) {return *this = *this - other;}
		template<class T> Modular operator - (const T& other) {return *this - Modular(other);}
		template<class T> Modular& operator -= (const T& other) {return *this -= Modular(other);}
		template<class T> friend Modular operator - (const T& other, const Modular& m) {return Modular(other) - m;}
		template<class T> friend Modular operator -= (const T& other, const Modular& m) {return Modular(other) -= m;}

		Modular& operator --() {return *this -= 1;}
		Modular operator --(int) {Modular tmp = *this; --*this; return tmp;}

		Modular operator * (const Modular& other) {return toModular(val * other.val % MODVALUE);}
		Modular& operator *= (const Modular& other) {return *this = *this * other;}
		template<class T> Modular operator * (const T& other) {return *this * Modular(other);}
		template<class T> Modular operator *= (const T& other) {return *this *= Modular(other);}
		template<class T> friend Modular operator * (const T& other, const Modular& m) {return Modular(other) * m;}
		template<class T> friend Modular operator *= (const T& other, const Modular& m) {return Modular(other) *= m;}

		Modular pow(long long power) {
			Modular m = *this, ans = 1;
			while (power) {
				if (power & 1) ans *= m;
				m *= m;
				power >>= 1;
			}
			return ans;
		}
		Modular pow(Modular& other) {return pow(other.val);}
		Modular inv() {return pow(MODVALUE-2);}

		Modular operator / (const Modular& other) {return *this * other;}
		Modular operator /= (const Modular& other) {return *this = *this / other;}
		template<class T> Modular operator / (const T& other) {return *this / Modular(other);}
		template<class T> Modular operator /= (const T& other) {return *this /= Modular(other);}
		template<class T> friend Modular operator / (const T& other, const Modular& m) {return Modular(other) / m;}
		template<class T> friend Modular operator /= (const T& other, const Modular& m) {return Modular(other) /= m;}

		friend istream& operator >> (istream& in, Modular& m) {in >> m.val; m = toModular(m.val % MODVALUE); return in;}
		friend ostream& operator << (ostream& out, const Modular& m) {return out << m.val;}

		Modular() : val(0) {}
		Modular(int val) : val(val % MODVALUE) {};
		Modular(long long val) : val(val % MODVALUE) {};
	};
}
namespace input {
	template<class T> inline void _R(T &x) {cin >> x;}
	template<class T1, class T2> inline void _R(pair<T1,T2> &x) {_R(x.first); _R(x.second);}
	template<class T> inline void _R(vector<T> &x) {for (auto& i : x) _R(i);}
	inline void R() {}
	template<class T1, class... T2> inline void R(T1 &x, T2 &...y) { _R(x); R(y...);}
} using namespace input;
namespace output {
	template<class T> inline void _W(const T& x);
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p);
	template<class T> inline void _W(const vector<T>& x);
	template<class T> inline void _W(const deque<T>& x);
	template<class T> inline void _W(const forward_list<T>& x);
	template<class T> inline void _W(const list<T>& x);
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x);
	template<class T> inline void _W(const multiset<T>& x);
	template<class T> inline void _W(const unordered_set<T>& x);
	template<class T> inline void _W(const unordered_multiset<T>& x);
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x);
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x);

	template<class T> inline void _W(const T& x) {cout << x;}
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p) {_W(p.first); _W(' '); _W(p.second);}
	template<class T> inline void _W(const vector<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const deque<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const forward_list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

	template<class T> inline void _W(const ordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

 	inline void W() {cout << '\n';}
	template<class T> inline void W(const T &x) {_W(x); W();}
	template<class T1, class... T2> inline void W(const T1 &x, const T2 &...y) { _W(x); _W(' '); W(y...);}
} using namespace output;
namespace output_format{
	inline string Case(int tc) {return "Case #" + to_string(tc) + ':';}
	inline string YN(bool b) {return b ? "YES" : "NO";}
} using namespace output_format;
namespace utility{
	inline ll pow(ll b, ll p, ll mod) {
		b %= mod;
		ll ans = 1;
		for (; p > 0; p >>= 1) {
			if (p&1) {ans *= b; ans %= mod;}
			b *= b;
			b %= mod;
		}
		return ans;
	}
	inline bool chkmin(ll& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(ll& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, int y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, int y) {return x < y ? x = y, 1 : 0;}
} using namespace utility;
namespace std {
    template <class T1, class T2> struct hash<pair<T1,T2>> {
    	std::size_t operator()(pair<T1,T2> _) const {
        	return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
    	}
    };
}
using Mint = Modular<MOD>;

template<class T>
struct primeSieve{
	int maxN = 0;
	vector<int> primes;
	vector<bool> isComposite;
	vector<int> min_divisor;

	vector<pair<T,int> > factorize(T x) {
		assert(x > 1);

		vector<pair<T,int> > factors;
		if (x <= maxN) {
			int cur = -1, cnt = -1;
			while (x != 1) {
				if (min_divisor[x] == cur) {
					cnt++;
					x /= cur;
				} else {
					if (cur != -1) {
						factors.push_back({cur,cnt});
					}
					cur = min_divisor[x];
					cnt = 0;
				}
			}
			factors.push_back({cur,cnt});
			return factors;
		} else {
			for (auto i : primes) {
				if (x == 1) break;
				int cnt = 0;
				while (x % i == 0) {
					cnt++;
					x /= i;
				}
				if (cnt > 0) factors.push_back({i,cnt});
			}
			if (x != 1) factors.push_back({x,1});
		}

		return factors;
	}

	primeSieve(int n) {
		maxN = n;
		primes.clear();
		isComposite.resize(n+1);
		min_divisor.resize(n+1);

		for (int i = 2; i <= n; i++) {
			if (!isComposite[i]) {
				primes.push_back(i);
				min_divisor[i] = i;
			}
			for (int j = 0; j < primes.size() && i * primes[j] <= n; j++) {
				isComposite[i*primes[j]] = 1;
				min_divisor[i*primes[j]] = primes[j];
				if (i % primes[j] == 0)
					break;
			}
		}
	}
};
primeSieve<long long> sieve(1000000);

vector<ll> a;
ll test(ll p) {
	ll sum = 0;
	for (auto& i : a) {
		ll below = i - i % p;
		if (below == 0)
			sum += p - i;
		else
			sum += min(i - below, p - i + below);
	}
	return sum;
}

int main() {
	int n; R(n);
	a.resize(n); R(a);

	ll ans = LLINF;

	while (clock() <= 2450) {
		ll u = rng() % n;
		ll v = rng() % n;
		while (u == v)
			v = rng() % n;

		u = a[u];
		v = a[v];

		for (ll i = max(1LL, a[u] - 1); i <= a[u] + 1; i++)
			for (ll j = max(1LL, a[v] - 1); j <= a[v] + 1; j++) {
				ll gcd = __gcd(i,j);
				if (gcd == 1) continue;
				for (auto& k : sieve.factorize(gcd))
					chkmin(ans, test(k.F));
			}
	}

	W(ans);

	return 0;
}

static auto _batch_ios = [](){
	#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);
	return 42;
}();


#pragma GCC optimize ("O3", "unroll-loops")
//#pragma GCC target ("avx2")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 2147483647
#define LLINF LLONG_MAX
#define PI 3.1415926535
#define MOD 1000000007
#define MOD2 998244353
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define distinct(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CASE(x) int x; cin >> x; while (x--)
#define CASEt(x) int x; cin >> x; for (int tc = 1; tc <= x; ++tc)
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
namespace {
	typedef long long ll;
	typedef unsigned long long ull;
	typedef pair<int,int> pii;
	typedef pair<ll,ll> pll;
	template <class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
	template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
	template <class T> using maxheap = priority_queue<T, vector<T>, greater<T>>;
	template <int MODVALUE>
	struct Modular {
	private:
		long long val;

		template<class T>
		static Modular toModular(const T& m) {
			long long val = m;
			if (val < 0) val += MODVALUE;
			if (val >= MODVALUE) val -= MODVALUE;
			return Modular(val);
		}

	public:
		bool operator == (const Modular& other) {return val == other.val;}
		bool operator != (const Modular& other) {return !(val == other.val);}
		template<class T> bool operator == (const T& other) {return *this == Modular(other);}
		template<class T> bool operator != (const T& other) {return !(*this == Modular(other));}
		template<class T> friend bool operator == (const T& other, const Modular& m) {return Modular(other) == m;}
		template<class T> friend bool operator != (const T& other, const Modular& m) {return !(Modular(other) == m);}

		bool operator < (const Modular& other) {return val < other.val;}
		bool operator <= (const Modular& other) {return val <= other.val;}
		bool operator > (const Modular& other) {return val > other.val;}
		bool operator >= (const Modular& other) {return val >= other.val;}
		template<class T> bool operator < (const T& other) {return *this < Modular(other);}
		template<class T> bool operator <= (const T& other) {return *this <= Modular(other);}
		template<class T> bool operator > (const T& other) {return *this > Modular(other);}
		template<class T> bool operator >= (const T& other) {return *this >= Modular(other);}
		template<class T> friend bool operator < (const T& other, const Modular& m) {return Modular(other) < m;}
		template<class T> friend bool operator <= (const T& other, const Modular& m) {return Modular(other) <= m;}
		template<class T> friend bool operator > (const T& other, const Modular& m) {return Modular(other) > m;}
		template<class T> friend bool operator >= (const T& other, const Modular& m) {return Modular(other) >= m;}

		Modular operator + (const Modular& other) {return toModular(val + other.val);}
		Modular& operator += (const Modular& other) {return *this = *this + other;}
		template<class T> Modular operator + (const T& other) {return *this + Modular(other);}
		template<class T> Modular& operator += (const T& other) {return *this += Modular(other);}
		template<class T> friend Modular operator + (const T& other, const Modular& m) {return Modular(other) + m;}
		template<class T> friend Modular operator += (const T& other, const Modular& m) {return Modular(other) += m;}

		Modular& operator ++() {return *this += 1;}
		Modular operator ++(int) {Modular tmp = *this; ++*this; return tmp;}

		Modular operator - () const {return Modular(-val);}
		Modular operator - (const Modular& other) {return toModular(val - other.val);}
		Modular& operator -= (const Modular& other) {return *this = *this - other;}
		template<class T> Modular operator - (const T& other) {return *this - Modular(other);}
		template<class T> Modular& operator -= (const T& other) {return *this -= Modular(other);}
		template<class T> friend Modular operator - (const T& other, const Modular& m) {return Modular(other) - m;}
		template<class T> friend Modular operator -= (const T& other, const Modular& m) {return Modular(other) -= m;}

		Modular& operator --() {return *this -= 1;}
		Modular operator --(int) {Modular tmp = *this; --*this; return tmp;}

		Modular operator * (const Modular& other) {return toModular(val * other.val % MODVALUE);}
		Modular& operator *= (const Modular& other) {return *this = *this * other;}
		template<class T> Modular operator * (const T& other) {return *this * Modular(other);}
		template<class T> Modular operator *= (const T& other) {return *this *= Modular(other);}
		template<class T> friend Modular operator * (const T& other, const Modular& m) {return Modular(other) * m;}
		template<class T> friend Modular operator *= (const T& other, const Modular& m) {return Modular(other) *= m;}

		Modular pow(long long power) {
			Modular m = *this, ans = 1;
			while (power) {
				if (power & 1) ans *= m;
				m *= m;
				power >>= 1;
			}
			return ans;
		}
		Modular pow(Modular& other) {return pow(other.val);}
		Modular inv() {return pow(MODVALUE-2);}

		Modular operator / (const Modular& other) {return *this * other;}
		Modular operator /= (const Modular& other) {return *this = *this / other;}
		template<class T> Modular operator / (const T& other) {return *this / Modular(other);}
		template<class T> Modular operator /= (const T& other) {return *this /= Modular(other);}
		template<class T> friend Modular operator / (const T& other, const Modular& m) {return Modular(other) / m;}
		template<class T> friend Modular operator /= (const T& other, const Modular& m) {return Modular(other) /= m;}

		friend istream& operator >> (istream& in, Modular& m) {in >> m.val; m = toModular(m.val % MODVALUE); return in;}
		friend ostream& operator << (ostream& out, const Modular& m) {return out << m.val;}

		Modular() : val(0) {}
		Modular(int val) : val(val % MODVALUE) {};
		Modular(long long val) : val(val % MODVALUE) {};
	};
}
namespace input {
	template<class T> inline void _R(T &x) {cin >> x;}
	template<class T1, class T2> inline void _R(pair<T1,T2> &x) {_R(x.first); _R(x.second);}
	template<class T> inline void _R(vector<T> &x) {for (auto& i : x) _R(i);}
	inline void R() {}
	template<class T1, class... T2> inline void R(T1 &x, T2 &...y) { _R(x); R(y...);}
} using namespace input;
namespace output {
	template<class T> inline void _W(const T& x);
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p);
	template<class T> inline void _W(const vector<T>& x);
	template<class T> inline void _W(const deque<T>& x);
	template<class T> inline void _W(const forward_list<T>& x);
	template<class T> inline void _W(const list<T>& x);
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x);
	template<class T> inline void _W(const multiset<T>& x);
	template<class T> inline void _W(const unordered_set<T>& x);
	template<class T> inline void _W(const unordered_multiset<T>& x);
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x);
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x);
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x);

	template<class T> inline void _W(const T& x) {cout << x;}
	template<class T1, class T2> inline void _W(const pair<T1,T2>& p) {_W(p.first); _W(' '); _W(p.second);}
	template<class T> inline void _W(const vector<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const deque<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const forward_list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const list<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const set<T1,T2,T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T> inline void _W(const unordered_multiset<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2, class T3> inline void _W(const map<T1, T2, T3>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_map<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}
	template<class T1, class T2> inline void _W(const unordered_multimap<T1, T2>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

	template<class T> inline void _W(const ordered_set<T>& x) {bool isFirst = 1; for (auto i : x) {if (!isFirst) cout << ' '; isFirst = 0; _W(i);}}

 	inline void W() {cout << '\n';}
	template<class T> inline void W(const T &x) {_W(x); W();}
	template<class T1, class... T2> inline void W(const T1 &x, const T2 &...y) { _W(x); _W(' '); W(y...);}
} using namespace output;
namespace output_format{
	inline string Case(int tc) {return "Case #" + to_string(tc) + ':';}
	inline string YN(bool b) {return b ? "YES" : "NO";}
} using namespace output_format;
namespace utility{
	inline ll pow(ll b, ll p, ll mod) {
		b %= mod;
		ll ans = 1;
		for (; p > 0; p >>= 1) {
			if (p&1) {ans *= b; ans %= mod;}
			b *= b;
			b %= mod;
		}
		return ans;
	}
	inline bool chkmin(ll& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(ll& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, ll y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmin(int& x, int y) {return x > y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(ll& x, int y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, ll y) {return x < y ? x = y, 1 : 0;}
	inline bool chkmax(int& x, int y) {return x < y ? x = y, 1 : 0;}
} using namespace utility;
namespace std {
    template <class T1, class T2> struct hash<pair<T1,T2>> {
    	std::size_t operator()(pair<T1,T2> _) const {
        	return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
    	}
    };
}
using Mint = Modular<MOD>;

template<class T>
struct primeSieve{
	int maxN = 0;
	vector<int> primes;
	vector<bool> isComposite;
	vector<int> min_divisor;

	vector<pair<T,int> > factorize(T x) {
		assert(x > 1);

		vector<pair<T,int> > factors;
		if (x <= maxN) {
			int cur = -1, cnt = -1;
			while (x != 1) {
				if (min_divisor[x] == cur) {
					cnt++;
					x /= cur;
				} else {
					if (cur != -1) {
						factors.push_back({cur,cnt});
					}
					cur = min_divisor[x];
					cnt = 0;
				}
			}
			factors.push_back({cur,cnt});
			return factors;
		} else {
			for (auto i : primes) {
				if (x == 1) break;
				int cnt = 0;
				while (x % i == 0) {
					cnt++;
					x /= i;
				}
				if (cnt > 0) factors.push_back({i,cnt});
			}
			if (x != 1) factors.push_back({x,1});
		}

		return factors;
	}

	primeSieve(int n) {
		maxN = n;
		primes.clear();
		isComposite.resize(n+1);
		min_divisor.resize(n+1);

		for (int i = 2; i <= n; i++) {
			if (!isComposite[i]) {
				primes.push_back(i);
				min_divisor[i] = i;
			}
			for (int j = 0; j < primes.size() && i * primes[j] <= n; j++) {
				isComposite[i*primes[j]] = 1;
				min_divisor[i*primes[j]] = primes[j];
				if (i % primes[j] == 0)
					break;
			}
		}
	}
};
primeSieve<long long> sieve(1000000);

vector<ll> a;
ll test(ll p) {
	ll sum = 0;
	for (auto& i : a) {
		ll below = i - i % p;
		if (below == 0)
			sum += p - i;
		else
			sum += min(i - below, p - i + below);
	}
	return sum;
}

int main() {
	int tt = clock();

	int n; R(n);
	a.resize(n); R(a);

	ll ans = LLINF;

	while (clock() - tt <= 2000) {
		ll u = rng() % n;
		ll v = rng() % n;
		while (u == v)
			v = rng() % n;

		u = a[u];
		v = a[v];

		for (ll i = max(1LL, a[u] - 1); i <= a[u] + 1; i++)
			for (ll j = max(1LL, a[v] - 1); j <= a[v] + 1; j++) {
				ll gcd = __gcd(i,j);
				if (gcd == 1) continue;
				for (auto& k : sieve.factorize(gcd))
					chkmin(ans, test(k.F));
			}
	}

	W(ans);

	return 0;
}

static auto _batch_ios = [](){
	#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);
	return 42;
}();