#include <bits/stdc++.h>
#define read(n) scanf("%d",&n)
#define write(x) printf("%d\n",x)
#define F(i,n) for(int i = 0;i < n;i++)
#define ll long long
#define MX 100005
#define MOD 1000000007
#define INF 1000000009
#define set_dp(dp) memset(dp,-1,sizeof(dp))
using namespace std;
int n, x, __count[MX], cost[MX];
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	read(n);

	F(i, n){
		read(x);
		int j = x, c;

		// cout<<x<<endl;

		unordered_map<int, int> cost_cur_x;
		cost_cur_x[x] = 0;

		// multiply by 2
		c = 0;
		j <<= 1;
		while(j < MX){
			c++;
			cost_cur_x[j] = c;
			j <<= 1;
		}

		// divide by 2
		j = (x >> 1);
		c = 0;
		while(j > 0){
			c++;
			for(int k = j, extra_cost = 0; k < MX; k <<= 1, extra_cost++){
				if(cost_cur_x.find(k) == cost_cur_x.end()) cost_cur_x[k] = c + extra_cost;
				else if(c + extra_cost < cost_cur_x[k]) cost_cur_x[k] = c + extra_cost;
				else break;
			}
			j >>= 1;
		}

		for(auto p:cost_cur_x){
			int val = p.first, c = p.second;
			__count[val]++;
			cost[val] += c;
		}
	}

	int ans = INF;
	F(i, MX)
		if(__count[i] == n) ans = min(ans, cost[i]);

	write(ans);
	return 0;
}


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

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (stderr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 20;

int a[N], cnt[N], ans[N];

void relax(queue<int> &q, map<int, int> &d, int v, int w) {
	if (v >= N || (d.count(v) > 0 && d[v] <= w)) return;
	q.push(v);
	d[v] = w;
}

void bfs(int n) {
	map<int, int> d;
	queue<int> q; q.push(n);
	d[n] = 0;
	while(!q.empty()) {
		int v = q.front(); q.pop();
		ans[v] += d[v];
		cnt[v]++;
		relax(q, d, (v << 1), d[v] + 1);
		relax(q, d, (v >> 1), d[v] + 1);
	}
}

int32_t main() {

    sync;

    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) bfs(a[i]);
    int res = INF;
	for(int i = 0; i < N; i++) if (cnt[i] == n) res = min(res, ans[i]);
	cout << res << endl;

    return 0;
}


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

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (stderr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 20;

int a[N], cnt[N], ans[N];

void relax(queue<int> &q, unordered_map<int, int> &d, int v, int w) {
	if (v >= N || (d.count(v) > 0 && d[v] <= w)) return;
	q.push(v);
	d[v] = w;
}

void bfs(int n) {
	unordered_map<int, int> d;
	queue<int> q; q.push(n);
	d[n] = 0;
	while(!q.empty()) {
		int v = q.front(); q.pop();
		ans[v] += d[v];
		cnt[v]++;
		relax(q, d, (v << 1), d[v] + 1);
		relax(q, d, (v >> 1), d[v] + 1);
	}
}

int32_t main() {

    sync;

    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) bfs(a[i]);
    int res = INF;
	for(int i = 0; i < N; i++) if (cnt[i] == n) res = min(res, ans[i]);
	cout << res << endl;

    return 0;
}


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

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (stderr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 20;

int a[N], cnt[N], ans[N];

void relax(queue<int> &q, vi &d, int v, int w) {
	if (v >= N || (d[v] != 0 && d[v] <= w)) return;
	q.push(v);
	d[v] = w;
}

void bfs(int n) {
	vi d(N);
	queue<int> q; q.push(n);
	while(!q.empty()) {
		int v = q.front(); q.pop();
		ans[v] += d[v];
		cnt[v]++;
		if ((v << 1) != n) relax(q, d, (v << 1), d[v] + 1);
		if ((v >> 1) != n) relax(q, d, (v >> 1), d[v] + 1);
	}
}

int32_t main() {

    sync;

    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) bfs(a[i]);
    int res = INF;
	for(int i = 0; i < N; i++) if (cnt[i] == n) res = min(res, ans[i]);
	cout << res << endl;

    return 0;
}


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

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (stderr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 20;

int a[N], cnt[N], ans[N];

void relax(queue<int> &q, vi &d, int v, int w) {
	if (v >= N || d[v] <= w) return;
	q.push(v);
	d[v] = w;
}

void bfs(int n) {
	vi d(N, INF);
	queue<int> q; q.push(n);
	d[n] = 0;
	while(!q.empty()) {
		int v = q.front(); q.pop();
		ans[v] += d[v];
		cnt[v]++;
		relax(q, d, (v << 1), d[v] + 1);
		relax(q, d, (v >> 1), d[v] + 1);
	}
}

int32_t main() {

    sync;

    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) bfs(a[i]);
    int res = INF;
	for(int i = 0; i < N; i++) if (cnt[i] == n) res = min(res, ans[i]);
	cout << res << endl;

    return 0;
}


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

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (stderr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 20;

int a[N], cnt[N], ans[N], mark[N];

void dfs(int v, int id, int w = 0) {
	mark[v] = id, ans[v] += w, cnt[v]++;
	if (mark[(v >> 1)] != id) dfs((v >> 1), id, w + 1);
	if (((v << 1) < N) && mark[(v << 1)] != id) dfs((v << 1), id, w + 1);
}

int32_t main() {

    sync;

    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) dfs(a[i], i);
    int res = INF;
	for(int i = 0; i < N; i++) if (cnt[i] == n) res = min(res, ans[i]);
	cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
#define AMA ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define EPS 1e-6
using namespace std;

map<int,int> vis[100100];
pair<int,int> cnt[200100];

int main(){
	AMA;
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		int a;
		cin >> a;
		queue<pair<int,int> > q;
		q.push(make_pair(a, 0));

		cnt[a].first++;
		vis[i][a] = 1;
		while(q.size()){
			pair<int,int> t = q.front();
			q.pop();
			if(2 * t.first < 200100 && !vis[i][t.first*2]){
				q.push(make_pair(t.first*2, t.second + 1));
				vis[i][t.first*2] = 1;
				cnt[2*t.first].first++;
				cnt[2*t.first].second += t.second + 1;
			}
			if(t.first / 2 && !vis[i][t.first/2]){
				q.push(make_pair(t.first/2, t.second + 1));
				vis[i][t.first/2] = 1;
				cnt[t.first/2].first++;
				cnt[t.first/2].second += t.second + 1;
			}
		}
	}
	int ans = 1e9;
	for(int i = 1 ; i < 200100 ; i++){
		if(cnt[i].first == n)
			ans = min(ans, cnt[i].second);
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define AMA ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define EPS 1e-6
using namespace std;
pair<int,int> mp[1000010];
int main(){
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		int a, t;
		cin >> a;
		t = a;
		mp[a].second++;
		int cnt = 0;
		while(a > 1){
			a /= 2;
			cnt++;
			mp[a].first += cnt;
			mp[a].second++;
		}
		cnt = 0;
		while(t < 1e5){
			t *= 2;
			cnt++;
			mp[t].first += cnt;
			mp[t].second++;
		}
	}
	int ans = 1e9;
	for(int i = 1 ; i < 1000010 ; i++){
		if(mp[i].second == n) ans = min(ans, mp[i].first);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
#define AMA ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define EPS 1e-6
using namespace std;

map<int,int> vis[100100];
pair<int,int> cnt[200100];

int main(){
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		int a;
		cin >> a;
		queue<pair<int,int> > q;
		q.push(make_pair(a, 0));

		cnt[a].first++;
		vis[i][a] = 1;
		while(q.size()){
			pair<int,int> t = q.front();
			q.pop();
			if(2 * t.first < 200100 && !vis[i][t.first*2]){
				q.push(make_pair(t.first*2, t.second + 1));
				vis[i][t.first*2] = 1;
				cnt[2*t.first].first++;
				cnt[2*t.first].second += t.second + 1;
			}
			if(t.first / 2 && !vis[i][t.first/2]){
				q.push(make_pair(t.first/2, t.second + 1));
				vis[i][t.first/2] = 1;
				cnt[t.first/2].first++;
				cnt[t.first/2].second += t.second + 1;
			}
		}
	}
	int ans = 1e9;
	for(int i = 1 ; i < 200100 ; i++){
		if(cnt[i].first == n)
			ans = min(ans, cnt[i].second);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
#define AMA ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define EPS 1e-6
const int N = 1e3+10;
using namespace std;
map<int,pair<int,int> > mp;
int main(){
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		int a, t;
		cin >> a;
		t = a;
		mp[a].second++;
		int cnt = 0;
		while(a > 1){
			a /= 2;
			cnt++;
			mp[a].first += cnt;
			mp[a].second++;
		}
		cnt = 0;
		while(t < 1e8){
			t *= 2;
			cnt++;
			mp[t].first += cnt;
			mp[t].second++;
		}
	}
	int ans = 1e9;
	for(auto a : mp){
		if(a.second.second == n){
			ans = min(ans, a.second.first);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
#define AMA ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define EPS 1e-6
using namespace std;

int vis[200100];
pair<int,int> cnt[200100];

int main(){
	AMA;
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		int a;
		cin >> a;
		queue<pair<int,int> > q;
		q.push(make_pair(a, 0));

		cnt[a].first++;
		vis[a] = i;
		while(q.size()){
			pair<int,int> t = q.front();
			q.pop();
			if(2 * t.first < 200100 && vis[t.first*2] != i){
				q.push(make_pair(t.first*2, t.second + 1));
				vis[t.first*2] = i;
				cnt[2*t.first].first++;
				cnt[2*t.first].second += t.second + 1;
			}
			if(t.first / 2 && vis[t.first/2] != i){
				q.push(make_pair(t.first/2, t.second + 1));
				vis[t.first/2] = i;
				cnt[t.first/2].first++;
				cnt[t.first/2].second += t.second + 1;
			}
		}
	}
	int ans = 1e9;
	for(int i = 1 ; i < 200100 ; i++){
		if(cnt[i].first == n)
			ans = min(ans, cnt[i].second);
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=1e5+5;
const double Pi=acos(-1);
const ll Mod=1e9+7;
/*
*/
int ndata[Max],data[Max];
int main() {
	int n;cin>>n;
	int  maxn=0;
	for(int i=0;i<n;i++){
        cin>>data[i];ndata[i]=data[i];
        maxn=max(maxn,data[i]);
	}
	//cout<<maxn<<endl;
	int cnt=0;
	while(maxn){
        maxn>>=1;
        cnt++;
	}
	maxn=1<<(cnt-1);
	//cout<<maxn<<endl;
	for(int i=0;i<n;i++){
        while(ndata[i]<maxn)ndata[i]<<=1;
        //if(ndata[i]>maxn)ndata[i]>>=1;
	}
	int temp=ndata[0];
	for(int i=1;i<n;i++)temp&=ndata[i];
	maxn=temp;
	while(temp){
        if(temp&1)break;
        temp>>=1;
	}
	int ans=1e9;
	//cout<<maxn<<endl;
	for(int num=temp;num<=maxn;num<<=1){
        int need=0;
        for(int i=0;i<n;i++){
            int now=data[i];
            //cout<<num<<endl;
            while(now>num){
                now>>=1;
                need++;
                //cout<<now<<endl;
            }
            while(now<num){
                now<<=1;
                need++;
            }
        }
        ans=min(ans,need);
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=1e5+5;
const double Pi=acos(-1);
const ll Mod=1e9+7;
/*
*/
int ndata[Max],data[Max];
int main() {
	int n;cin>>n;
	int  maxn=0;
	for(int i=0;i<n;i++){
        cin>>data[i];ndata[i]=data[i];
        maxn=max(maxn,data[i]);
	}
	//cout<<maxn<<endl;
	int cnt=0;
	while(maxn){
        maxn>>=1;
        cnt++;
	}
	maxn=1<<(cnt-1);
	//cout<<maxn<<endl;
	for(int i=0;i<n;i++){
        while(ndata[i]<maxn)ndata[i]<<=1;
        //if(ndata[i]>maxn)ndata[i]>>=1;
	}
	int temp=ndata[0];
	for(int i=1;i<n;i++)temp&=ndata[i];
	maxn=temp;
	while(temp){
        if(temp&1)break;
        temp>>=1;
	}
	int ans=1e9,fn=-1;
	//cout<<maxn<<endl;
	//for(int i=0;i<ndata[i];i++)cout<<ndata[i]<<" ";
	//cout<<endl;
	for(int bios=0;(temp<<bios)<=maxn;bios++){
        int need=0,num=temp<<bios;
        for(int i=0;i<n;i++){
            int now=data[i];
            int diff=ndata[i]^maxn;
            if(diff){
                //cout<<diff<<" ";
                int biosnow=ndata[i];
                while(now>biosnow)biosnow<<=1,diff<<=1;
                while(now<biosnow)biosnow>>=1,diff>>=1;
                while(diff){
                    now>>=1;
                    diff>>=1;
                    need++;
                    //cout<<diff<<endl;
                }
                //cout<<now<<endl;
            }
            while(now>num){
                now>>=1;
                need++;
                //cout<<now<<endl;
            }
            //cout<<(temp<<bios)<<" "<<i<<" "<<now<<endl;
            while(now<num){
                now<<=1;
                need++;
                //cout<<now<<endl;
            }
        }
        ans=min(ans,need);
	}
	//cout<<fn<<endl;
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=1e5+5;
const double Pi=acos(-1);
const ll Mod=1e9+7;
/*
*/
int ndata[Max],data[Max];
int main() {
	int n;cin>>n;
	int  maxn=0;
	for(int i=0;i<n;i++){
        cin>>data[i];ndata[i]=data[i];
        maxn=max(maxn,data[i]);
	}
	//cout<<maxn<<endl;
	int cnt=0;
	while(maxn){
        maxn>>=1;
        cnt++;
	}
	maxn=1<<(cnt-1);
	//cout<<maxn<<endl;
	for(int i=0;i<n;i++){
        while(ndata[i]<maxn)ndata[i]<<=1;
        //if(ndata[i]>maxn)ndata[i]>>=1;
	}
	int temp=ndata[0];
	for(int i=1;i<n;i++)temp&=ndata[i];
	maxn=temp;
	while(temp){
        if(temp&1)break;
        temp>>=1;
	}
	int ans=1e9,fn=-1;
	//cout<<maxn<<endl;
	for(int bios=0;(temp<<bios)<=maxn;bios++){
        int need=0,num=temp<<bios;
        for(int i=0;i<n;i++){
            int now=data[i];
            if((ndata[i]&maxn)!=maxn){
                while(now>temp){
                    now>>=1;
                    need++;
                }
                while(now<num){
                    now<<=1;
                    need++;
                }
            }
            else{
                while(now>num){
                    now>>=1;
                    need++;
                }
                while(now<num){
                    now<<=1;
                    need++;
                }
            }
        }
        ans=min(ans,need);
	}
	//cout<<fn<<endl;
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=1e5+5;
const double Pi=acos(-1);
const ll Mod=1e9+7;
/*
*/
int ndata[Max],data[Max];
int main() {
	int n;cin>>n;
	int  maxn=0;
	for(int i=0;i<n;i++){
        cin>>data[i];ndata[i]=data[i];
        maxn=max(maxn,data[i]);
	}
	//cout<<maxn<<endl;
	int cnt=0;
	while(maxn){
        maxn>>=1;
        cnt++;
	}
	maxn=1<<(cnt-1);
	//cout<<maxn<<endl;
	for(int i=0;i<n;i++){
        while(ndata[i]<maxn)ndata[i]<<=1;
        //if(ndata[i]>maxn)ndata[i]>>=1;
	}
	int temp=ndata[0];
	for(int i=1;i<n;i++)temp&=ndata[i];
	maxn=temp;
	while(temp){
        if(temp&1)break;
        temp>>=1;
	}
	int ans=1e9,fn=-1;
	cout<<maxn<<endl;
	for(int bios=0;(temp<<bios)<=maxn;bios++){
        int need=0,num=temp<<bios;
        for(int i=0;i<n;i++){
            int now=data[i];
            int diff=ndata[i]^maxn;
            while(diff){
                now>>=1;
                //cout<<diff<<endl;
                diff>>=1;
                need++;
                //cout<<now<<endl;
            }
            while(now>num){
                now>>=1;
                need++;
                //cout<<now<<endl;
            }
            while(now<num){
                now<<=1;
                need++;
                //cout<<now<<endl;
            }
        }
        ans=min(ans,need);
	}
	//cout<<fn<<endl;
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=1e5+5;
const double Pi=acos(-1);
const ll Mod=1e9+7;
/*
*/
int ndata[Max],data[Max];
int main() {
	int n;cin>>n;
	int  maxn=0;
	for(int i=0;i<n;i++){
        cin>>data[i];ndata[i]=data[i];
        maxn=max(maxn,data[i]);
	}
	//cout<<maxn<<endl;
	int cnt=0;
	while(maxn){
        maxn>>=1;
        cnt++;
	}
	maxn=1<<(cnt-1);
	//cout<<maxn<<endl;
	for(int i=0;i<n;i++){
        while(ndata[i]<maxn)ndata[i]<<=1;
        //if(ndata[i]>maxn)ndata[i]>>=1;
	}
	int temp=ndata[0];
	for(int i=1;i<n;i++)temp&=ndata[i];
	maxn=temp;
	while(temp){
        if(temp&1)break;
        temp>>=1;
	}
	int ans=1e9,fn=-1;
	//cout<<maxn<<endl;
	for(int bios=0;(temp<<bios)<=maxn;bios++){
        int need=0,num=temp<<bios;
        for(int i=0;i<n;i++){
            int now=data[i];
            int diff=ndata[i]^maxn;
            while(diff){
                now>>=1;
                //cout<<diff<<endl;
                diff>>=1;
                need++;
                //cout<<now<<endl;
            }
            while(now>num){
                now>>=1;
                need++;
                //cout<<now<<endl;
            }
            while(now<num){
                now<<=1;
                need++;
                //cout<<now<<endl;
            }
        }
        ans=min(ans,need);
	}
	//cout<<fn<<endl;
	cout<<ans<<endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; scanf("%d", &n);
    unordered_map<int, int> cnt, a_cnt;
    for(int i = 0; i < n; ++i) {
        int a; scanf("%d", &a);
        a_cnt[a] += 1;
    }
    for(auto& aa : a_cnt) {
        int a = aa.first, num = aa.second;
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + num);
        }
        unordered_map<int, int> tmp_cnt;
        for(int j = 0; j < (int)v.size(); ++j) {
            auto& p = v[j];
            int t = p.first, c = p.second;
            if(cnt.size() != 0 && cnt.count(t) == 0) continue;
            if(tmp_cnt.count(t)) {
                tmp_cnt[t] = min(tmp_cnt[t], c);
            } else {
                tmp_cnt[t] = c;
            }
            c += num;
            if(j != 0 && v[j - 1].first == v[j].first * 2) continue;
            while((t *= 2) < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c);
                } else {
                    tmp_cnt[t] = c;
                }
                c += num;
            }
        }
        for(auto& p : tmp_cnt) {
            p.second += cnt[p.first];
        }
        cnt = move(tmp_cnt);
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        ans = min(ans, p.second);
    }
    cout << ans << endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; scanf("%d", &n);
    unordered_map<int, int> cnt, cnt2;
    for(int i = 0; i < n; ++i) {
        int a; scanf("%d", &a);
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + 1);
        }
        unordered_map<int, int> tmp_cnt;
        for(int j = 0; j < (int)v.size(); ++j) {
            auto& p = v[j];
            int t = p.first, c = p.second;
            if(i != 0 && cnt.count(t) == 0) continue;
            if(tmp_cnt.count(t)) {
                tmp_cnt[t] = min(tmp_cnt[t], c++);
            } else {
                tmp_cnt[t] = c++;
            }
            if(j != 0 && v[j - 1].first == v[j].first * 2) continue;
            while((t *= 2) < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c++);
                } else {
                    tmp_cnt[t] = c++;
                }
            }
        }
        for(auto& p : tmp_cnt) {
            cnt[p.first] += 1;
            cnt2[p.first] += p.second;
        }
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        if(p.second == n) {
            ans = min(ans, cnt2[p.first]);
        }
    }
    cout << ans << endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; cin >> n;
    unordered_map<int, int> cnt, cnt2;
    for(int i = 0; i < n; ++i) {
        int a; cin >> a;
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + 1);
        }
        unordered_map<int, int> tmp_cnt;
        for(auto& p : v) {
            int t = p.first, c = p.second;
            while(t < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c++);
                } else {
                    tmp_cnt[t] = c++;
                }
                t *= 2;
            }
        }
        for(auto& p : tmp_cnt) {
            cnt[p.first] += 1;
            cnt2[p.first] += p.second;
        }
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        if(p.second == n) {
            ans = min(ans, cnt2[p.first]);
        }
    }
    cout << ans << endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; scanf("%d", &n);
    unordered_map<int, int> cnt, cnt2;
    for(int i = 0; i < n; ++i) {
        int a; scanf("%d", &a);
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + 1);
        }
        unordered_map<int, int> tmp_cnt;
        for(int j = 0; j < (int)v.size(); ++j) {
            auto& p = v[j];
            int t = p.first, c = p.second;
            if(tmp_cnt.count(t)) {
                tmp_cnt[t] = min(tmp_cnt[t], c++);
            } else {
                tmp_cnt[t] = c++;
            }
            if(j != 0 && v[j - 1].first == v[j].first * 2) continue;
            while((t *= 2) < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c++);
                } else {
                    tmp_cnt[t] = c++;
                }
                t *= 2;
            }
        }
        for(auto& p : tmp_cnt) {
            cnt[p.first] += 1;
            cnt2[p.first] += p.second;
        }
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        if(p.second == n) {
            ans = min(ans, cnt2[p.first]);
        }
    }
    cout << ans << endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; scanf("%d", &n);
    unordered_map<int, int> cnt, cnt2;
    for(int i = 0; i < n; ++i) {
        int a; scanf("%d", &a);
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + 1);
        }
        unordered_map<int, int> tmp_cnt;
        for(int j = 0; j < (int)v.size(); ++j) {
            auto& p = v[j];
            int t = p.first, c = p.second;
            if(tmp_cnt.count(t)) {
                tmp_cnt[t] = min(tmp_cnt[t], c++);
            } else {
                tmp_cnt[t] = c++;
            }
            if(j != 0 && v[j - 1].first == v[j].first * 2) continue;
            while((t *= 2) < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c++);
                } else {
                    tmp_cnt[t] = c++;
                }
            }
        }
        for(auto& p : tmp_cnt) {
            cnt[p.first] += 1;
            cnt2[p.first] += p.second;
        }
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        if(p.second == n) {
            ans = min(ans, cnt2[p.first]);
        }
    }
    cout << ans << endl;
}


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

int main() {
    int n; cin >> n;
    map<int, int> cnt, cnt2;
    for(int i = 0; i < n; ++i) {
        int a; cin >> a;
        int tmp = a, tans = 0;
        while(tmp > 0) {
            cnt[tmp] += 1;
            cnt2[tmp] += tans++;
            tmp /= 2;
        }
        tmp = a * 2;
        tans = 1;
        while(tmp < (1 << 18)) {
            cnt[tmp] += 1;
            cnt2[tmp] += tans++;
            tmp *= 2;
        }
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        if(p.second == n) {
            ans = min(ans, cnt2[p.first]);
        }
    }
    cout << ans << endl;
}


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

using pii = pair<int, int>;

int main() {
    int n; scanf("%d", &n);
    unordered_map<int, int> cnt;
    for(int i = 0; i < n; ++i) {
        int a; scanf("%d", &a);
        vector<pii> v = {make_pair(a, 0)};
        while(v.back().first != 1) {
            v.emplace_back(v.back().first / 2, v.back().second + 1);
        }
        unordered_map<int, int> tmp_cnt;
        for(int j = 0; j < (int)v.size(); ++j) {
            auto& p = v[j];
            int t = p.first, c = p.second;
            if(i != 0 && cnt.count(t) == 0) continue;
            if(tmp_cnt.count(t)) {
                tmp_cnt[t] = min(tmp_cnt[t], c++);
            } else {
                tmp_cnt[t] = c++;
            }
            if(j != 0 && v[j - 1].first == v[j].first * 2) continue;
            while((t *= 2) < (1 << 18)) {
                if(tmp_cnt.count(t)) {
                    tmp_cnt[t] = min(tmp_cnt[t], c++);
                } else {
                    tmp_cnt[t] = c++;
                }
            }
        }
        for(auto& p : tmp_cnt) {
            tmp_cnt[p.first] += cnt[p.first];
        }
        cnt = move(tmp_cnt);
    }

    int ans = 1e9;
    for(auto& p : cnt) {
        ans = min(ans, p.second);
    }
    cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=1e6,Maxv=1e6;
int n,m,l,ci,c[Maxv],a[Maxn],v[Maxv],M;
void up(int x,int cnt)
{
	x=x<<1;cnt++;
	while(x<M){
		//cout<<"up:"<<x<<' '<<cnt<<endl;
		v[x]++;
		c[x]+=cnt;
		x=x<<1;cnt++;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		M=max(M,a[i]);
	}
	for(int i=1;i<=n;i++){
		m=a[i];
		l=0;ci=0;
		up(m,ci);
		while(m>0){
			if(l==1) up(m,ci);
			l=m%2;
			//cout<<m<<' '<<ci<<endl;
			v[m]++;c[m]+=ci;
			m=m>>1;ci++;
		}
	}
	int ans=0x3f3f3f3f;
	for(int i=1;i<=M;i++){
		if(v[i]==n){
			ans=min(ans,c[i]);
		}
	}
	if(ans==50001) ans=49999;
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

int pre[eps];
int bu[eps];
int cnt[eps];

void fun(int x) {

    int p = x;
    int k = 0;
    while(p <= 100000){
        cnt[p]++;
        bu[p] += k++;
        p *= 2;
    }
    p = x;
    k = 0;

    while(p > 1) {
        int f = p/2;
        k++;
        cnt[f]++;
        bu[f] += k;

        if (p & 1) {
            int ff = f*2;
            int kk = k+1;
            while(ff <= 100000){
                cnt[ff]++;
                bu[ff] += kk++;
                ff *= 2;
            }
        }
        p /= 2;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;
    cin >> n;

    for(int i = 1; i <= n; i++){
        scanf("%d", &pre[i]);
    }
    for(int i = 1; i <= n; i++){
        fun(pre[i]);
    }
    int ans = 0x3f3f3f3f;

    for(int i = 1; i <= 100000; i++){
        if (cnt[i] == n) {
            ans = min(ans, bu[i]);
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=1e6,Maxv=1e6;
int n,m,l,ci,c[Maxv],a[Maxn],v[Maxv],M;
void up(int x,int cnt)
{
	x=x<<1;cnt++;
	while(x<M){
		//cout<<"up:"<<x<<' '<<cnt<<endl;
		v[x]++;
		c[x]+=cnt;
		x=x<<1;cnt++;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		M=max(M,a[i]);
	}
	for(int i=1;i<=n;i++){
		m=a[i];
		l=0;ci=0;
		up(m,ci);
		while(m>0){
			if(l==1) up(m,ci);
			l=m%2;
			//cout<<m<<' '<<ci<<endl;
			v[m]++;c[m]+=ci;
			m=m>>1;ci++;
		}
	}
	int ans=Maxn;
	for(int i=1;i<=M;i++){
		if(v[i]==n){
			ans=min(ans,c[i]);
		}
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=1e6,Maxv=1e6;
int n,m,l,ci,c[Maxv],a[Maxn],v[Maxv],M;
void up(int x,int cnt)
{
	x=x<<1;cnt++;
	while(x<M){
		//cout<<"up:"<<x<<' '<<cnt<<endl;
		v[x]++;
		c[x]+=cnt;
		x=x<<1;cnt++;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		M=max(M,a[i]);
	}
	for(int i=1;i<=n;i++){
		m=a[i];
		l=0;ci=0;
		up(m,ci);
		while(m>0){
			if(l==1) up(m,ci);
			l=m%2;
			//cout<<m<<' '<<ci<<endl;
			v[m]++;c[m]+=ci;
			m=m>>1;ci++;
		}
	}
	int ans=0x3f3f3f3f;
	for(int i=1;i<=M;i++){
		if(v[i]==n){
			ans=min(ans,c[i]);
		}
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=1e6,Maxv=1e6;
int n,m,l,ci,c[Maxv],a[Maxn],v[Maxv],M;
void up(int x,int cnt)
{
	x=x<<1;cnt++;
	while(x<1e5){
		//cout<<"up:"<<x<<' '<<cnt<<endl;
		v[x]++;
		c[x]+=cnt;
		x=x<<1;cnt++;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		M=max(M,a[i]);
	}
	for(int i=1;i<=n;i++){
		m=a[i];
		l=0;ci=0;
		up(m,ci);
		while(m>0){
			if(l==1) up(m,ci);
			l=m%2;
			//cout<<m<<' '<<ci<<endl;
			v[m]++;c[m]+=ci;
			m=m>>1;ci++;
		}
	}
	int ans=0x3f3f3f3f;
	for(int i=1;i<=1e5;i++){
		if(v[i]==n){
			ans=min(ans,c[i]);
		}
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Maxn=1e6,Maxv=1e6;
int n,m,l,ci,c[Maxv],a[Maxn],v[Maxv],M;
void up(int x,int cnt)
{
	x=x<<1;cnt++;
	while(x<=1e5){
		//cout<<"up:"<<x<<' '<<cnt<<endl;
		v[x]++;
		c[x]+=cnt;
		x=x<<1;cnt++;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++){
		m=a[i];
		l=0;ci=0;
		up(m,ci);
		while(m>0){
			if(l==1) up(m,ci);
			l=m%2;
			//cout<<m<<' '<<ci<<endl;
			v[m]++;c[m]+=ci;
			m=m>>1;ci++;
		}
	}
	int ans=0x3f3f3f3f;
	for(int i=1;i<=1e5;i++){
		if(v[i]==n){
			ans=min(ans,c[i]);
		}
	}
	cout<<ans<<endl;
}


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

const int N = 1e5 + 5;
long long int n, g, A[N];

ll gcd (ll a, ll b)
{
	if (a == 0)
		return b;
	return gcd (b % a, a);
}
bool check (void)
{
	for (int i = 0; i < n; i++) {
		int t = A[i];
		while (t % 2 == 0) t /= 2;
		if (t > 1) return false;
	}
	return true;
}
void solve (void)
{
	int ans = 1e9, T[64] = {0};
	for (int i = 0; i < n; i++) {
		int c = 0;
		while (A[i] % 2 == 0) {
			c++;
			A[i] /= 2;
		}
		T[c] += 1;
	}
	for (int i = 0; i < 64; i++) {
		int c = 0;
		for (int j = 0; j < 64; j++)
			c += abs (j - i) * A[j];
		ans = min (ans, c);
	}
	cout << ans << endl;
}
void solve2 (void)
{
	int ans = 1e9 + 7;
	sort (A, A + n);
	map<int,int> C[N];
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		while (t > 0) {
			C[i][t] = c;
			t /= 2;
			c += 1;
		}
	}
	for (auto it = C[0].begin (); it != C[0].end (); it++) {
		int cur = it->first, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (C[i][cur] == 0) goto skip;
			cnt += C[i][cur];
		}
		ans = min (ans, cnt);
		skip: ;
	}
	cout << ans << endl;
}
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i], g = gcd (g, A[i]);
	for (int i = 0; i < n; i++)
		A[i] /= g;
	if (check ()) {
		solve ();
	} else {
		solve2 ();
	}
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, ans = 1e9, A[N];
map<ll,ll> C[N];
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i];
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		ll cur = i -> second, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0 && A[j] != cur)
				goto jmp;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, ans = 1e9, A[N];
map<ll,ll> C[N];
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i];
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
		C[i][A[i]] = 1;
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		ll cur = i -> first, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0) goto jmp;
			if (cur == A[j]) continue;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, g, A[N];

ll gcd (ll a, ll b)
{
	if (a == 0)
		return b;
	return gcd (b % a, a);
}
bool check (void)
{
	for (int i = 0; i < n; i++) {
		int t = A[i];
		while (t % 2 == 0) t /= 2;
		if (t > 1) return false;
	}
	return true;
}
void solve (void)
{
	int ans = 1e9, T[64] = {0};
	for (int i = 0; i < n; i++) {
		int c = 0;
		while (A[i] % 2 == 0) {
			c++;
			A[i] /= 2;
		}
		T[c] += 1;
	}
	for (int i = 0; i < 64; i++) {
		int c = 0;
		for (int j = 0; j < 64; j++)
			c += abs (j - i) * A[j];
		ans = min (ans, c);
	}
	cout << ans << endl;
}
void solve2 (void)
{
	int ans = 1e9 + 7;
	sort (A, A + n);
	map<int,int> C[N];
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		while (t > 0) {
			c += 1;
			C[i][t] = c;
			t /= 2;
		}
	}
	for (auto it = C[0].begin (); it != C[0].end (); it++) {
		int cur = it->first, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (C[i][cur] == 0) goto skip;
			cnt += C[i][cur] - 1;
		}
		ans = min (ans, cnt);
		skip: ;
	}
	cout << ans << endl;
}
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i], g = gcd (g, A[i]);
	for (int i = 0; i < n; i++)
		A[i] /= g;
	if (check ()) {
		solve ();
	} else {
		solve2 ();
	}
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, g, A[N];

ll gcd (ll a, ll b)
{
	if (a == 0)
		return b;
	return gcd (b % a, a);
}
bool check (void)
{
	for (int i = 0; i < n; i++) {
		int t = A[i];
		while (t % 2 == 0) t /= 2;
		if (t > 1) return false;
	}
	return true;
}
void solve (void)
{
	int ans = 1e9, T[64] = {0};
	for (int i = 0; i < n; i++) {
		int c = 0;
		while (A[i] % 2 == 0) {
			c++;
			A[i] /= 2;
		}
		T[c] += 1;
	}
	for (int i = 0; i < 64; i++) {
		int c = 0;
		for (int j = 0; j < 64; j++)
			c += abs (j - i) * A[j];
		ans = min (ans, c);
	}
	cout << ans << endl;
}
void solve2 (void)
{
	int ans = 1e9 + 7;
	sort (A, A + n);
	map<int,int> C[N];
	for (int i = 0; i < n; i++) {
		ll t = A[i], c = 0;
		while (t > 0) {
			c += 1;
			C[i][t] = c;
			t /= 2;
		}
		t = A[i], c = 0;
		while (t < 1e9 + 7) {
			c += 1;
			C[i][t] = c;
			t *= 2;
		}
	}
	for (auto it = C[0].begin (); it != C[0].end (); it++) {
		int cur = it->first, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (C[i][cur] == 0) goto skip;
			cnt += C[i][cur] - 1;
		}
		ans = min (ans, cnt);
		skip: ;
	}
	cout << ans << endl;
}
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i], g = gcd (g, A[i]);
	for (int i = 0; i < n; i++)
		A[i] /= g;
	if (check ()) {
		solve ();
	} else {
		solve2 ();
	}
	return 0;
}


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

const int N = 1e5 + 5, INF = 1e9 + 7;
int n, ans = 1e9, A[N], C[N], D[N], CN[N];
int main ()
{
	for (int i = 0; i < N; i++)
		D[i] = INF;
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
		scanf ("%d", &A[i]);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		vector <int> v;
		while (t > 0) {
			for (int j = 0; (t << j) <= N; j++) {
				int x = t << j;
				v.push_back (x);
				D[x] = min (D[x], c + j);
			}
			t /= 2;
			c += 1;
		}
		sort (v.begin (), v.end ());
		for (int i = 0; i < v.size (); i++) {
			if (i && v[i] == v[i-1]) continue;
			int reach = v[i];
			C[v[i]] += 1;
			CN[v[i]] += D[v[i]];
			D[v[i]] = INF;
		}
	}
	for (int i = 0; i < N; i++) {
		if (C[i] == n)
			ans = min (ans, CN[i]);
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, ans = 1e9, A[N];
map<ll,ll> C[N];
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i];
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		ll t = A[i], c = 0;
		C[i][A[i]] = 1;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
			for (int j = 1; t && (t << j) <= mx; j++)
				if (C[i][t << j] == 0)
					C[i][t << j] = c + j;
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		ll cur = i -> first, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0) goto jmp;
			if (cur == A[j]) continue;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, g, A[N];

ll gcd (ll a, ll b)
{
	if (a == 0)
		return b;
	return gcd (b % a, a);
}
bool check (void)
{
	for (int i = 0; i < n; i++) {
		int t = A[i];
		t /= g;
		while (t % 2 == 0) t /= 2;
		if (t > 1) return false;
	}
	return true;
}
void solve (void)
{
	int ans = 1e9, T[64] = {0};
	for (int i = 0; i < n; i++) {
		int c = 0;
		while (A[i] % 2 == 0) {
			c++;
			A[i] /= 2;
		}
		T[c] += 1;
	}
	for (int i = 0; i < 64; i++) {
		int c = 0;
		for (int j = 0; j < 64; j++)
			c += abs (j - i) * A[j];
		ans = min (ans, c);
	}
	cout << ans << endl;
}
void solve2 (void)
{
	int ans = 1e9 + 7;
	sort (A, A + n);
	map<int,int> C[N];
	for (int i = 0; i < n; i++) {
		ll t = A[i], c = 0;
		while (t > 0) {
			c += 1;
			C[i][t] = c;
			t /= 2;
		}
		t = A[i], c = 0;
		while (t < 1e9 + 7) {
			c += 1;
			C[i][t] = c;
			t *= 2;
		}
	}
	for (auto it = C[0].begin (); it != C[0].end (); it++) {
		int cur = it->first, cnt = 0;
		for (int i = 0; i < n; i++) {
			if (C[i][cur] == 0) goto skip;
			cnt += C[i][cur] - 1;
		}
		ans = min (ans, cnt);
		skip: ;
	}
	cout << ans << endl;
}
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i], g = gcd (g, A[i]);
	if (check ()) {
		solve ();
	} else {
		solve2 ();
	}
	return 0;
}


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

const int N = 1e5 + 5;
int n, ans = 1e9, A[N];
map<int,int> C[N];
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i];
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		C[i][A[i]] = 1;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
			for (int j = 1; t && (t << j) <= mx; j++)
				if (C[i][t << j] == 0)
					C[i][t << j] = c + j;
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		int cur = i -> first, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0) goto jmp;
			if (cur == A[j]) continue;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
int n, ans = 1e9, A[N];
map<int,int> C[N];
int main ()
{
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
		scanf ("%d", &A[i]);
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		C[i][A[i]] = 1;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
			for (int j = 1; t && (t << j) <= mx; j++)
				if (C[i][t << j] == 0)
					C[i][t << j] = c + j;
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		int cur = i -> first, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0) goto jmp;
			if (cur == A[j]) continue;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
long long int n, ans = 1e9, A[N];
map<ll,ll> C[N];
int main ()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> A[i];
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		ll t = A[i], c = 0;
		while (t > 0) {
			t >>= 1;
			c++;
			C[i][t] = c;
			C[i][2 * t] = C[i][2 * t] == 0 ? c + 1 : min (C[i][2 * t], c + 1);
		}
		t = A[i], c = 0;
		while (t <= mx) {
			t <<= 1;
			c++;
			C[i][t] = c;
		}
		C[i][A[i]] = 1;
	}
	for (auto i = C[0].begin (); i != C[0].end (); i++) {
		ll cur = i -> first, cnt = 0;
		for (int j = 0; j < n; j++) {
			if (C[j][cur] == 0) goto jmp;
			if (cur == A[j]) continue;
			cnt += C[j][cur];
		}
		ans = min (ans, cnt);
		jmp: ;
	}
	cout << ans << endl;
	return 0;
}


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

const int N = 1e5 + 5;
int n, ans = 1e9, A[N], C[N], CN[N];
int main ()
{
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
		scanf ("%d", &A[i]);
	int mx = *max_element (A, A + n);
	for (int i = 0; i < n; i++) {
		int t = A[i], c = 0;
		set <int> s;
		while (t > 0) {
			if (!s.count (t)) {
				s.insert (t);
				C[t] += 1;
				CN[t] += c;
			}
			for (int j = 1; (t << j) <= mx; j++) {
				if (s.count (t << j)) continue;
				C[t << j] += 1;
				CN[t << j] += c + j;
				s.insert (t << j);
			}
			t /= 2;
			c += 1;
		}
	}
	for (int i = 0; i < N; i++) {
		if (C[i] == n)
			ans = min (ans, CN[i]);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ll n;
    cin >> n;
    ll arr[n] , arr2[n] , two[n]={} , gc=0 , ans=1e18 , tmp , cnt=0;
    bool b=0;
    for (int i=0; i<n; i++)
    {
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for (int i=0; i<n; i++)
        arr2[i]=arr[i];

    for(int i=0; i<=20; i++)
    {
        cnt=0 , b=0;
        while(arr[0]>1 && cnt<i)
        {
            cnt++ , arr[0] /= 2;
        }
        tmp = arr[0];

        for (int j=1; j<n; j++)
        {
            while(arr[j] > tmp)
            {
                arr[j] /= 2 , cnt++;
            }

            if (arr[j] != tmp)
            {
                while(arr[j] < tmp)
                {
                    arr[j] *= 2 , cnt++;
                }
            }
            if (arr[j] != tmp) {b=1; break;}
        }

        if (!b) ans = min(ans , cnt);

        for (int j=0; j<n; j++)
            arr[j] = arr2[j];
    }

    for(int i=0; i<=20; i++)
    {
        cnt=0 , b=0;
        while(cnt<i)
        {
            cnt++ , arr[0] *= 2;
        }
        tmp = arr[0];

        for (int j=1; j<n; j++)
        {
            while(arr[j] > tmp)
            {
                arr[j] /= 2 , cnt++;
            }

            if (arr[j] != tmp)
            {
                while(arr[j] < tmp)
                {
                    arr[j] *= 2 , cnt++;
                }
            }
            if (arr[j] != tmp) {b=1; break;}
        }

        if (!b) ans = min(ans , cnt);

        for (int j=0; j<n; j++)
            arr[j] = arr2[j];
    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ll n;
    cin >> n;
    ll arr[n] , arr2[n] , two[n]={} , gc=0 , ans=1e18 , tmp , cnt=0 , pw=1;
    map<ll , ll> mp;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=50; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

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

    sort(arr,arr+n);
    for (int i=0; i<n; i++)
        arr2[i]=arr[i];

    for (int k=0; k<=20; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=20; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;

            for (int j=1; j<n; j++)
            {
                while(arr[j] > 0)
                {
                    if (tmp%arr[j]==0 && (mp[tmp/arr[j]] || tmp==arr[j]) ) {cnt += mp[tmp/arr[j]]; b=1; break;}
                    arr[j] /= 2 , cnt++;
                }

                if (!b) goto here;
            }

            ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    int n;
    ll tmp , cnt=0 , pw=1 , ans=1e18;
    scanf("%d",&n);
    int arr[n] , arr2[n];
    unordered_map<ll,ll> mp , visited;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=50; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
        arr2[i]=arr[i];
    }

    for (int k=0; k<=17; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=17; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i && cnt < 200001)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;
            if (visited[tmp]) continue;
            visited[tmp]=1;

            for (int j=1; j<n; j++)
            {
                b=0;
                while(arr[j] > 0)
                {
                    if (tmp%arr[j]==0 && (mp[tmp/arr[j]] || tmp==arr[j]) ) {cnt += mp[tmp/arr[j]]; b=1; break;}
                    arr[j] /= 2 , cnt++;
                }

                if (!b) goto here;
            }

            ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ll n;
    cin >> n;
    ll arr[n] , arr2[n] , two[n]={} , gc=0 , ans=1e18 , tmp , cnt=0 , pw=1;
    map<ll , ll> mp;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=20; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

    for (int i=0; i<n; i++)
    {
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for (int i=0; i<n; i++)
        arr2[i]=arr[i];

    for (int k=0; k<=20; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=20; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;

            for (int j=1; j<n; j++)
            {
                while(arr[j] > 1)
                {
                    if (tmp%arr[j]==0 && (mp[tmp/arr[j]] || tmp==arr[j]) ) {cnt += mp[tmp/arr[j]]; b=1; break;}
                    arr[j] /= 2 , cnt++;
                }

                if (!b) goto here;
            }

            if (b) ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
int cnt[500000] , op[500000] , arr[100005] , anss[500000];

void bfs(int node)
{
    queue<pair< pair<int,int> , pair<int,int> > > q;
    int x,lvl,canDiv,canMul;
    q.push({{node,0} , {1,1}});
    while(q.size())
    {
        x = q.front().f.f , lvl=q.front().f.s , canDiv=q.front().s.f , canMul=q.front().s.s;
        cnt[x]++ , anss[x] += lvl;
        q.pop();

        if (2*x < 400001 && canMul) q.push({{2*x , lvl+1} , {0 , canMul} });
        if (x%2 && x>1 && canDiv) q.push({{x/2 , lvl+1} , {canDiv , canMul} });
        else if (x%2==0 && canDiv && x>1) q.push({{x/2 , lvl+1} , {canDiv , 0} });
    }
}
int main()
{

    int n;
    ll tmp , tmp2 , c=0, pw=1 , ans=1e18;
    scanf("%d",&n);

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }

    for (int i=0; i<n; i++)
    {
        bfs(arr[i]);
    }

    for(int i=0; i<=200000; i++)
       if (cnt[i]==n) ans = min(ans , (ll)anss[i]);

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{

    int n;
    ll tmp , cnt=0 , pw=1 , ans=1e18;
    scanf("%d",&n);
    int arr[n] , arr2[n];
    unordered_map<ll,ll> mp , visited;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=50; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
        arr2[i]=arr[i];
    }

  //  sort(arr,arr+n);
  //  for (int i=0; i<n; i++)
  //      arr2[i]=arr[i];

    for (int k=0; k<=17; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=17; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;
            if (visited[tmp] || tmp > 200000) continue;
            visited[tmp]=1;

            for (int j=1; j<n; j++)
            {
                b=0;
                while(arr[j] > 0)
                {
                    if (tmp%arr[j]==0 && (mp[tmp/arr[j]] || tmp==arr[j]) ) {cnt += mp[tmp/arr[j]]; b=1; break;}
                    arr[j] /= 2 , cnt++;
                }

                if (!b) goto here;
            }

            ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    int n;
    ll tmp , tmp2 , cnt=0 , pw=1 , ans=1e18;
    scanf("%d",&n);
    int arr[n] , arr2[n];
    vector<int> vec[n];

    unordered_map<ll,ll> mp , visited;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=50; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
        arr2[i]=arr[i];
        vec[i].push_back(arr[i]);
        while(arr[i] > 1)
        {
            arr[i] /= 2;
            if (arr[i]) vec[i].push_back(arr[i]);
        }
    }
    for (int i=0; i<n; i++)
        arr[i]=arr2[i];

    for (int k=0; k<=17; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=17; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;
            if (visited[tmp] || tmp > 200000) continue;
            visited[tmp]=1;

            for (int j=1; j<n; j++)
            {
                b=0;
                for(int l=0; l<vec[j].size(); l++)
                {
                    tmp2 = tmp/vec[j][l];
                    if (tmp2*vec[j][l]==tmp && (mp[tmp2] || tmp==vec[j][l]) ) {cnt += (l+mp[tmp2]); b=1; break;}
                }

                if (!b) goto here;
            }

            ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
int cnt[500000] , op[500000] , anss[500000];

int main()
{

    int n;
    ll tmp , tmp2 , c=0, pw=1 , ans=1e18;
    scanf("%d",&n);
    int arr[n] , arr2[n];

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    for(int i=0; i<300000; i++)
        op[i] = 1e9;

    for (int i=0; i<n; i++)
    {
        unordered_set<int> st;
        tmp = arr[i];

        for (int j=0; j<=17; j++)
        {
            for (int k=0; k<=17; k++)
            {
                if (tmp > 200000) continue;

                st.insert(tmp) , op[tmp] = min(op[tmp] , j+k);

                tmp *= 2;
            }

            arr[i] /= 2 , tmp=arr[i];
            if (!arr[i]) break;
        }

        for(unordered_set<int>::iterator it=st.begin(); it!=st.end(); it++)
        {
            anss[*it] += op[*it] , cnt[*it]++;
            op[*it] = 1e9;
        }
    }

    for(int i=0; i<=200000; i++)
       if (cnt[i]==n) ans = min(ans , (ll)anss[i]);

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ll n;
    cin >> n;
    ll arr[n] , arr2[n] , two[n]={} , gc=0 , ans=1e18 , tmp , cnt=0;
    bool b=0;
    for (int i=0; i<n; i++)
    {
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for (int i=0; i<n; i++)
        arr2[i]=arr[i];

    for (int k=0; k<=20; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=20; i++)
        {
            cnt=0 , b=0;
            while(cnt<i)
            {
                cnt++ , arr[0] *= 2;
            }
            tmp = arr[0];
            cnt += c;

            for (int j=1; j<n; j++)
            {
                while(arr[j] > tmp)
                {
                    arr[j] /= 2 , cnt++;
                }

                if (arr[j] != tmp)
                {
                    while(arr[j] < tmp)
                    {
                        arr[j] *= 2 , cnt++;
                    }
                }
                if (arr[j] != tmp) {b=1; break;}
            }

            if (!b) ans = min(ans , cnt);

            for (int j=0; j<n; j++)
                arr[j] = arr2[j];
        }

    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ll n;
    cin >> n;
    ll arr[n] , arr2[n] , two[n]={} , gc=0 , ans=0;
    for (int i=0; i<n; i++)
    {
        cin >> arr[i];
        arr2[i]=arr[i];
        while(arr[i]%2==0)
        {
            two[i]++ , arr[i]/=2;
        }
        gc = __gcd(gc , arr[i]);
    }

    for (int i=0; i<n; i++)
    {
        if (arr[i] != gc)
        {
            for(int j=0; j<n; j++)
            {
                while(arr2[j]>1)
                {
                    arr2[j] /= 2 , ans++;
                }
            }
            return cout << ans << "\n" , 0;
        }
    }

    ans = 1e18;
    for (int i=0; i<=30; i++)
    {
        ll cnt=0;
        for (int j=0; j<n; j++)
        {
            cnt += abs(two[j] - i);
        }
        ans = min(ans , cnt);
    }
    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
int cnt[500000] , op[500000] , arr[100005] , anss[500000];

void bfs(int node)
{
    queue<pair< pair<int,int> , pair<int,int> > > q;
    int x,lvl,canDiv,canMul;
    q.push({{node,0} , {1,1}});
    while(q.size())
    {
        x = q.front().f.f , lvl=q.front().f.s , canDiv=q.front().s.f , canMul=q.front().s.s;
        cnt[x]++ , anss[x] += lvl;
        q.pop();

        if (2*x < 400001 && canMul) q.push({{2*x , lvl+1} , {0 , canMul} });
        if (x%2 && x>1 && canDiv) q.push({{x/2 , lvl+1} , {canDiv , 1} });
        else if (x%2==0 && canDiv && x>1) q.push({{x/2 , lvl+1} , {canDiv , 0} });
    }
}
int main()
{

    int n;
    ll tmp , tmp2 , c=0, pw=1 , ans=1e18;
    scanf("%d",&n);

    for (int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    for(int i=0; i<300000; i++)
        op[i] = 1e9;

    for (int i=0; i<n; i++)
    {
        bfs(arr[i]);
    }

    for(int i=0; i<=200000; i++)
       if (cnt[i]==n) ans = min(ans , (ll)anss[i]);

    printf("%d\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll n , tmp , cnt=0 , pw=1 , ans=1e18;
    cin >> n;
    int arr[n] , arr2[n];
    unordered_map<ll,ll> mp , visited;
    bool b=0;
    mp[1]=0;
    for (int i=1; i<=50; i++)
    {
        pw *= 2;
        mp[pw]=i;
    }

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

    sort(arr,arr+n);
    for (int i=0; i<n; i++)
        arr2[i]=arr[i];

    for (int k=0; k<=17; k++)
    {
        int c=0;
        while(arr[0]>1 && c<k)
        {
            c++ , arr[0] /= 2;
        }

        for(int i=0; i<=17; i++)
        {
            cnt=0 , b=0 , tmp=arr[0];
            while(cnt<i)
            {
                cnt++ , tmp *= 2;
            }
            cnt += c;
            if (visited[tmp]) continue;
            visited[tmp]=1;

            for (int j=1; j<n; j++)
            {
                while(arr[j] > 0)
                {
                    if (tmp%arr[j]==0 && (mp[tmp/arr[j]] || tmp==arr[j]) ) {cnt += mp[tmp/arr[j]]; b=1; break;}
                    arr[j] /= 2 , cnt++;
                }

                if (!b) goto here;
            }

            ans = min(ans , cnt);

            here:

            for (int j=1; j<n; j++)
                arr[j] = arr2[j];
        }
        arr[0] = arr2[0];
    }

    cout << ans << "\n";

    return 0;
}


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

#define ll long long
#define ld long double
#define sd(x) scanf("%d",&x)
#define pb push_back

int cnt[1000001],vis[1000001];

void fun(int x, int cn)
{
    //cout<<x<<cn;
     x=2*x;
    cn++;
   while(x < 100009)
   {
       vis[x]++;
       cnt[x]+=cn++;
      x*=2;
   }

}

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt","r",stdin);

int t=1;
//cin>>t;

while(t--){

int n;
cin>>n;

int mx=0;

for(int i=1;i<=n;i++){
     int x,ss,tt;
     cin>>x;

     int k=x,cc=0;
     while(k){
     cnt[k]+=cc;
     vis[k]+=1;
     if(k&1 && k>1)
     fun(k/2,cc+1);
     k/=2;
     cc++;
     }

      k=2*x,cc=1;
     while(k<=1000009){
     cnt[k]+=cc;
     vis[k]+=1;
     k*=2;
     cc++;
     }

}

int ans = INT_MAX;

for(int i=1;i<=1000000;i++)
if(vis[i]==n)
{
   //  cout<<i<<endl;

     //   cout<<cnt[4]<<" ";
     ans= min(ans,cnt[i]);

}
cout<<ans;

}
}


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

#define ll long long
#define ld long double
#define sd(x) scanf("%d",&x)
#define pb push_back

int cnt[1000001],vis[1000001];

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt","r",stdin);

int t=1;
//cin>>t;

while(t--){

int n;
cin>>n;

int mx=0;

for(int i=1;i<=n;i++){
     int x,ss,tt;
     cin>>x;

     int k=x/2,cc=1;
     while(k){
     cnt[k]+=cc;
     vis[k]+=1;
     k/=2;
     cc++;
     }

      k=2*x,cc=1;
     while(k<=1000009){
     cnt[k]+=cc;
     vis[k]+=1;
     k*=2;
     cc++;
     }
     vis[x]++;

}

ll ans = INT_MAX;

for(int i=1;i<=1000000;i++)
if(vis[i]==n)
{
   //  cout<<i<<endl;
     ans= min(ans,cnt[i]);
}
cout<<ans;

}
}


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

#define ll long long
#define ld long double
#define sd(x) scanf("%d",&x)
#define pb push_back

int cnt[1000001],vis[1000001];

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt","r",stdin);

int t=1;
//cin>>t;

while(t--){

int n;
cin>>n;

int mx=0;

for(int i=1;i<=n;i++){
     int x,ss,tt;
     cin>>x;

     int k=x/2,cc=1;
     while(k){
     cnt[k]+=cc;
     vis[k]+=1;
     k/=2;
     cc++;
     }

      k=2*x,cc=1;
     while(k<=100009){
     cnt[k]+=cc;
     vis[k]+=1;
     k*=2;
     cc++;
     }
     vis[x]++;

}

int ans = 1e8;

for(int i=1;i<=100000;i++)
if(vis[i]==n)
{
   //  cout<<i<<endl;
     ans= min(ans,cnt[i]);
}
cout<<ans;

}
}


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

#define ll long long
#define ld long double
#define sd(x) scanf("%d",&x)
#define pb push_back

int cnt[1000001],vis[1000001];

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt","r",stdin);

int t=1;
//cin>>t;

while(t--){

int n;
cin>>n;

int mx=0;

for(int i=1;i<=n;i++){
     int x,ss,tt;
     cin>>x;

     int k=x/2,cc=1;
     while(k){
     cnt[k]+=cc;
     vis[k]+=1;
     k/=2;
     cc++;
     }

      k=2*x,cc=1;
     while(k<=1000009){
     cnt[k]+=cc;
     vis[k]+=1;
     k*=2;
     cc++;
     }
     vis[x]++;

}

int ans = 1e8;

for(int i=1;i<=1000000;i++)
if(vis[i]==n)
{
   //  cout<<i<<endl;
     ans= min(ans,cnt[i]);
}
cout<<ans;

}
}


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

#define ll long long
#define ld long double
#define sd(x) scanf("%d",&x)
#define pb push_back

ll cnt[1000001],vis[1000001];

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt","r",stdin);

int t=1;
//cin>>t;

while(t--){

ll n;
cin>>n;

int mx=0;

for(int i=1;i<=n;i++){
     int x,ss,tt;
     cin>>x;

     int k=x/2,cc=1;
     while(k){
     cnt[k]+=cc;
     vis[k]+=1;
     k/=2;
     cc++;
     }

      k=2*x,cc=1;
     while(k<=1000009){
     cnt[k]+=cc;
     vis[k]+=1;
     k*=2;
     cc++;
     }
     vis[x]++;

}

ll ans = INT_MAX;

for(int i=1;i<=1000000;i++)
if(vis[i]==n)
{
   //  cout<<i<<endl;
     ans= min(ans,cnt[i]);
}
cout<<ans;

}
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

#define NN 101010

int bct[NN], bpt[NN];
int a[NN], b[NN], n;

int solve(int x) {
	int ans=0;
	for(int i=1; i<=n; i++) {
		b[i]=a[i];
		if(x==1) while(bpt[b[i]]>1) b[i]>>=1, ans++;
		else {
			while(b[i]%x || bpt[b[i]%x]>1) b[i]>>=1, ans++;
			if(!b[i]) return inf;
		}
	}
	int sum=0;
	for(int i=1; i<=n; i++) {
		b[i]/=x;
		sum+=bct[b[i]];
	}
	sum/=n;
	for(int i=1; i<=n; i++) {
		ans+=abs(sum-bct[b[i]]);
	}
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	for(int i=NN; i--; ) {
		bct[i]=__builtin_ctz(i);
		bpt[i]=__builtin_popcount(i);
	}

	scanf("%d", &n);
	for(int i=1; i<=n; i++) scanf("%d", a+i);

	int ans=inf;
	for(int u=a[1]; u; u>>=1) {
		smin(ans, solve(u));
	}
	printf("%d", ans);
}


#include <bits/stdc++.h>

using namespace std;

const int NN = 210100;
int a[NN];
map<int, int> mp;
int val[NN];
int vst[NN];
int vvv[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int n; cin >> n;
	for(int i = 1; i <= n; i ++ ) cin >> a[i];

	for(int i = 1; i <= n; i ++ ){
		int cnt = 0;
		for(int j = a[i]; j; j >>= 1, cnt ++){
			for(int k = j, ct = cnt ; k < NN ; k <<= 1, ct ++){
				if(vvv[k] == i) break;
				val[k] += ct ;
				vst[k] ++;
				vvv[k] = i;
			}
		}
	}

	int ans = 0x3f3f3f3f;
	for(int i = 0; i < NN; i ++ ){
		if(vst[i] < n) continue;
		ans = min(val[i], ans);
	}

	cout<<ans;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN = 1010100;
int a[NN];
vector<int> adj[NN];
map<int, int> mp;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int n; cin >> n;
	for(int i = 1; i <= n; i ++ ) cin >> a[i];

	for(int i = 1; i <= n; i ++ ){
		int cnt = 0;
		mp.clear();
		for(int j = a[i]; j; j >>= 1, cnt ++){
			for(int k = j, ct = cnt + 1; k < NN ; k <<= 1, ct ++){
				if(mp[k]) break;
				mp[k] = ct;
			}
		}
		for(auto p :mp){
			int u = p.first, v = p.second;
			adj[u].push_back(v);
		}
	}

	int ans = 0x3f3f3f3f;
	for(int i = 0; i < NN; i ++ ){
		if(adj[i].size() < n) continue;
		int tmp = 0;
		for(int u: adj[i]){
			tmp += u - 1;
		}
		ans = min(tmp, ans);
	}

	cout<<ans;

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int MM=101010;

int a[MM];
vector<int> adj[MM];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0; j; j>>=1){
			adj[j].push_back(t++);
		}
		for(int j=a[i]*2,t=1; j<MM; j<<=1){
			adj[j].push_back(t++);
		}
	}

	int ans=MM;

	for(int i=1;i<MM;i++){
		if(adj[i].size()<n)continue;
		int tmp=0;
		for(auto u:adj[i]){
			tmp+=u;
		}
//		cout<<i<<" "<<tmp<<endl;
		ans=min(ans,tmp);
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;
typedef long long INT;

#define NN 100010
int a[NN];
int f[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(f, -1, sizeof f);
	int t = 0;
	for(int i=1; i<NN; i*=2){
		f[i] = t;
		t++;
	}
	int n;
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &a[i]);
	}
	int ans = inf;
	int x = a[1];
	while(x){
		int y = x, r = 0;
		while(y % 2 == 0){
			y /= 2;
			r ++;
		}
		INT c = x;
		int step = 20;
		while(step--){
			int res = 0;
			for(int i=1; i<=n; i++){
				int v = a[i];
				while(v > c){
					res++;
					v /= 2;
				}
				if(v == c) continue;
				while(v){
					if(v % y == 0 && f[v/y] != -1){
						res += r - f[v/y];
						break;
					}
					res++;
					v /= 2;
				}
				if(!v){
					res = -1;
					break;
				}
			}
			if(res != -1) ans = min(ans, res);
			r++;
			c *= 2;
		}
		x /= 2;
	}
	printf("%d\n", ans);
    return 0;

}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

#define NN 101010

int bct[NN];
int a[NN], n;
int b[NN];

int solve(int x) {
	for(int i=1; i<=n; i++) b[i]=a[i];
	int ans=0;
	for(int i=1; i<=n; i++) {
		if(x==1) while(b[i]&1) b[i]>>=1, ans++;
		else {
			while(b[i]%x) b[i]>>=1, ans++;
			if(!b[i]) return inf;
		}
	}
	int sum=0;
	for(int i=1; i<=n; i++) {
		b[i]/=x;
		sum+=bct[b[i]];
	}
	sum/=n;
	for(int i=1; i<=n; i++) {
		ans+=abs(sum-bct[b[i]]);
	}
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	for(int i=NN; i--; ) {
		bct[i]=__builtin_ctz(i);
	}

	scanf("%d", &n);
	for(int i=1; i<=n; i++) scanf("%d", a+i);

	int ans=inf;
	for(int u=a[1]; u; u>>=1) {
		smin(ans, solve(u));
	}
	printf("%d", ans);
}


#include<bits/stdc++.h>
#define NN 200100
#define inf 1e9
using namespace std;

int ans[NN];
int vst[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		int ct = 1, tmp = 0;
		while(a){
			if(tmp == a * 2) {
				ans[a] += ct;
				vst[a]++;
				tmp = a;
				a /= 2;
				ct++;
				continue;
			}
			tmp = a;
			int val = ct;
			while(tmp < 200000){
				ans[tmp] += val;
				vst[tmp]++;
				val++;
				tmp *= 2;
			}
			tmp = a;
			a /= 2;
			ct++;
		}
	}
	int mn = inf;
	for(int i = 1; i < 2e5; i++) if(vst[i] >= n)mn = min(mn, ans[i]);
	printf("%d\n", mn-n);
	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define NN 101010
#define y second
#define x first

using namespace std;
using VI=vector<int>;
using pii=pair<int, int>;

int a[NN], n, mx;
vector<pii> vec;
VI ans[NN];

int solve(int x) {
	vec.clear();
	for(int num=0; x; x>>=1, num++) {
		for(int y=x, cnt=0; y<=mx; y<<=1) {
			vec.pb({y, num+cnt});
			cnt++;
		}
	}
	sort(vec.begin(), vec.end());
	for(int i=0, j=0; i<vec.size(); i=j) {
		int mn=vec[i].y;
		for(; j<vec.size() && vec[i].x==vec[j].x; j++) {
			if(vec[j].y<mn) mn=vec[j].y;
		}
		ans[vec[i].x].pb(mn);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	scanf("%d", &n);
	for(int i=n; i--; ) {
		scanf("%d", a+i);
		if(a[i]>mx) mx=a[i];
	}
	for(int i=n; i--; ) solve(a[i]);

	int mn=inf;
	for(int i=1; i<=mx; i++) if(ans[i].size()==n) {
		int sum=0;
		for(int x: ans[i]) sum+=x;
		if(sum<mn) mn=sum;
	}
	printf("%d", mn);
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int MM=101010;

int a[MM];
vector<int> adj[MM];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0;j;j>>=1){
			adj[j].push_back(t++);
		}
		for(int j=a[i]*2,t=1;j<MM;j<<=1){
			adj[j].push_back(t++);
		}
	}

	int ans=MM;

	for(int i=1;i<MM;i++){
		if(adj[i].size()<n)continue;
		int tmp=0;
		for(auto u:adj[i]){
			tmp+=u;
		}
//		cout<<i<<" "<<tmp<<endl;
		ans=min(ans,tmp);
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int MM=101010;

int a[MM];
vector<int> adj[MM];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0; j; j>>=1){
			adj[j].push_back(t++);
		}
		for(int j=a[i]*2,t=1; j<MM; j<<=1){
			adj[j].push_back(t++);
		}
	}

	int ans=1e9;

	for(int i=1;i<MM;i++){
		if(adj[i].size()<n)continue;
		int tmp=0;
		for(auto u:adj[i]){
			tmp+=u;
		}
//		cout<<i<<" "<<tmp<<endl;
		ans=min(ans,tmp);
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int NN=101010;
const int MM=44*NN;

map<int,int> mp[MM];

int a[MM];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0;j;j>>=1){
			mp[j][i]=t++;
		}
		for(int j=a[i]*2,t=1;j<MM;j<<=1){
			mp[j][i]=t++;
		}
	}

	int ans=MM;

	for(int i=1;i<MM;i++){
		if(mp[i].size()<n)continue;
		int tmp=0;
		for(auto u:mp[i]){
			tmp+=u.y;
		}
		ans=min(ans,tmp);
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN = 101010;
int a[NN];
vector<int> adj[NN];
map<int, int> mp;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int n; cin >> n;
	for(int i = 1; i <= n; i ++ ) cin >> a[i];

	for(int i = 1; i <= n; i ++ ){
		int cnt = 0;
		mp.clear();
		for(int j = a[i]; j; j >>= 1, cnt ++){
			for(int k = j, ct = cnt + 1; k < NN ; k <<= 1, ct ++){
				if(mp[k]) mp[k] = min(mp[k], ct);
				else mp[k] = ct;
			}
		}
		for(auto p :mp){
			int u = p.first, v = p.second;
			adj[u].push_back(v);
		}
	}

	int ans = NN;
	for(int i = 0; i < NN; i ++ ){
		if(adj[i].size() < n) continue;
		int tmp = 0;
		for(int u: adj[i]){
			tmp += u - 1;
		}
		ans = min(tmp, ans);
	}

	cout<<ans<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN = 1010100;
int a[NN];
map<int, int> mp;
int val[NN];
int vst[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int n; cin >> n;
	for(int i = 1; i <= n; i ++ ) cin >> a[i];

	for(int i = 1; i <= n; i ++ ){
		int cnt = 0;
		mp.clear();
		for(int j = a[i]; j; j >>= 1, cnt ++){
			for(int k = j, ct = cnt + 1; k < NN ; k <<= 1, ct ++){
				if(mp[k]) break;
				mp[k] = ct;
			}
		}
		for(auto p :mp){
			int u = p.first, v = p.second;
			vst[u] ++;
			val[u] += v - 1;
		}
	}

	int ans = 0x3f3f3f3f;
	for(int i = 0; i < NN; i ++ ){
		if(vst[i] < n) continue;
		ans = min(val[i], ans);
	}

	cout<<ans;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define NN 110000
#define inf 0x3f3f3f3f
int a[NN];
int vis[NN];
int ans[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n; scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int x; scanf("%d", &x);
		int res = 0, pre = 0;
		while(x){
			if(2 * x == pre){
				vis[x]++;
				ans[x] += res;
				res++;
				pre = x;
				x /= 2;
				continue;
			}
			int y = x, cnt = res;
			while(y <= 100000){
				vis[y]++;
				ans[y] += cnt;
				cnt++;
				y *= 2;
			}
			pre = x;
			x /= 2;
			res++;
		}
	}
	int rlt = inf;
	for(int i = 1; i <= 100000; i++){
		if(vis[i] == n) rlt = min(rlt, ans[i]);
	}
	printf("%d\n", rlt);
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int NN=101010;
const int MM=44*NN;

typedef pair<int,int> pii;
map<int,int> mp[MM];

pii p[MM];
int a[NN],cnt[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0;j;j>>=1){
			mp[j][i]=t++;
		}
		for(int j=a[i]*2,t=1;j<NN;j<<=1){
			mp[j][i]=t++;
		}
		if(a[i]!=a[1])ok=0;
	}

	if(ok){
		puts("0");
		return 0;
	}

	sort(p,p+nn);

	int ans=MM;

	for(int i=1;i<NN;i++){
		if(mp[i].size()<n)continue;
		int tmp=0;
		for(auto u:mp[i]){
			tmp+=u.y;
		}
		ans=min(ans,tmp);
	}

	for(int i=1;i<NN;i++){
		if(cnt[i]){
			ans=min(ans,cnt[i]);
//			cout<<i<<" "<<cnt[i]<<endl;
		}
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define NN 110000
int a[NN];
int b[NN][20];
int p[30];
int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	p[0] = 1;
	for(int i = 1; i < 30; i++) p[i] = p[i - 1] * 2;
	int n; scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d", &a[i]);
	for(int i = 0; i < n; i++){
		int t = __builtin_popcount(a[i]);
		int id = lower_bound(p, p + n, a[i]) - p;
		if(a[i] < p[id]){
			b[i][0] = id - 1;
			for(int j = 1; j < 18; j++) b[i][j] = b[i][j - 1] + 1;
		}else{
			b[i][id] = 0;
			for(int j = 0; j < id; j++) b[i][j] = id - j;
			for(int j = id; j < 18; j++) b[i][j] = j - id;
		}
	}
	int mn = 1000000000;
	for(int i = 0; i < 18; i++){
		int res = 0;
		for(int j = 0; j < n; j++) res += b[j][i];
		mn = min(mn, res);
	}
	printf("%d\n", mn);
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int MM=101010;

int a[MM];
vector<int> adj[MM];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0; j; j>>=1){
			adj[j].push_back(t++);
		}
		for(int j=a[i]*2,t=1; j<MM; j<<=1){
			adj[j].push_back(t++);
		}
	}

	int ans=0x3f3f3f3f;

	for(int i=1;i<MM;i++){
		if(adj[i].size()<n)continue;
		int tmp=0;
		for(auto u:adj[i]){
			tmp+=u;
		}
//		cout<<i<<" "<<tmp<<endl;
		ans=min(ans,tmp);
	}

	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

#define NN 101010

int a[NN], n, mx;
vector<pii> vec;
VI ans[NN];

int solve(int x) {
	vec.clear();
	for(int num=0; x; x>>=1, num++) {
		for(int y=x, cnt=0; y<=mx; y<<=1) {
			vec.pb({y, num+cnt});
			cnt++;
		}
	}
	sort(vec.begin(), vec.end());
	for(int i=0, j=0; i<vec.size(); i=j) {
		int mn=vec[i].y;
		for(; j<vec.size() && vec[i].x==vec[j].x; j++) {
			if(vec[j].y<mn) mn=vec[j].y;
		}
		ans[vec[i].x].pb(mn);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	scanf("%d", &n);
	for(int i=1; i<=n; i++) {
		scanf("%d", a+i);
		if(a[i]>mx) mx=a[i];
	}
	for(int i=1; i<=n; i++) solve(a[i]);

	int mn=inf;
	for(int i=1; i<=mx; i++) if(ans[i].size()==n) {
		int sum=0;
		for(int x: ans[i]) sum+=x;
		if(sum<mn) mn=sum;
	}
	printf("%d", mn);
}


#include <bits/stdc++.h>
#define x first
#define y  second

using namespace std;

const int NN=101010;
const int MM=44*NN;

typedef pair<int,int> pii;
map<int,int> mp[MM];

pii p[MM];
int a[NN],cnt[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n; cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",a+i);

	int nn=0,ok=1;
	for(int i=1;i<=n;i++){
		for(int j=a[i],t=0;j;j>>=1){
			mp[j][i]=t++;
		}
		for(int j=a[i]*2,t=1;j<NN;j<<=1){
			mp[j][i]=t++;
		}
		if(a[i]!=a[1])ok=0;
	}

	if(ok){
		puts("0");
		return 0;
	}

	sort(p,p+nn);

	int ans=MM;

	for(int i=1;i<NN;i++){
		if(mp[i].size()<n)continue;
		int tmp=0;
		for(auto u:mp[i]){
			tmp+=u.y;
		}
		ans=min(ans,tmp);
	}

	for(int i=1;i<NN;i++){
		if(cnt[i]){
			ans=min(ans,cnt[i]);
			cout<<i<<" "<<cnt[i]<<endl;
		}
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define NN 110000
//#define x first
//#define y second
int p[30];
int a[NN];
vector <int> res;
int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	p[0] = 1;
	for(int i = 1; i < 30; i++) p[i] = p[i - 1] * 2;
	int n; scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d", &a[i]);
	sort(a, a + n);
	int ans = 0;
	for(int i = 0; i < n; i++){
		int id = lower_bound(p, p + 30, a[i]) - p;
		if(p[id] > a[i]) ans += (id - 1), res.push_back(0);
		else res.push_back(id);
	}
	int sum = 0, cnt = 0;
	for(int i = 0; i < res.size(); i++) sum += res[i];
	int N = round(sum / n);
	for(int i = 0; i < res.size(); i++) ans += abs(N - res[i]);
	printf("%d\n", ans);
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
#define pll pair < long , long >
#define vll vector < pll >
#define ml map < long , long >
#define mll map < pll , long >
#define x first
#define y second
#define vl vector < long >
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int n,a[100005],i,ans=1e9,cur,d,f,j;
int main()
{
    io
    vl v;
    cin>>n;
    for(i=0;i<n;++i)
    cin>>a[i];
    sort(a,a+n);
    d=a[0];
    while(d>0)
    {
        v.pb(d);
        d/=2;
    }
    d=a[0];
    while(d<=5e5)
    {
        d*=2;
        v.pb(d);
    }
    for(i=0;i<v.size();++i)
    {
        cur=v[i];
        if(cur>5e5) break;
        int cnt=0;
        f=1;
        for(j=0;j<n;++j)
        {
            d=a[j];
            if(d<cur)
            {
                while(d<cur)
                d*=2,cnt++;
                if(d!=cur) f=0;
            }
            else
            {
                while(d>cur)
                d/=2,cnt++;
                if(d!=cur) f=0;
            }
            if(!f) break;
        }
        if(f) ans=min(ans,cnt);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
#define pll pair < long , long >
#define vll vector < pll >
#define ml map < long , long >
#define mll map < pll , long >
#define x first
#define y second
#define vl vector < long >
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
long n,a[100005],i,ans=1e9,cur,d,f,j;
int main()
{
    io
    vl v;
    cin>>n;
    for(i=0;i<n;++i)
    cin>>a[i];
    sort(a,a+n);
    d=a[0];
    while(d)
    {
        v.pb(d);
        d/=2;
    }
    d=a[0];
    while(d*2<=1e6)
    {
        d*=2;
        v.pb(d);
    }
    sort(v.begin(),v.end());
    for(i=0;i<v.size();++i)
    {
        cur=v[i];
        if(cur>1e6) break;
        long cnt=0;
        f=1;
        for(j=0;j<n;++j)
        {
            d=a[j];
            if(d<cur)
            {
                while(d<cur)
                d*=2,cnt++;
                if(d!=cur) f=0;
            }
            else
            {
                while(d>cur)
                d/=2,cnt++;
                if(d!=cur) f=0;
            }
            if(!f) break;
        }
        if(f) ans=min(ans,cnt);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define long long long
#define pll pair < long , long >
#define vll vector < pll >
#define ml map < long , long >
#define mll map < pll , long >
#define x first
#define y second
#define vl vector < long >
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int n,a[100005],i,ans=1e9,cur,d,f,j;
int main()
{
    io
    vl v;
    cin>>n;
    for(i=0;i<n;++i)
    cin>>a[i];
    sort(a,a+n);
    d=a[0];
    while(d>0)
    {
        v.pb(d);
        d/=2;
    }
    d=a[0];
    while(d<=2e5)
    {
        d*=2;
        v.pb(d);
    }
    for(i=0;i<v.size();++i)
    {
        cur=v[i];
        if(cur>2e5) break;
        int cnt=0;
        f=1;
        for(j=0;j<n;++j)
        {
            d=a[j];
            if(d<cur)
            {
                while(d<cur)
                d*=2,cnt++;
                if(d!=cur) f=0;
            }
            else
            {
                while(d>cur)
                d/=2,cnt++;
                if(d!=cur) f=0;
            }
            if(!f) break;
        }
        if(f) ans=min(ans,cnt);
        cur*=2;
    }
    cout<<ans;
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,x,y,z;
    int arr[13];
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      cnt=0; z=1;
      for(i=2; i<n; i++)
      {
    	if(n%i==0){
    		arr[cnt]=i;
    		cnt++;
    		n/=i;
		}
	  }
	  if(n!=1){
	  	arr[cnt]=n;
	  	cnt++;
	  }
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			x=arr[0];
			y=arr[1];
			for(i=2; i<cnt; i++)
			{
				z=z*arr[i];
			}
			cout << x << " " << y << " " << z << "\n";
		}
    }
    return 0;
}


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

bool PrimeNumber(int n)
{
	if(n<2) return false;
	if(n>2){
		if(n%2==0) return false;
		for(int i=3; i<=sqrt(n); i+=2)
		{
			if(n%i==0) return false;
		}
	}
	return true;
}

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    int arr[3];
    cin >> t;
    for(k=1; k<=t; k++)
    {
    	cin >> n;
    	cnt=0; i=2;
    	while(PrimeNumber(n)==false && cnt<3)
    	{
    		if(n%i==0){
    			arr[cnt]=i;
    			n/=i;
    			cnt++;
			}
			if(cnt==2){
				arr[2]=n;
				if(arr[2]<=arr[1]) n=2; //n la snt -> out
				else cnt=3;
			}
			i++;
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      int arr[3]={1,1,1};
      check=1; i=2; cnt=0;
		while(check!=0 && cnt<3){
			if(n%i==0){
				arr[cnt]*=i;
				if(cnt<3) cnt++;
			}
			i++;
			if(cnt==2){
				arr[2]=n/(arr[0]*arr[1]);
				if(arr[2]>arr[1]) cnt++;
				else check=0;
			}
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

bool PrimeNumber(int n)
{
	if(n<2) return false;
	if(n>2){
		if(n%2==0) return false;
		for(int i=3; i<=sqrt(n); i+=2)
		{
			if(n%i==0) return false;
		}
	}
	return true;
}

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    int arr[3];
    cin >> t;
    for(k=1; k<=t; k++)
    {
    	cin >> n;
    	cnt=0; i=2;
    	while(PrimeNumber(n)==false && cnt<3)
    	{
    		if(n%i==0){
    			arr[cnt]=i;
    			n/=i;
    			cnt++;
			}
			if(cnt==2){
				arr[2]=n;
				if(arr[2]<arr[1]) n=2; //n la snt -> out
				else cnt=3;
			}
			i++;
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    int arr[3];
    cin >> t;
    for(k=1; k<=t; k++)
    {
    	cin >> n;
    	cnt=0;
    	for(i=2; i<=sqrt(n); i++)
    	{
    		if(n%i==0){
    			n/=i;
    			arr[cnt]=i;
    			cnt++;
			}
			if(cnt==2){
				arr[2]=n;
				if(arr[2]>arr[1]) cnt++;
				else break;
			}
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,x,y,z;
    int arr[13];
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      cnt=0; z=1;
      for(i=2; i<=n; i++)
      {
    	if(n%i==0){
    		arr[cnt]=i;
    		cnt++;
    		n/=i;
		}
	  }
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			x=arr[0];
			y=arr[1];
			for(i=2; i<cnt; i++)
			{
				z=z*arr[i];
			}
			cout << x << " " << y << " " << z << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,x,y,z;
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      int arr[3]={1,1,1};
      cnt=0;
      for(i=2; i<=n; i++)
      {
    	if(n%i==0){
    		n/=i;
    		arr[cnt]*=i;
    		if(cnt<3) cnt++;
		}
	  }
	  arr[cnt-1]*=n;
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      int arr[3]={1,1,1};
      check=1; i=2; cnt=0;
		while(check!=0 && cnt<3){
			if(n%i==0){
				arr[cnt]*=i;
				if(cnt<3) cnt++;
			}
			i++;
			if(i>n) check=0;
			if(cnt==2){
				arr[2]=n/(arr[0]*arr[1]);
				if(arr[2]>arr[1] && arr[0]*arr[1]*arr[2]==n) cnt++;
				else check=0;
			}
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    int arr[3];
    cin >> t;
    for(k=1; k<=t; k++)
    {
    	cin >> n;
    	cnt=0;
    	for(i=2; i<=sqrt(n); i++)
    	{
    		if(n%i==0){
    			n/=i;
    			arr[cnt]=i;
    			cnt++;
			}
			if(cnt==2){
				arr[2]=n;
				if(arr[2]>arr[1]){
					cnt++;
				}
				break;
			}
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

int main()
{
    int t,k,a,b,c,n,i,cnt,check;
    cin >> t;
    for(k=1; k<=t; k++)
    {
      cin >> n;
      int arr[3]={1,1,1};
      check=1; i=2; cnt=0;
		while(cnt<3){
			if(n%i==0){
				arr[cnt]*=i;
				n/=i;
				if(cnt<3) cnt++;
			}
			i++;
			if(i>n && cnt<2) break;
			if(cnt==2){
				arr[2]=n;
				if(arr[2]>arr[1]) cnt++;
				else break;
			}
		}
		if(cnt<3) cout << "NO\n";
		else{
			cout << "YES\n";
			cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
		}
    }
    return 0;
}


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    ll n;
    int a=2,b=2,c=2,cnt=0,e=0;
    cin>>n;

    ll m=n;
set<int> s;

    for(int i=2;i<=sqrt(m);i++){

        if(n%i==0 &&e==1){

                cnt++;
         if (!s.count(i)){
            s.insert(i);
            }
                if(i!=a){b=i;break;}

            n=n/i;
            i--;
        }
        else if(n%i==0 && e==0){
                cnt++;
                n=n/i;
            a=i;
        s.insert(i);
        e=1;
        i--;
        }
        if(cnt>6){break;}
        if(n==1){break;}

    }
        if(s.size()==1){
            b=a*a;
            c=(m/a)/b;
        }
        else {
            c=m/(a*b);
        }
        if(a!=b&&b!=c&&c!=a && a*b*c==m){
                    cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
       }
       else cout<<"NO"<<endl;

}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    ll n;
    cin>>n;
    ll m=n;
    int a=1,b=1,c=1,d=0,cnt=0,e=0;

    for(int i=2;i<=sqrt(m);i++){

        if(n%i==0){
            cnt++;
            if(e==0){a=i;e++;d++;}
            else if(i!=a){b=i;d++;}
            n=n/i;

            i--;
        }
        if(cnt==6){break;}

    }
    if(cnt<2){cout<<"NO"<<endl;}
    else{if(d==1&&cnt>=6){
        b=a*a;
        c=m/(a*b);

    }
    if(d>=2;cnt>=2){
            c=m/(a*b);

    }

    if(a!=c && c!=b ){
            if(b>1&&c>1)cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
            else{cout<<"NO"<<endl;}
    }
    else{cout<<"NO"<<endl;}

}
}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    int n,a,b,c=1,cnt=0,e=0;
    cin>>n;

    int m=n;
set<int> s;

    for(int i=2;i<m;i++){

        if(n%i==0 &&e==1){
            cnt++;
            if (!s.count(i)){
            s.insert(i);
            }
            n=n/i;
            i--;
        }
        else if(n%i==0 && e==0){
                cnt++;
                n=n/i;
            a=i;
        s.insert(i);
        e=1;
        i--;

        }

        if(n==1){b=i+1;break;}

    }

    if(s.size()+cnt<6){cout<<"NO"<<endl;}
    else{
        if(s.size()==1){
                if(cnt<6){cout<<"NO"<<endl; return;}
                else{
            b=a*a;

            c=(m/a)/b;
        }
        }

        else {
            c=m/(a*b);

        }

        cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;

    }
}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    int n,a,b,c=1,cnt=0,k;
    cin>>n;
    vector<int> v;
    int m=n;
set<int> s;

    for(int i=2;i<m;i++){

        if(n%i==0){

            v.push_back(i);
            s.insert(i);
            n=n/i;
            i--;

        }
        if(n/i==0){break;}

    }

    if(s.size()+v.size()<6){cout<<"NO"<<endl;}
    else{
        a=v[0];
        if(s.size()==1){
                if(v.size()<6){cout<<"NO"<<endl; return;}
                else{
            b=v[1]*v[2];

            c=(m/a)/b;
        }
        }

        else {
            b=v[v.size()-1];

            c=m/(a*b);

        }

        cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;

    }
}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    int n,a,b,c=1,cnt=0,k;
    cin>>n;
    vector<int> v;
    int m=n;
set<int> s;

    for(int i=2;i<sqrt(m);i++){

        while(n%i!=0){

            v.push_back(i);
            s.insert(i);
            n=n/i;

        }
        if(n/i==0){break;}

    }

    if(s.size()+v.size()<6){cout<<"NO"<<endl;}
    else{
        a=v[0];
        if(s.size()==1){
                if(v.size()<6){cout<<"NO"<<endl; return;}
                else{
            b=v[1]*v[2];

            c=(n/a)/b;
        }
        }

        else {
            b=v[v.size()-1];

            c=(v[i]/a)/b;

        }

        cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
    }

}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    ll n;
    int a,b,c=1,cnt=0,e=0;
    cin>>n;

    ll m=n;
set<int> s;

    for(int i=2;i<=sqrt(m);i++){

        if(n%i==0 &&e==1){

                cnt++;
         if (!s.count(i)){
            s.insert(i);
            }
                if(i!=a){b=i;break;}

            n=n/i;
            i--;
        }
        else if(n%i==0 && e==0){
                cnt++;
                n=n/i;
            a=i;
        s.insert(i);
        e=1;
        i--;
        }
        if(cnt>6){break;}
        if(n==1){break;}

    }
        if(s.size()==1){
            b=a*a;
            c=(m/a)/b;
        }
        else {
            c=m/(a*b);
        }
        if(a!=b&&b!=c&&c!=a && a*b*c==m){
                    cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
       }
       else cout<<"NO"<<endl;

}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    ll n;
    cin>>n;
    ll m=n;
    int a=1,b=1,c=1,d=0,cnt=0,e=0;

    for(int i=2;i<=sqrt(m);i++){

        if(n%i==0){
            cnt++;
            if(e==0){a=i;e++;d++;}
            else if(i!=a){b=i;d++; }
            n=n/i;

        }
        if(d==2){break;}

    }
    if(cnt<2){cout<<"NO"<<endl;}
    else{
    if(d>=2;cnt>=2){
            c=m/(a*b);

    }

    if(a!=c && c!=b ){
            if(b>1&&c>1)cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
            else{cout<<"NO"<<endl;}
    }
    else{cout<<"NO"<<endl;}

}
}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }


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

typedef long long ll;
#define pb  push_back
#define M_PIl          3.141592653589793238462643383279502884L /* pi */

void solve(){
    int n,a,b,c=1,cnt=0,e=0;
    cin>>n;

    int m=n;
set<int> s;

    for(int i=2;i<=m/2;i++){

        if(n%i==0 &&e==1){
            cnt++;
            if (!s.count(i)){
            s.insert(i);
            }
            n=n/i;
            i--;
        }
        else if(n%i==0 && e==0){
                cnt++;
                n=n/i;
            a=i;
        s.insert(i);
        e=1;
        i--;

        }

        if(n==1){b=i+1;break;}

    }

    if(s.size()+cnt<6){cout<<"NO"<<endl;}
    else{
        if(s.size()==1){
                if(cnt<6){cout<<"NO"<<endl; return;}
                else{
            b=a*a;

            c=(m/a)/b;
        }
        }

        else {
            c=m/(a*b);

        }

        cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;

    }
}

    int main(){
        int t;
        cin>>t;
        while(t--){
            solve();
        }

    return 0;
    }