#include<stdio.h>
int n,m;
int a[100100],b[100100],c[100100];
int check(int k);
int main()
{
	int i,mi=0,mid,f,e,ans=-1;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=m;i++)
	{
		scanf("%d",&b[i]);
		c[i]=0;
	}
	f=1;e=n+1;
	mid=(f+e)/2;
	while(mi!=mid)
	{
		if(check(mid))
		{
			e=mid;
			ans=mid;
		}
		else
			f=mid;
		mi=mid;
		mid=(f+e)/2;
	}
	printf("%d",ans);
	return 0;
}

int check(int k)
{
	int i,s=0,j;
	for(i=k;i>0;i--)
	{
		if(c[a[i]]||!a[i])
		{
			if(s)
				s--;
		}
		else
		{
			c[a[i]]=1;
			s+=b[a[i]];
		}
	}
	for(i=1;i<=m;i++)
	{
		if(!c[i])
			s=1;
		c[i]=0;
	}

	if(s>0)
		return 0;
	else
		return 1;
}


#include <vector>
#include <cstdio>
using namespace std;
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //< about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*logn*n)
    int cnt_first = 0;
    int cnt;
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all, we should shift all.
    //at least we can shift all back until one recover.
    //x = that one + 0...0 otherwise x can't shift to it.
    //
    //just try x, then increasing x until x >all
    for(int x = 0; x <= 2*max_a; x++){
        cnt = 0;
        for(int i = 0; i < n; i++){
            int y;
            if(a[i] >= x){
                for(y = a[i]; y > x; y/=2){
                    cnt ++;
                }
            }else{
                //y <= 2*(max_a - 1)
                for(y = a[i]; y < x; y*=2){
                    cnt ++;
                }
            }
            if(y != x){
                break;
            }
            if(i == n - 1){
                if(ans == -1){
                    ans = cnt;
                }else{
                    ans = min(ans, cnt);
                }
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


/*! \brief brute-force, slow
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //< about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    int cnt;
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //O(n*logn*(max_a + 1))
    //if x > all >=1, must *2 each,
    //at least we can shift back (-n operation) to better solution: x/2
    //until x/2 <= all. So best solution must be in [0, max_a]
    for(int x = 0; x <= max_a; x++){
        cnt = 0;
        for(int i = 0; i < n; i++){
            int y;
            if(a[i] >= x){
                for(y = a[i]; y > x; y >>= 1){
                    cnt ++;
                }
                if(y < x){
                    //1. same width y < x => impossible
                    //2. width - 1 y < x, if same width y > x. it is possilbe
                    //to remove some 1 to decrease y and shift back.
                    //to be simple, do not check 1 and assume 2. Do not check
                    //the first right 1s after low_x. Just try all.
                    int shifts = 1;
                    y <<= shifts;
                    while(y != x){
                        if(y == 0){
                            break;
                        }
                        shifts ++;
                        y >>= shifts;
                        y <<= shifts;
                    }
                    cnt += 2*shifts;
                }
            }else{
                //y <= 2*(max_a - 1)
                for(y = a[i]; y < x; y <<= 1){
                    cnt ++;
                }
                if(y > x){
                    //1. same width: we can try to remove low 1s.
                    //2. width + 1 => width y < x and exists:
                    //we can't add 1s, impossible
                    //to be simple, assume 1 and try to remove some 1s.
                    int shifts = 0;
                    while(y != x){
                        if(y == 0){
                            break;
                        }
                        shifts ++;
                        y >>= shifts;
                        y <<= shifts;
                    }
                    cnt += 2*shifts;
                }
            }
            if(y != x){
                break;
            }
            if(i == n - 1){
                if(ans == -1){
                    ans = cnt;
                }else{
                    ans = min(ans, cnt);
                }
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


#include <vector>
#include <cstdio>
using namespace std;
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //< about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*logn*n)
    int cnt_first = 0;
    int cnt;
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all, at least we can shift all until one does not shift,
    //that one == x. So x <= maximum.
    for(int x = 0; x <= max_a; x++){
        cnt = 0;
        for(int i = 0; i < n; i++){
            int y;
            if(a[i] >= x){
                for(y = a[i]; y > x; y/=2){
                    cnt ++;
                }
            }else{
                //y <= 2*(max_a - 1)
                for(y = a[i]; y < x; y*=2){
                    cnt ++;
                }
            }
            if(y != x){
                break;
            }
            if(i == n - 1){
                if(ans == -1){
                    ans = cnt;
                }else{
                    ans = min(ans, cnt);
                }
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int check(const vector<int> &a, int x)
{
    int ans = -1;
    int cnt = 0;
    int n = a.size();
    for(int i = 0; i < n; i++){
        int y;
        if(a[i] >= x){
            for(y = a[i]; y > x; y/=2){
                cnt ++;
            }
        }else{
            //y <= 2*(max_a - 1)
            for(y = a[i]; y < x; y*=2){
                cnt ++;
            }
        }
        if(y != x){
            return -1;
        }
        if(i == n - 1){
            if(ans == -1){
                ans = cnt;
            }else{
                ans = min(ans, cnt);
            }
        }
    }
    return ans;
}
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //at least 0 work, so ans < about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*logn*n)
    int cnt_first = 0;
    int cnt;
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all, we should shift all.
    //at least we can shift all back until one recover.
    //x = that one + 0...0 otherwise x can't shift to it.
    //
    //if x no solution, 2*x will not be solution because we can always
    //do right shifts to get smaller solution.
    //
    //just try x, if x have solution,
    //then increasing x until x >all and have solution or no solution
    //
    //
    for(int x = 0; x <= 2000000; x++){
        int cnt = check(a, x);
        if(cnt != -1){
            if(ans == -1){
                ans = cnt;
            }else{
                ans = min(ans, cnt);
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


/*! \file C.cpp
 *  \brief Fix corner case: shift right then shift left to remove some
 *  trailing 1s.
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
/* x > 0 */
void get_range(int x, int &low, int &high)
{
    /* 2**17 > 1e5 >= max_a */
    for(int bit = 0; bit <= 16; bit ++){
        if(x & (1 << bit)){
            low = bit;
            break;
        }
    }
    for(int bit = 16; bit >= 0; bit --){
        if(x & (1 << bit)){
            high = bit;
            break;
        }
    }
}
int check(const vector<int> &a, int x)
{
    int cnt = 0;
    int n = a.size();
    int high_x, low_x, wx, high_y, low_y, wy;
    if(x > 0){
        get_range(x, low_x, high_x);
        wx = high_x - low_x + 1;
    }else{
        wx = 0;
    }
    for(int i = 0; i < n; i++){
        //y <= 2*max_a <= 2e5
        int y = a[i];
        //y >= 1 > 0
        get_range(y, low_y, high_y);
        wy = high_y - low_y + 1;
        int diffs = wy - wx;
        if(diffs < 0){
            //we can't add 1
            return -1;
        }
        int shifts;
        if(diffs > 0){
            //remove trailing 1s with bit in [low_y, low_y + diffs - 1]
            shifts = low_y + diffs;
            y >>= shifts;
            cnt += shifts;
            high_y -= shifts;
            low_y = 0;
        }
        /* match x */
        if(wx > 0){
            shifts = low_y - low_x;
            if(shifts >= 0){
                y >>= shifts;
                cnt += shifts;
            }else{
                shifts = -shifts;
                y <<= shifts;
                cnt += shifts;
            }
        }
        if(y != x){
            return -1;
        }
        if(i == n - 1){
            return cnt;
        }
    }
    return -1;
}
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //at least 0 work, so ans < about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*n)
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all >= 1 and has solution, we should make all *2.
    //we can shift-back to avoid n operations. So there is better
    //smaller solution (x > 1). If there is no solution, x > all
    //should be ignored too.
    //
    for(int x = 0; x <= max_a; x++){
        int cnt = check(a, x);
        if(cnt != -1){
            if(ans == -1){
                ans = cnt;
            }else{
                ans = min(ans, cnt);
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


/*! \file C.cpp
 *  \brief Fix corner case: shift right then shift left to remove some
 *  trailing 1s.
 *  Fix corner case: shift the first 1s after high_y - wx - 1, do not
 *  shift prefix 0s.
 *  Set high_x for wx == 0 to match the match-shift.
 *
 *  Only for test. Should TLE.
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
/* x > 0 */
void get_range(int x, int &low, int &high)
{
    /* 2**17 > 1e5 >= max_a */
    for(int bit = 0; bit <= 16; bit ++){
        if(x & (1 << bit)){
            low = bit;
            break;
        }
    }
    for(int bit = 16; bit >= 0; bit --){
        if(x & (1 << bit)){
            high = bit;
            break;
        }
    }
}
int check(const vector<int> &a, int x)
{
    int cnt = 0;
    int n = a.size();
    int high_x, low_x, wx, high_y, low_y, wy;
    if(x > 0){
        get_range(x, low_x, high_x);
        wx = high_x - low_x + 1;
    }else{
        //x = 1 is better than 0, we can ignored it
        wx = 0;
        //shift out high_y to -1
        high_x = -1;
    }
    for(int i = 0; i < n; i++){
        //y <= max_a <= 1e5
        int y = a[i];
        //y >= 1 > 0
        get_range(y, low_y, high_y);
        wy = high_y - low_y + 1;
        if(wy < wx){
            //we can't add 1
            return -1;
        }
        //wy >= wx,
        //remove all 1s(not prefix 0s before all 1s out of wx)
        //if wx == 0, remove all 1s.
        for(int bit = high_y - wx; bit >= 0; bit --){
            if((1 << bit) & y){
                //shift the first 1 from bit bit to bit 0, then shift out
                y >>= (bit + 1);
                cnt += (bit + 1);
                low_y -= bit + 1;
                high_y -= bit + 1;
                break;
            }
        }
        //high_x == -1 and high_y == -1 if wx == 0
        if(high_y > high_x){
            //shift right
            y >>= (high_y - high_x);
            cnt += high_y - high_x;
        }else if(high_y < high_x){
            //shift left
            y <<= (high_x - high_y);
            cnt += high_x - high_y;
        }
        if(y != x){
            return -1;
        }
        if(x == 48){
            //printf("ai %d cnt %d\n",a[i], cnt);
        }
        if(i == n - 1){
            if(cnt < 0){
                printf("cnt %d x%d\n", cnt,x);
            }
            return cnt;
        }
    }
    return -1;
}
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //at least 0 work, so ans < about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*n)
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all >= 1 and has solution, we should make all *2.
    //we can shift-back to avoid n operations. So there is better
    //smaller solution (x > 1). If there is no solution, x > all
    //should be ignored too.
    //
    for(int x = 0; x <= max_a; x++){
        int cnt = check(a, x);
        if(cnt != -1){
            if(ans == -1){
                ans = cnt;
            }else{
                ans = min(ans, cnt);
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


/*! \file C.cpp
 *  \brief Record the count of ai to reach x and the sum of shortest
 * operations of all ai reach x. Build the shortest path carefully.
 *  Do not count 0 twice.
 *  Merge n == 1.
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //[0, n]
    vector<int>cnt(max_a + 1, 0);
    //ops[x] <= b*n
    //m[x] = int(log(x, 2)) + 1, m[x] <= m = int(log(max_a, 2)) + 1 <= 17
    //b = max(m[x] for 0, <=2*(m[x] - 1) for 2^m[x]-1)_x = max(m, 2*(m - 1))
    //  < 2*m
    //so ops[x] < 2*m*n <= 34*1e5 = 3.4e6
    vector<int>ops(max_a + 1, 0);
    //at least we can /2 to reach 0
    //< about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(n*m^2)
    //O(m^2): sum(lshifts_max + 1) lshifts_max + 1 may be 1 if x&1 == 0
    //if x > all >=1, must *2 each,
    //at least we can shift back (-n operation) to better solution: x/2
    //until x/2 <= all. So best solution must be in [0, max_a]
    //update the ops of the values ai can reach
    for(int i = 0; i < n; i++){
        int r_shifts = 0;
        //right-shift, then check all left-shifts
        int x = a[i];
        while(1){
            //x > 0, 2^17 > 1e5 >= max_a, valid l_shifts <= 16 = int(log(1e5,2))
            for(int l_shifts = 0, y = x; y <= max_a; l_shifts ++, y <<= 1){
                ops[y] += r_shifts + l_shifts;
                cnt[y] ++;
            }
            //x10...0 => x1 (>0)
            while((x & 1) == 0){
                x >>= 1;
                r_shifts ++;
                ops[x] += r_shifts;
                cnt[x] ++;
            }
            x >>= 1;
            r_shifts ++;
            if(x == 0){
                //x become 0 here and only here
                ops[x] += r_shifts;
                cnt[x] ++;
                break;
            }
        }
    }
    for(int x = 0; x <= max_a; x++){
        if(cnt[x] == n){
            if(ans == -1){
                ans = ops[x];
            }else{
                ans = min(ans, ops[x]);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


/*! \file C.cpp
 *  \brief Fix corner case: shift right then shift left to remove some
 *  trailing 1s.
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
/* x > 0 */
void get_range(int x, int &low, int &high)
{
    /* 2**17 > 1e5 >= max_a */
    for(int bit = 0; bit <= 16; bit ++){
        if(x & (1 << bit)){
            low = bit;
            break;
        }
    }
    for(int bit = 16; bit >= 0; bit --){
        if(x & (1 << bit)){
            high = bit;
            break;
        }
    }
}
int check(const vector<int> &a, int x)
{
    int cnt = 0;
    int n = a.size();
    int high_x, low_x, wx, high_y, low_y, wy;
    if(x > 0){
        get_range(x, low_x, high_x);
        wx = high_x - low_x + 1;
    }else{
        wx = 0;
    }
    for(int i = 0; i < n; i++){
        //y <= 2*max_a <= 2e5
        int y = a[i];
        //y >= 1 > 0
        get_range(y, low_y, high_y);
        wy = high_y - low_y + 1;
        int diffs = wy - wx;
        if(diffs < 0){
            //we can't add 1
            continue;
        }
        int shifts;
        if(diffs > 0){
            //remove trailing 1s with bit in [low_y, low_y + diffs - 1]
            shifts = low_y + diffs;
            y >>= shifts;
            cnt += shifts;
            high_y -= shifts;
            low_y = 0;
        }
        /* match x */
        if(wx > 0){
            shifts = low_y - low_x;
            if(shifts >= 0){
                y >>= shifts;
                cnt += shifts;
            }else{
                shifts = -shifts;
                y <<= shifts;
                cnt += shifts;
            }
        }
        if(y != x){
            return -1;
        }
        if(i == n - 1){
            return cnt;
        }
    }
    return -1;
}
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //at least 0 work, so ans < about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    //O(logn*n)
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //if x > all >= 1 and has solution, we should make all *2.
    //we can shift-back to avoid n operations. So there is better
    //smaller solution (x > 1). If there is no solution, x > all
    //should be ignored too.
    //
    for(int x = 0; x <= max_a; x++){
        int cnt = check(a, x);
        if(cnt != -1){
            if(ans == -1){
                ans = cnt;
            }else{
                ans = min(ans, cnt);
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


/*! \file C.cpp
 *  \brief Record the count of ai to reach x and the sum of shortest
 * operations of all ai reach x. Build the shortest path carefully.
 */
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
    //[1,1e5]
    int n;
    scanf("%d", &n);
    //ai[1,1e5]
    vector<int>a(n);
    int max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        max_a = max(max_a, a[i]);
    }
    //[0, n]
    vector<int>cnt(max_a + 1, 0);
    //~< log(max_a)*n
    vector<int>ops(max_a + 1, 0);
    //at least we can /2 to reach 0
    //< about nlogn ~ 1e5*log(1e5) < 2e6
    int ans = -1;
    if(n == 1){
        printf("%d\n", 0);
        return 0;
    }
    //O(n*m^2) m^2: sum(lshifts_max + 1) lshifts_max + 1 may be 1 if x&1 == 0
    //if x > all >=1, must *2 each,
    //at least we can shift back (-n operation) to better solution: x/2
    //until x/2 <= all. So best solution must be in [0, max_a]
    //update the ops of the values ai can reach
    for(int i = 0; i < n; i++){
        int r_shifts = 0;
        //right-shift, then check all left-shifts
        int x = a[i];
        while(1){
            for(int l_shifts = 0; l_shifts <= 16; l_shifts ++){
                int y = x << l_shifts;
                if(y > max_a){
                    break;
                }
                ops[y] += r_shifts + l_shifts;
                cnt[y] ++;
                //x == 0
                if(y == 0){
                    break;
                }
            }
            while((x & 1) == 0){
                x >>= 1;
                r_shifts ++;
                ops[x] += r_shifts;
                cnt[x] ++;
                if(x == 0){
                    break;
                }
            }
            if(x == 0){
                break;
            }
            x >>= 1;
            r_shifts ++;
        }
    }
    for(int x = 0; x <= max_a; x++){
        if(cnt[x] == n){
            if(ans == -1){
                ans = ops[x];
            }else{
                ans = min(ans, ops[x]);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = 999900009;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

}


#include<bits/stdc++.h>

using namespace std;

vector<int> cv;
int steps[100010];
int cnt[100010];
int visited[100010];
int MAX = -1;

inline void bfs(int n, int c) {
    queue<pair<int, int> > Q;
    Q.push(make_pair(n, 0));

    while(!Q.empty()) {
        pair<int, int> cur = Q.front();
        //cout<<cur.first<< " "<<cur.second<<endl;
        Q.pop();
        visited[cur.first] = c;
        cnt[cur.first]++;
        steps[cur.first] += cur.second;

        int a1 = cur.first * 2;
        int a2 = cur.first / 2;

        if(visited[a1] != c && a1 <= MAX)
            Q.push(make_pair(a1, cur.second + 1));

        if(visited[a2] != c)
            Q.push(make_pair(a2, cur.second + 1));
    }
}

int main() {
    int n;
    cin >> n;
    while(n--) {
        int a;
        cin >> a;
        MAX = max(MAX, a);
        cv.push_back(a);
    }

    for(int i = 0; i < cv.size(); ++i) {
        bfs(cv[i], i + 1);
    }

    int MIN = numeric_limits<int>::max();

    for(int i = 1; i <= MAX; ++i) {
        //cout<<i<<" "<<cnt[i]<<endl;
        if(cnt[i] == cv.size()) {
            MIN = min(MIN, steps[i]);
        }
    }

    cout << MIN << endl;

    return 0;
}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;
    }


#include<iostream>
#include<limits>
#include<queue>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        bool vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == true) continue;

            vis[ele] = true;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<100005;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                //cout<<i<<endl;
                //cout<<cnt[i]<<endl;
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

    return 0;

}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = 999900009;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

    return 0;

}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];
int vis[100005];

int main(){

    int n;
    cin>>n;

    for(int i=0;i<n;++i){

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);
        memset(vis, 0, sizeof(vis));

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<100005;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                //cout<<i<<endl;
                //cout<<cnt[i]<<endl;
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

vector<int> cv;
int steps[100010];
int cnt[100010];
int MAX = -1;

inline void bfs(int n) {
    set<int> done;
    queue<pair<int, int> > Q;
    Q.push(make_pair(n, 0));

    while(!Q.empty()) {
        pair<int, int> cur = Q.front();
        //cout<<cur.first<< " "<<cur.second<<endl;
        Q.pop();
        done.insert(cur.first);
        cnt[cur.first]++;
        steps[cur.first] += cur.second;

        int a1 = cur.first * 2;
        int a2 = cur.first / 2;

        if(done.find(a1) == done.end() && a1 <= MAX)
            Q.push(make_pair(a1, cur.second + 1));

        if(done.find(a2) == done.end())
            Q.push(make_pair(a2, cur.second + 1));
    }
}

int main() {
    int n;
    cin >> n;
    while(n--) {
        int a;
        cin >> a;
        MAX = max(MAX, a);
        cv.push_back(a);
    }

    for(int i = 0; i < cv.size(); ++i) {
        bfs(cv[i]);
    }

    int MIN = numeric_limits<int>::max();

    for(int i = 1; i <= MAX; ++i) {
        //cout<<i<<" "<<cnt[i]<<endl;
        if(cnt[i] == cv.size()) {
            MIN = min(MIN, steps[i]);
        }
    }

    cout << MIN << endl;

    return 0;
}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];
int vis[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));
    memset(vis, 0, sizeof(vis));

    for(int i=0;i<n;++i){

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == i+1) continue;

            vis[ele] = i+1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<100005;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                //cout<<i<<endl;
                //cout<<cnt[i]<<endl;
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

    return 0;

}


#include<iostream>
#include<limits>
#include<queue>
#include <cstring>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

}


#include<bits/stdc++.h>

using namespace std;

vector<int> cv;
int steps[100010];
int cnt[100010];
int MAX = -1;

inline void bfs(int n) {
    map<int, int> done;
    queue<pair<int, int> > Q;
    Q.push(make_pair(n, 0));

    while(!Q.empty()) {
        pair<int, int> cur = Q.front();
        //cout<<cur.first<< " "<<cur.second<<endl;
        Q.pop();
        done[cur.first]++;
        cnt[cur.first]++;
        steps[cur.first] += cur.second;

        int a1 = cur.first * 2;
        int a2 = cur.first / 2;

        if(!done[a1] && a1 <= MAX)
            Q.push(make_pair(a1, cur.second + 1));

        if(!done[a2])
            Q.push(make_pair(a2, cur.second + 1));
    }
}

int main() {
    int n;
    cin >> n;
    while(n--) {
        int a;
        cin >> a;
        MAX = max(MAX, a);
        cv.push_back(a);
    }

    for(int i = 0; i < cv.size(); ++i) {
        bfs(cv[i]);
    }

    int MIN = numeric_limits<int>::max();

    for(int i = 1; i <= MAX; ++i) {
        //cout<<i<<" "<<cnt[i]<<endl;
        if(cnt[i] == cv.size()) {
            MIN = min(MIN, steps[i]);
        }
    }

    cout << MIN << endl;

    return 0;
}


#include<iostream>
#include<limits>
#include<queue>
#include<cstdio>

using namespace std;
#define mp make_pair
#define x first
#define y second
#define MAX numeric_limits<int>::max();

int cnt[100005];
int tot[100005];

int main(){

    int n;
    cin>>n;
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));

    for(int i=0;i<n;++i){
        int vis[100005];
        memset(vis, 0, sizeof(vis));

        int no;
        cin>>no;

        pair<int, int> cur;
        cur=mp(no, 0);
        queue<pair<int, int> >series;
        series.push(cur);

        while(!series.empty()){
            int ele = series.front().x;
            int y = series.front().y;
            series.pop();

            if(ele>100005 || ele<1) continue;
            if(vis[ele] == 1) continue;

            vis[ele] = 1;
            cnt[ele] += y;
            tot[ele]++;

            series.push(mp(ele*2, y+1));
            series.push(mp(ele/2, y+1));

        }

    }
    int res = MAX;
    for(int i=1;i<n;++i){
        if(tot[i] == n){
            if(res > cnt[i]){
                res = cnt[i];
            }
        }

    }

    cout<<res<<endl;

}


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

int gcd(int a, int b){
    if(a==1 || b==1){
        return 1;
    }
    if(a>b) swap(a,b);
    return(gcd((b%a),a));
}

int cont(int k,int a){
    int res=0;
    while(k!=a){
        if(k>a){
            k=k/2;
            res++;
        }
        if(k<a){
            k=k*2;
            res++;
        }
    }
    return res;
}

int main(){
    int n,arr[MAX];
    cin>>n;
    int temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        arr[i]=temp;
    }
    sort(arr,arr+n);
    int a=arr[0];
    int b;
    for(int i=1;i<n;i++){
        b=arr[i];
        if(a%2==1 && b%2==1){
            a=1;
        }else{
            if(b%a==0){
                //cout<<a<<" "<<b<<" "<<n<<endl;
                if(n%2==1){
                    if(i<=n/2) a=b;
                    else a=a;
                }else{
                    if(i<=n/2-1) a=b;
                    else a=a;
                }
            }else{
                int c=gcd(a,b);
                a=a/gcd(a,b);
                b=b/gcd(a,b);
                if(a%2==0 && b%2==1) a=a;
                if(b%2==0 && a%2==1) a=b;
                if(a%2==1 && b%2==1) a=1;
            }
        }
    }
    int myres=0;
    for(int i=0;i<n;i++){
        myres+=cont(arr[i],a);
    }
    cout<<myres<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <fstream>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define FOR(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000
#define MEMSET_INF 127
#define MEMSET_HALF_INF 63
#define zero(arr) memset((arr), 0, sizeof (arr))
#define init(arr) memset((arr), -1, sizeof (arr))
#define LSOne(S) (S & (-S))

//memset(dist, MEMSET_INF, sizeof dist);
//memset(dp_memo, -1, sizeof dp_memo);
//memset(arr, 0, sizeof arr);

class UnionFind {
    private: vi p, rank;
    public:
        UnionFind(int N) {
            rank.assign(N,0);
            p.assign(N,0);
            for (int i=0;i<N;i++) {
                p[i]=i;
            }
        }
        int findSet(int i) {
            return (p[i]==i) ? i : (p[i]=findSet(p[i]));
        }
        bool isSameSet(int i, int j) {
            return findSet(i)==findSet(j);
        }
        void unionSet(int i, int j) {
            if (!isSameSet(i,j)) {
                int x = findSet(i), y = findSet(j);
                if (rank[x] > rank[y])
                    p[y]=x;
                else {
                    p[x]=y;
                    if (rank[x]==rank[y])
                        rank[y]++;
                }
            }
        }
};

class SegmentTree {
private:
  vi st, A;
  int n;
  int left (int p) { return p << 1; }
  int right(int p) { return (p << 1) + 1; }

  void build(int p, int L, int R) {
    if (L == R)
      st[p] = L;
    else {
      build(left(p) , L              , (L + R) / 2);
      build(right(p), (L + R) / 2 + 1, R          );
      int p1 = st[left(p)], p2 = st[right(p)];
      st[p] = (A[p1] <= A[p2]) ? p1 : p2;
  } }

  int rmq(int p, int L, int R, int i, int j) {
    if (i >  R || j <  L) return -1;
    if (L >= i && R <= j) return st[p];

    int p1 = rmq(left(p) , L              , (L+R) / 2, i, j);
    int p2 = rmq(right(p), (L+R) / 2 + 1, R          , i, j);

    if (p1 == -1) return p2;
    if (p2 == -1) return p1;
    return (A[p1] <= A[p2]) ? p1 : p2; }

  int update_point(int p, int L, int R, int idx, int new_value) {
    int i = idx, j = idx;

    if (i > R || j < L)
      return st[p];

    if (L == i && R == j) {
      A[i] = new_value;
      return st[p] = L;
    }

    int p1, p2;
    p1 = update_point(left(p) , L              , (L + R) / 2, idx, new_value);
    p2 = update_point(right(p), (L + R) / 2 + 1, R          , idx, new_value);

    return st[p] = (A[p1] <= A[p2]) ? p1 : p2;
  }

public:
  SegmentTree(const vi &_A) {
    A = _A; n = (int)A.size();
    st.assign(4 * n, 0);
    build(1, 0, n - 1);
  }

  int rmq(int i, int j) { return rmq(1, 0, n - 1, i, j); }

  int update_point(int idx, int new_value) {
    return update_point(1, 0, n - 1, idx, new_value); }
};

class FenwickTree {
    private:
        vi ft;
    public:
        FenwickTree(int n) {
            ft.assign(n+1, 0);
        }
        int rsq(int b) {
            int sum = 0;
            for (; b; b -= LSOne(b))
                sum += ft[b];
            return sum;
        }
        void adjust(int k, int v) {
            for (; k < (int)ft.size(); k += LSOne(k)) {
                ft[k] += v;
            }
        }
};

int cnt[100002], vis[100003], stp[100003];

int main()
{
    int n;
    int ans = INF;
    int a,b;
    scanf("%d",&n);
    zero(stp);
    zero(cnt);
    FOR (i, 1, n)
    {
        scanf("%d",&a);
        queue<ii> q;
        q.push(ii(a,0));
        while (!q.empty())
        {
            a = q.front().first;
            b = q.front().second;
            q.pop();
            if (a>100000)
                continue;
            if (vis[a]==i)
                continue;
            vis[a]=i;
            stp[a]+=b;
            cnt[a]++;
            q.push(ii(2*a,b+1));
            q.push(ii(a/2,b+1));
        }
    }
    FOR (i,1, 100000)
    {
        if (cnt[i]==n)
            ans = min(ans, stp[i]);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<long long, long long> l[HH]; // count, dis
pair<long long, long long> r[HH]; // count, dis

pair<long long, long long> cur_l; // count, dis
pair<long long, long long> cur_r;
pair<long long, long long> parent;

long long ans;

// total n
// total dis to cur
pair<long long, long long> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<long long, long long> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<long long, long long> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<long long, long long> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second);
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl

	pair<long long, long long> copyparent = parent;
	pair<long long, long long> copycurl = cur_l;
	pair<long long, long long> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc])
			cur_l.first--;

		if (rc * 2 + 1 < mmm)
			cur_r = r[rc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include<cstdio>
#include<cstring>
using namespace std;

int n,a[100000],mm,cnt[100001],pos[100000]; bool b[100001];

int main(){
    scanf("%d",&n); mm=0;
    for(int i=0; i<n; i++){scanf("%d",&a[i]); mm=(mm<a[i]?a[i]:mm);}
    for(int i=0; i<n; i++){
        int x=1; memset(b,true,sizeof b);
        int now=-1;
        while(a[i]/x>0){
            int y=a[i]/x; now++; x*=2; int now1=now;
            while(y<mm+3){
                if(b[y]){pos[y]++; cnt[y]+=now1; b[y]=false;}
                y*=2; now1++;
            }
        }
    }
	int ans=11111111;
    for(int i=0; i<mm+3; i++)if(pos[i]==n&&ans>cnt[i])ans=cnt[i];
    printf("%d\n",ans);
    return 0;
}


/*
ide : binary tree
dp1 : total operasi yg dibutuhkan buat semua vertex di bawahnya ke dia
dp2 : total operasi yg dibutuhkan buat semua vertex selain dia dan bawahnya ke dia
ditambah cari min
kompleksitas : O(N)
*/
//910492899
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <set>
#include <map>
#include <deque>
#include <vector>
#include <queue>
#include <stack>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MOD7 10000000007
#define INF 2000000000
#define INFLL 9000000000000000000LL
#define EPS 1e-7
#define pi acos(-1)
#define sqr(x) x*x
#define kevin using
#define keren namespace
#define abiez std
#define SYNC ios_base::sync_with_stdio(0)
#define lol ;
#define tulis cout <<
#define ok << endl
#define trus <<" "<<

kevin keren abiez;

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
LL n,a[100005],cnt[100005],anak[100005],maxs;
LL dp1[1000005],dp2[1000005];

LL cari(LL xnow) {
	if (xnow > maxs) return 0;
	if (anak[xnow] != -1) return anak[xnow] + cnt[xnow];
	anak[xnow] = cari(xnow*2) + cari(xnow*2+1);
	return anak[xnow] + cnt[xnow];
}

bool cf(LL a,LL b) {
	return a > b;
}

LL cari1(LL xnow) {
	if (xnow > maxs) return 0;
	if (dp1[xnow] != -1) return dp1[xnow];
	dp1[xnow] = cari1(xnow*2) + cari1(xnow*2+1) + anak[xnow];
	return dp1[xnow];
}

LL cari2(LL xnow) {
	if (xnow > 1) {
		if (xnow % 2 == 1) {
			if (anak[xnow] + cnt[xnow] < n|| dp2[xnow/2] >= INF) {
				dp2[xnow] = INF;
			}else {
				dp2[xnow] = dp1[xnow/2] - dp1[xnow] - anak[xnow] - cnt[xnow] + anak[xnow/2] + cnt[xnow/2] - anak[xnow] - cnt[xnow] + dp2[xnow/2] + n-anak[xnow/2] - cnt[xnow/2];
			}
		}else {
				dp2[xnow] = dp1[xnow/2] - dp1[xnow] - anak[xnow] - cnt[xnow] + anak[xnow/2] + cnt[xnow/2] - anak[xnow] - cnt[xnow] + dp2[xnow/2] + n-anak[xnow/2] - cnt[xnow/2];
		}
	}
	return dp2[xnow];
}
LL ans;
int tmp2;
int main() {
	scanf("%I64d",&n);
	for (int i=0;i<n;i++) {
		scanf("%I64d",&a[i]);
		if (i > 0 && a[i] != a[0]) tmp2 = a[i];
		maxs = max(maxs,a[i]);
		cnt[a[i]]++;
	}
	sort(a,a+n,cf);
	memset(anak,-1,sizeof(anak));
	for (int i=maxs;i>=1;i--) {
		cari(i);
	}
	memset(dp1,-1,sizeof(dp1));
	for (int i=maxs;i>=1;i--) {
		dp1[i] = cari1(i);
	}
	for (int i=1;i<=maxs;i++) {
		dp2[i] = cari2(i);
	}
	ans = INF;
	int tmp = 0;
	for (int i=1;i<=maxs;i++) {
		if (dp1[i]+dp2[i] < ans) {
			ans = min(ans,dp1[i]+dp2[i]);
			tmp = i;
		}
	}
	printf("%lld\n",ans);
}


#include <iostream>
#include <stdio.h>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<long long, long long> l[HH]; // count, dis
pair<long long, long long> r[HH]; // count, dis

pair<long long, long long> cur_l; // count, dis
pair<long long, long long> cur_r;
pair<long long, long long> parent;

long long ans;

// total n
// total dis to cur
pair<long long, long long> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<long long, long long> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<long long, long long> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<long long, long long> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second + (a[lc] ? 1 : 0) + (a[rc] ? 1 : 0));
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl

	pair<long long, long long> copyparent = parent;
	pair<long long, long long> copycurl = cur_l;
	pair<long long, long long> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc])
			cur_l.first--;

		if (rc * 2 + 1 < mmm)
			cur_r = r[rc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int gcd(int a, int b) {
	if(b == 0)
		return a;
	return gcd(b, a % b);
}
int main() {
	int n, map[100010], map2[100010], i, j, cur, ans, mid;
	bool status, local;
	while((scanf("%d", &n)) != EOF) {
		scanf("%d", &map[0]);
		cur = map[0];
		for(i=1;i<n;++i) {
			scanf("%d", &map[i]);
			cur = gcd(cur, map[i]);
		}
		sort(map, map + n);
		status = true;
		for(i=0;i<n;++i) {
			map2[i] = map[i] / cur;
			if(status) {
				local = false;
				for(j=0;j<=17;++j) {
					if(map2[i] == (1 << j))
						local = true;
				}
				if(!local)
					status = false;
			}
		}
		if(!status) {
			ans = 0;
			for(i=0;i<n;++i) {
				while(map[i] > 1) {
					ans++;
					map[i] /= 2;
				}
			}
			printf("%d\n" ,ans);
		}
		else {
			mid = (n-1)/2;
			ans = 0;
			for(i=0;i<mid;++i) {
				while(map2[i] != map2[mid]) {
					map2[i] *= 2;
					++ans;
				}
			}
			for(i=mid+1;i<n;++i) {
				while(map2[i] != map2[mid]) {
					map2[i] /= 2;
					++ans;
				}
			}
			printf("%d\n", ans);
		}

	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<long long, long long> l[HH]; // count, dis
pair<long long, long long> r[HH]; // count, dis

pair<long long, long long> cur_l; // count, dis
pair<long long, long long> cur_r;
pair<long long, long long> parent;

long long ans;

// total n
// total dis to cur
pair<long long, long long> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<long long, long long> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<long long, long long> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<long long, long long> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second + (a[cur] ? 1 : 0));
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl

	pair<long long, long long> copyparent = parent;
	pair<long long, long long> copycurl = cur_l;
	pair<long long, long long> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc])
			cur_l.first--;

		if (rc * 2 + 1 < mmm)
			cur_r = r[rc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	if (lc < mmm) { // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc])
			cur_r.first--;

		if (lc * 2 + 1 < mmm)
			cur_l = l[lc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include <iostream>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<int, int> l[HH]; // count, dis
pair<int, int> r[HH]; // count, dis

pair<int, int> cur_l; // count, dis
pair<int, int> cur_r;
pair<int, int> parent;

int ans;

// total n
// total dis to cur
pair<int, int> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<int, int> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<int, int> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<int, int> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second);
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl;

	pair<int, int> copyparent = parent;
	pair<int, int> copycurl = cur_l;
	pair<int, int> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc]) {
			cur_l.first--;
		}
		cur_r = r[rc * 2 + 1];

			dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	if (lc < mmm) { // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc]) {
			cur_r.first--;
		}
		cur_l = l[lc * 2 + 1];

			dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include <iostream>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<int, int> l[HH]; // count, dis
pair<int, int> r[HH]; // count, dis

pair<int, int> cur_l; // count, dis
pair<int, int> cur_r;
pair<int, int> parent;

int ans;

// total n
// total dis to cur
pair<int, int> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<int, int> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<int, int> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<int, int> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second);
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl;

	pair<int, int> copyparent = parent;
	pair<int, int> copycurl = cur_l;
	pair<int, int> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc]) {
			cur_l.first--;
		}
		if (rc * 2 + 1 < mmm) {
			cur_r = r[rc * 2 + 1];
		}
		else
			make_pair(0, 0);

			dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	if (lc < mmm) { // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc]) {
			cur_r.first--;
		}
		if (lc * 2 + 1 < mmm) {
			cur_l = l[lc * 2 + 1];
		}
		else
			make_pair(0, 0);

			dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include<stdio.h>

int main()
{
	int ar[100000],size,i,max,min;
	int test,operCount=0,anseto1=0;

	scanf("%d", &size);
	for (i=0; i <= size-1; i++)
		scanf("%d", &ar[i]);

	max = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(max<ar[i])
		{
			max = ar[i];
		}
	}

	min = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(min>ar[i])
		{
			min = ar[i];

		}
		else if(min==1)
			{
				anseto1=1;
				break;
			}
	}

	for (i=0; i <= size-1; i++)
	{
		int r = max%ar[i];
		if(r==0)
		{
			test = 1;
		}
		else
		{
			test=0;
			break;
		}
	}
	for(i=0; i <= size-1; i++)
	{
		if(test==1)
		{
			while(ar[i]<max)
			{
				ar[i]=ar[i]*2;
				operCount++;
			}
		}
		if(test==0)
		{
			for(i=0; i <= size-1; i++)
			{
				if(anseto1==1)
				{
					while(ar[i]!=min)
					{
					ar[i]=ar[i]/2;
					operCount++;
					}
				}
				else
				{
					while(ar[i]!=min/2)
					{
					ar[i]=ar[i]/2;
					operCount++;
					}
				}

			}
		}

	}

	printf("%d",operCount);
}


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	int n, map[100010], i, j, k, cur, ans, mini;
	bool status;
	while((scanf("%d", &n)) != EOF) {
		for(i=0;i<n;++i)
			scanf("%d", &map[i]);
		mini = 1000000;
		for(i=0;;++i) {
			if((map[0] >> i) == 0)
				break;
			cur = map[0] >> i;
			ans = i;
			for(j=1;j<n;++j) {
				status = true;
				if(map[j] > cur) {
					for(k=0;;++k) {
						if((map[j] >> k) == 0) {
							status = false;
							break;
						}
						if((map[j] >> k) == cur) {
							ans += k;
							break;
						}
					}
					if(status == false)
						break;
				}
				else {
					for(k=0;;++k) {
						if((map[j] << k) > 100000) {
							status = false;
							break;
						}
						if((map[j] << k) == cur) {
							ans += k;
							break;
						}
					}
					if(status == false)
						break;
				}
			}
			if(j == n) {
				if(ans < mini)
					mini = ans;
			}
		}
		for(i=0;;++i) {
			if((map[0] << i) > 100000)
				break;
			cur = map[0] << i;
			ans = i;
			for(j=1;j<n;++j) {
				status = true;
				if(map[j] > cur) {
					for(k=0;;++k) {
						if((map[j] >> k) == 0) {
							status = false;
							break;
						}
						if((map[j] >> k) == cur) {
							ans += k;
							break;
						}
					}
					if(status == false)
						break;
				}
				else {
					for(k=0;;++k) {
						if((map[j] << k) > 100000) {
							status = false;
							break;
						}
						if((map[j] << k) == cur) {
							ans += k;
							break;
						}
					}
					if(status == false)
						break;
				}
			}
			if(j == n) {
				if(ans < mini)
					mini = ans;
			}
		}
		printf("%d\n", mini);
	}
	return 0;
}


/*
ide : binary tree
dp1 : total operasi yg dibutuhkan buat semua vertex di bawahnya ke dia
dp2 : total operasi yg dibutuhkan buat semua vertex selain dia dan bawahnya ke dia
ditambah cari min
kompleksitas : O(N)
*/
//910492899
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <set>
#include <map>
#include <deque>
#include <vector>
#include <queue>
#include <stack>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MOD7 10000000007
#define INF 2000000000
#define INFLL 9000000000000000000LL
#define EPS 1e-7
#define pi acos(-1)
#define sqr(x) x*x
#define kevin using
#define keren namespace
#define abiez std
#define SYNC ios_base::sync_with_stdio(0)
#define lol ;
#define tulis cout <<
#define ok << endl
#define trus <<" "<<

kevin keren abiez;

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
LL n,a[100005],cnt[100005],anak[100005],maxs;
LL dp1[1000005],dp2[1000005];

LL cari(LL xnow) {
	if (xnow > maxs) return 0;
	if (anak[xnow] != -1) return anak[xnow] + cnt[xnow];
	anak[xnow] = cari(xnow*2) + cari(xnow*2+1);
	return anak[xnow] + cnt[xnow];
}

bool cf(LL a,LL b) {
	return a > b;
}

LL cari1(LL xnow) {
	if (xnow > maxs) return 0;
	if (dp1[xnow] != -1) return dp1[xnow];
	dp1[xnow] = cari1(xnow*2) + cari1(xnow*2+1) + anak[xnow];
	return dp1[xnow];
}

LL cari2(LL xnow) {
	if (xnow > 1) {
		if (xnow % 2 == 1) {
			if (anak[xnow] + cnt[xnow] < n|| dp2[xnow/2] >= INF) {
				dp2[xnow] = INF;
			}else {
				dp2[xnow] = dp1[xnow/2] - dp1[xnow] - anak[xnow] - cnt[xnow] + anak[xnow/2] + cnt[xnow/2] - anak[xnow] - cnt[xnow] + dp2[xnow/2] + n-anak[xnow/2] - cnt[xnow/2];
			}
		}else {
				dp2[xnow] = dp1[xnow/2] - dp1[xnow] - anak[xnow] - cnt[xnow] + anak[xnow/2] + cnt[xnow/2] - anak[xnow] - cnt[xnow] + dp2[xnow/2] + n-anak[xnow/2] - cnt[xnow/2];
		}
	}
	return dp2[xnow];
}
LL ans;
int tmp2;
int main() {
	SYNC;
	cin >> n;
	for (int i=0;i<n;i++) {
		cin >> a[i];
		if (i > 0 && a[i] != a[0]) tmp2 = a[i];
		maxs = max(maxs,a[i]);
		cnt[a[i]]++;
	}
	sort(a,a+n,cf);
	memset(anak,-1,sizeof(anak));
	for (int i=maxs;i>=1;i--) {
		cari(i);
	}
	memset(dp1,-1,sizeof(dp1));
	for (int i=maxs;i>=1;i--) {
		dp1[i] = cari1(i);
	}
	for (int i=1;i<=maxs;i++) {
		dp2[i] = cari2(i);
	}
	ans = INF;
	int tmp = 0;
	for (int i=1;i<=maxs;i++) {
		if (dp1[i]+dp2[i] < ans) {
			ans = min(ans,dp1[i]+dp2[i]);
			tmp = i;
		}
	}
	tulis ans ok lol
}


#include <iostream>
#include <stdio.h>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<int, int> l[HH]; // count, dis
pair<int, int> r[HH]; // count, dis

pair<int, int> cur_l; // count, dis
pair<int, int> cur_r;
pair<int, int> parent;

int ans;

// total n
// total dis to cur
pair<int, int> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<int, int> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<int, int> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<int, int> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second + (a[cur] ? 1 : 0));
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl

	pair<int, int> copyparent = parent;
	pair<int, int> copycurl = cur_l;
	pair<int, int> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	if (lc < mmm) { // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc])
			cur_l.first--;

		if (rc * 2 + 1 < mmm)
			cur_r = r[rc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	if (lc < mmm) { // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc])
			cur_r.first--;

		if (lc * 2 + 1 < mmm)
			cur_l = l[lc * 2 + 1];
		else
			make_pair(0, 0);

		dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include <iostream>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<int, int> l[HH]; // count, dis
pair<int, int> r[HH]; // count, dis

pair<int, int> cur_l; // count, dis
pair<int, int> cur_r;
pair<int, int> parent;

int ans;

// total n
// total dis to cur
pair<int, int> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		pair<int, int> ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		pair<int, int> ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<int, int> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second);
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl;

	pair<int, int> copyparent = parent;
	pair<int, int> copycurl = cur_l;
	pair<int, int> copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	{ // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc]) {
			cur_l.first--;
		}
		cur_r = r[rc * 2 + 1];

		if (lc < mmm)
			dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	{ // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc]) {
			cur_r.first--;
		}
		cur_l = l[lc * 2 + 1];

		if (lc < mmm)
			dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


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

int gcd(int a, int b){
    if(a==1 || b==1){
        return 1;
    }
    if(a>b) swap(a,b);
    if(a==0) return b;
    return(gcd((b%a),a));
}

int cont(int k,int a){
    int res=0;
    while(k!=a){
        if(k>a){
            k=k/2;
            res++;
        }
        if(k<a){
            k=k*2;
            res++;
        }
    }
    return res;
}

int main(){
    int n,arr[MAX];
    cin>>n;
    int temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        arr[i]=temp;
    }
    sort(arr,arr+n);
    int a=arr[0];
    int b;
    for(int i=1;i<n;i++){
        b=arr[i];
        if(a%2==1 && b%2==1){
            a=1;
        }else{
            if(b%a==0){
                //cout<<a<<" "<<b<<" "<<n<<endl;
                if(n%2==1){
                    if(i<=n/2) a=b;
                    else a=a;
                }else{
                    if(i<=n/2-1) a=b;
                    else a=a;
                }
            }else{
                int c=gcd(a,b);
                a=a/gcd(a,b);
                b=b/gcd(a,b);
                if(a%2==0 && b%2==1) a=a;
                if(b%2==0 && a%2==1) a=b;
                if(a%2==1 && b%2==1) a=1;
            }
        }
    }
    int myres=0;
    for(int i=0;i<n;i++){
        myres+=cont(arr[i],a);
    }
    cout<<myres<<endl;
    return 0;
}


#include <iostream>
#include <climits>

using namespace std;

#define HH 100002

int n;
int mmm;
bool a[HH];
pair<int, int> l[HH]; // count, dis
pair<int, int> r[HH]; // count, dis

pair<int, int> cur_l; // count, dis
pair<int, int> cur_r;
pair<int, int> parent;

int ans;

// total n
// total dis to cur
pair<int, int> count(int cur, bool left, int dis) {
	if (a[cur]) {
		if (left) {
			cur_l.first++;
			cur_l.second += dis;
		}
		else {
			cur_r.first++;
			cur_r.second += dis;
		}
	}

	 // all child count, all child dis sum
	int lc = cur * 2;
	int rc = lc + 1;
	if (lc < mmm) {
		auto ret = count(lc, left, dis + 1);
		l[cur].first = ret.first;
		l[cur].second = ret.second + l[cur].first;
	}
	if (rc < mmm) {
		auto ret = count(rc, left, dis + 1);
		r[cur].first = ret.first;
		r[cur].second = ret.second + r[cur].first;
	}
	pair<int, int> to_ret = make_pair(l[cur].first + r[cur].first + (a[cur] ? 1 : 0), l[cur].second + r[cur].second);
	return to_ret;
}

void dfs(int cur) {
	ans = min(ans, cur_l.second + cur_r.second + parent.second);

	// cout << "cur: " << cur << endl;

	auto copyparent = parent;
	auto copycurl = cur_l;
	auto copycurr = cur_r;
	int lc = 2 * cur;
	int rc = lc + 1;
	{ // to left child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_r.second + cur_r.first * 2;

		parent.first += cur_r.first;

		cur_l.second -= cur_l.first;
		if (a[lc]) {
			cur_l.first--;
		}
		cur_r = r[rc * 2 + 1];

		if (lc < mmm)
			dfs(lc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

	{ // to right child
		if (a[cur]) {
			parent.first++;
			parent.second += parent.first;
		}

		parent.second += cur_l.second + cur_l.first * 2;

		parent.first += cur_l.first;

		cur_r.second -= cur_r.first;
		if (a[rc]) {
			cur_r.first--;
		}
		cur_l = l[lc * 2 + 1];

		if (lc < mmm)
			dfs(rc);

		parent = copyparent;
		cur_l = copycurl;
		cur_r = copycurr;
	}

}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		a[x] = true;
		mmm = max(mmm, x);
	}

	mmm++;
	l[1] = count(2, true, 1);
	r[1] = count(3, false, 1);

	ans = INT_MAX;
	dfs(1);
	cout << ans << endl;
}


#include<stdio.h>

int main()
{
	int ar[100000],size,i,max,min;
	int test,operCount=0;

	scanf("%d", &size);
	for (i=0; i <= size-1; i++)
		scanf("%d", &ar[i]);

	max = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(max<ar[i])
		{
			max = ar[i];
		}
	}

	min = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(min>ar[i])
		{
			min = ar[i];
		}
	}

	for (i=0; i <= size-1; i++)
	{
		int r = max%ar[i];
		if(r==0)
		{
			test = 1;
		}
		else
		{
			test=0;
			break;
		}
	}
	for(i=0; i <= size-1; i++)
	{
		if(test==1)
		{
			while(ar[i]<max)
			{
				ar[i]=ar[i]*2;
				operCount++;
			}
		}
		if(test==0)
		{
			for(i=0; i <= size-1; i++)
			{
				while(ar[i]!=min/2)
			{
				ar[i]=ar[i]/2;
				operCount++;
			}

			}
		}

	}

	printf("%d",operCount);
}


#include<stdio.h>

int main()
{
	int ar[100000],size,i,max,min,lessmax;
	int test,operCount=0,anseto1=0;

	scanf("%d", &size);
	for (i=0; i <= size-1; i++)
		scanf("%d", &ar[i]);

	max = ar[0];
	lessmax = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(max<ar[i])
		{
			lessmax = max;
			max = ar[i];

		}
	}

	min = ar[0];
	for (i=0; i <= size-1; i++)
	{
		if(min>ar[i])
		{
			min = ar[i];

		}
		else if(min==1)
			{
				anseto1=1;
				break;
			}
	}

	for (i=0; i <= size-1; i++)
	{
		int r = max%ar[i];
		if(r==0)
		{
			test = 1;
		}
		else
		{
			test=0;
			break;
		}
	}
	for(i=0; i <= size-1; i++)
	{
		if(test==1)
		{
			while(ar[i]!=lessmax )
			{
				if(ar[i]<lessmax)
				{
				ar[i]=ar[i]*2;
				operCount++;
				}
				else if(ar[i]>lessmax)
				{
					ar[i]=ar[i]/2;
					operCount++;

				}
			}
		}
		if(test==0)
		{
			for(i=0; i <= size-1; i++)
			{
				if(anseto1==1)
				{
					while(ar[i]!=min)
					{
					ar[i]=ar[i]/2;
					operCount++;
					}
				}
				else
				{
					while(ar[i]!=min/2)
					{
					ar[i]=ar[i]/2;
					operCount++;
					}
				}

			}
		}

	}

	printf("%d",operCount);
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
ll mn = 1000000000;

void go(int up , int st , int n , int i){
    if(v[up] != i){
        a[up] ++;
        v[up] = i;
        step[up] += st;
        if(a[up] == n){
            if(mn >= step[up])
                mn = step[up];
        }
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , -1 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100000){
        go(up , st1 , n , i);
        up *= 2;
        st1 ++;
    }
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             go(dn , st2 , n , i);
             go(dn * 2 , st2 + 1 , n , i);
         }else{
             dn /= 2;
             go(dn , st2 , n , i);
         }
         st2 ++;
    }
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
vector <int> b;

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t / 2 , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100020){
        int f = up;
        for(int j = 0 ; j < m ; j++){
            if(!v[f] && f <= 100000){
                v[f] = 1;
                a[f] ++;
                step[f] += st1;
                if(a[f] == n)
                    b.push_back(f);
            }else if(f >100000)
                break;
            f++;
        }
        up *= 2;
        m *= 2;
        st1 ++;
    }
    while(dn > 0){
            a[dn]++;
            step[dn] += st2;
            if(a[dn] == n)
                b.push_back(dn);
        dn /= 2;
        st2++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
ll mn = 1000000000;

void go(int up , int st , int n , int i){
    if(v[up] != i){
        a[up] ++;
        v[up] = i;
        step[up] += st;
        if(a[up] == n){
            if(mn >= step[up])
                mn = step[up];
        }
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , -1 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100000){
        go(up , st1 , n , i);
        up *= 2;
        st1 ++;
    }
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             while(dn <= 100000){
                go(dn , st2 , n , i);
                st2 ++;
                dn *= 2;
             }
         }else{
             dn /= 2;
             go(dn , st2 , n , i);
         }
         st2 ++;
    }
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100008];
int step[100008];
vector <int> b;

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    a[t]++;
    if(a[t] == n)
        b.push_back(t);
    v[t] = 1;
    int up = t , dn = t , st1 = 1 , st2 = 1;
    while(up * 2 <= 100020){
        if(!v[up * 2]){
            v[up * 2] = 1;
            a[up * 2]++;
            step[up * 2] += st1;
            if(a[up * 2] == n)
                b.push_back(up * 2);
        }
        if(!v[up * 2 + 1] && up * 2 + 1 <= 100020){
            v[up * 2 + 1] = 1;
            a[up * 2 + 1]++;
            step[up * 2 + 1] += st1;
            if(a[up * 2 + 1] == n)
                b.push_back(up * 2 + 1);
        }
        up *= 2;
        st1++;
    }
    while(dn / 2 > 0){
        if(!v[dn / 2]){
            v[dn / 2] = 1;
            a[dn / 2]++;
            step[dn / 2] += st2;
            if(a[dn / 2] == n)
                b.push_back(dn / 2);
        }
        dn /= 2;
        st2++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
ll mn = 1000000000;
vector <int> b;

void go(int up , int st , int n){
    if(!v[up]){
        a[up] ++;
        v[up] = 1;
        step[up] += st;
        if(a[up] == n){
            if(mn >= step[up])
                mn = step[up];
        }
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100000){
        go(up , st1 , n);
        up *= 2;
        st1 ++;
    }
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             go(dn , st2 , n);
             go(dn * 2 , st2 + 1 , n);
         }else{
             dn /= 2;
             go(dn , st2 , n);
         }
         st2 ++;
    }
    memset(v , 0 , sizeof(v));
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100001];
int v[100001];
int step[100001];
bool f=0;
stack <int> q;
vector <int> b;

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    a[t]++;
    v[t] = 1;
    int up = t , dn = t , st1 = 1 , st2 = 1;
    while(up * 2 <= 100000){
        if(!v[up * 2]){
            v[up * 2] = 1;
            a[up * 2]++;
            step[up * 2] += st1;
            if(a[up * 2] == n)
                b.push_back(up * 2);
        }
        if(!v[up * 2 + 1] && up * 2 + 1 <= 100000){
            v[up * 2 + 1] = 1;
            a[up * 2 + 1]++;
            step[up * 2 + 1] += st1;
            if(a[up * 2 + 1] == n)
                b.push_back(up * 2 + 1);
        }
        up *= 2;
        st1++;
    }
    while(dn / 2 > 0){
        if(!v[dn / 2]){
            v[dn / 2] = 1;
            a[dn / 2]++;
            step[dn / 2] += st2;
            if(a[dn / 2] == n)
                b.push_back(dn / 2);
        }
        dn /= 2;
        st2++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
ll mn = 1000000000;

void go(int up , int st , int n , int i){
    if(v[up] != i){
        a[up] ++;
        v[up] = i;
        step[up] += st;
        if(a[up] == n){
            if(mn >= step[up])
                mn = step[up];
        }
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , -1 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100000){
        go(up , st1 , n , i);
        up *= 2;
        st1 ++;
    }
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             int g = dn , tp = st2;
             while(g <= 100000){
                go(g , tp , n , i);
                tp ++;
                g *= 2;
             }
         }else{
             dn /= 2;
             go(dn , st2 , n , i);
         }
         st2 ++;
    }
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
ll mn = 1000000000;
vector <int> b;

void go(int up , int st , int n){
    while(up <= 100000){
        if(!v[up]){
            v[up] = 1;
            a[up] ++;
            step[up] += st;
            if(a[up] == n){
                if(mn >= step[up])
                    mn = step[up];
            }
        }
        up *= 2;
        st ++;
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    go(up , st1 , n);
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             go(dn , st2 , n);
         }else{
             dn /= 2;
             if(!v[dn]){
                v[dn] = 1;
                a[dn] ++;
                step[dn] += st2;
                if(a[dn] == n){
                    if(mn >= step[dn])
                        mn = step[dn];
                }
             }
         }
         st2 ++;
    }
    memset(v , 0 , sizeof(v));
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
vector <int> b;

void go(int up , int st , int n){
    while(up <= 100000){
        if(!v[up]){
            v[up] = 1;
            a[up] ++;
            step[up] += st;
            if(a[up] == n){
                b.push_back(up);
            }
        }
        up *= 2;
        st ++;
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    go(up , st1 , n);
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             go(dn , st2 , n);
         }else{
             dn /= 2;
             if(!v[dn]){
                v[dn] = 1;
                a[dn] ++;
                step[dn] += st2;
                if(a[dn] == n){
                    b.push_back(dn);
                }
             }
         }
         st2 ++;
    }
    memset(v , 0 , sizeof(v));
}
ll mn = 1000000000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100008];
int step[100008];
vector <int> b;

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    a[t]++;
    v[t] = 1;
    int up = t , dn = t , st1 = 1 , st2 = 1;
    while(up * 2 <= 100020){
        if(!v[up * 2]){
            v[up * 2] = 1;
            a[up * 2]++;
            step[up * 2] += st1;
            if(a[up * 2] == n)
                b.push_back(up * 2);
        }
        if(!v[up * 2 + 1] && up * 2 + 1 <= 100020){
            v[up * 2 + 1] = 1;
            a[up * 2 + 1]++;
            step[up * 2 + 1] += st1;
            if(a[up * 2 + 1] == n)
                b.push_back(up * 2 + 1);
        }
        up *= 2;
        st1++;
    }
    while(dn / 2 > 0){
        if(!v[dn / 2]){
            v[dn / 2] = 1;
            a[dn / 2]++;
            step[dn / 2] += st2;
            if(a[dn / 2] == n)
                b.push_back(dn / 2);
        }
        dn /= 2;
        st2++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
vector <int> b;

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t / 2 , st1 = 0 , st2 = 1 , m = 1;
    while(up <= 100020){
        int f = up;
        for(int j = 0 ; j < m ; j++){
            if(!v[f] && f <= 100){
                v[f] = 1;
                a[f] ++;
                step[f] += st1;
                if(a[f] == n)
                    b.push_back(f);
            }else if(f >100)
                break;
            f++;
        }
        up *= 2;
        m *= 2;
        st1 ++;
    }
    while(dn > 0){
            a[dn]++;
            step[dn] += st2;
            if(a[dn] == n)
                b.push_back(dn);
        dn /= 2;
        st2++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


#include <bits/stdc++.h>

using namespace std;

typedef vector <int> VI;
typedef pair <int,int> PII;
typedef long long ll;
int a[100008];
int v[100007];
int step[100008];
vector <int> b;

void go(int up , int st , int n){
    while(up <= 100000){
        if(!v[up]){
            v[up] = 1;
            a[up] ++;
            step[up] += st;
            if(a[up] == n){
                b.push_back(up);
            }
        }
        up *= 2;
        st ++;
    }
}

int main(){
int n , t , mx = 0 , m = 0;
memset(a , 0 , sizeof(a));
memset(v , 0 , sizeof(v));
memset(step , 0 , sizeof(step));
cin >> n;
for(int i = 0 ; i < n ; i++){
    cin >> t;
    int up = t , dn = t , st1 = 0 , st2 = 1 , m = 1;
    go(up , st1 , n);
    while(dn / 2 > 0){
         if(dn % 2){
             dn /= 2;
             go(dn , st2 , n);
         }else{
             dn /= 2;
             if(!v[dn]){
                v[dn] = 1;
                a[dn] ++;
                step[dn] += st2;
                if(a[dn] == n){
                    b.push_back(dn);
                }
             }
         }
         st2 ++;
    }
    memset(v , 0 , sizeof(v));
}
int mn = 100000;
for(int i = 0 ; i < b.size() ; i++){
    if(mn >= step[ b[i] ])
        mn = step[ b[i] ];
}
cout << mn;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo,N;
//map <ll,int> M;
int n;

ll bat(ll k){
    ll ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

ll bbt(ll x,ll y){
	//cout<<x<<" "<<y<<endl;
	ll t=0;
	while (x && x!=y && (x&1)==0) {
		t++;
		x>>=1;
		y>>=1;
	}
	if (x==y) return 2*t; else return oo;
}

ll get_it(int k){
	//if ((a[1]>>(N-k)&1)==0) return oo;
    ll t=a[1],x0=b[1],y,z=oo;
    if (x0<k) t<<=(k-x0); else t>>=(x0-k);
    ll cnt0=abs(k-x0),cnt,x,T;
    x0=k;
    for (int i=0; i<=20; i++){
        cnt=cnt0+i;
        x=x0+i;
        T=(t<<i);
        if (T<0) return z;
        for (int j=2; j<=n; j++) {
            y=a[j];
            if (b[j]<x) y<<=(x-b[j]); else y>>=(b[j]-x);
            if (T!=y) {
                ll p=bbt(T,y);
                if (p>=oo) return oo;
                cnt+=p;
                if (b[j]<x) {
                	cnt-=abs(x-b[j]);
                	continue;
				}
            }
            cnt+=abs(x-b[j]);
        }
        z=min(z,cnt);
        //if (z==90) cout<<k<<" "<<i<<endl;
    }
    //cout<<k<<endl;
    return z;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",&a[i]);
    sort(a+1,a+n+1);
    for (int i=1; i<=n; i++) b[i]=bat(a[i]);
    N=bat(a[1]);
    for (int i=1; i<=N; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo;
int n;

ll bat(ll k){
    int ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

ll get_it(int k){
    ll t=a[1],x=b[1],y;
    if (x<k) t<<=(k-x); else t>>=(x-k);
    ll cnt=abs(k-x);
    for (int i=2; i<=n; i++) {
        y=a[i];
        if (b[i]<k) y<<=(k-b[i]); else y>>=(b[i]-k);
        if (t!=y) return oo;
        cnt+=abs(k-b[i]);
    }
    return cnt;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        b[i]=bat(a[i]);
    }
    for (int i=1; i<=30; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo;
int n;

ll bat(ll k){
    int ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

ll get_it(int k){
    ll t=a[1],x0=b[1],y,z=oo;
    if (x0<k) t<<=(k-x0); else t>>=(x0-k);
    ll cnt0=abs(k-x0),cnt,x,T;
    x0=k;
    for (int i=0; i<=20; i++){
        cnt=cnt0+i;
        x=x0+i;
        T=(t<<i);
        for (int i=2; i<=n; i++) {
            y=a[i];
            if (b[i]<x) y<<=(x-b[i]); else y>>=(b[i]-x);
            if (T!=y) {
                cnt=oo;
                break;
            }
            cnt+=abs(x-b[i]);
        }
        z=min(z,cnt);
    }

    return z;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        b[i]=bat(a[i]);
    }
    swap(a[1],a[n]);
    swap(b[1],b[n]);
    for (int i=1; i<=30; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo;
int n;

ll bat(ll k){
    ll ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

ll bbt(ll x,ll y){
	//cout<<x<<" "<<y<<endl;
	ll t=0;
	while (x && x!=y && (x&1)==0) {
		t++;
		x>>=1;
		y>>=1;
	}
	if (x==y) return 2*t; else return oo;
}

ll get_it(int k){
    ll t=a[1],x0=b[1],y,z=oo;
    if (x0<k) t<<=(k-x0); else t>>=(x0-k);
    ll cnt0=abs(k-x0),cnt,x,T;
    x0=k;
    for (int i=0; i<=10; i++){
        cnt=cnt0+i;
        x=x0+i;
        T=(t<<i);
        if (T<0) return z;
        for (int j=2; j<=n; j++) {
            y=a[j];
            if (b[j]<x) y<<=(x-b[j]); else y>>=(b[j]-x);
            if (T!=y) {
                ll p=bbt(T,y);
                if (p>=oo) return oo;
                cnt+=p;
                if (b[j]<x) {
                	cnt-=abs(x-b[j]);
                	continue;
				}
            }
            cnt+=abs(x-b[j]);
        }
        z=min(z,cnt);
        //if (z==90) cout<<k<<" "<<i<<endl;
    }
    //cout<<k<<endl;
    return z;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        b[i]=bat(a[i]);
    }
    //get_it(8);
    for (int i=1; i<=30; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo,N;
//map <ll,int> M;
int n;

ll bat(ll k){
    ll ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

ll bbt(ll x,ll y){
	//cout<<x<<" "<<y<<endl;
	ll t=0;
	while (x && x!=y && (x&1)==0) {
		t++;
		x>>=1;
		y>>=1;
	}
	if (x==y) return 2*t; else return oo;
}

ll get_it(int k){
	if ((a[1]>>(N-k)&1)==0) return oo;
    ll t=a[1],x0=b[1],y,z=oo;
    if (x0<k) t<<=(k-x0); else t>>=(x0-k);
    ll cnt0=abs(k-x0),cnt,x,T;
    x0=k;
    for (int i=0; i<=15; i++){
        cnt=cnt0+i;
        x=x0+i;
        T=(t<<i);
        if (T<0) return z;
        for (int j=2; j<=n; j++) {
            y=a[j];
            if (b[j]<x) y<<=(x-b[j]); else y>>=(b[j]-x);
            if (T!=y) {
                ll p=bbt(T,y);
                if (p>=oo) return oo;
                cnt+=p;
                if (b[j]<x) {
                	cnt-=abs(x-b[j]);
                	continue;
				}
            }
            cnt+=abs(x-b[j]);
        }
        z=min(z,cnt);
        //if (z==90) cout<<k<<" "<<i<<endl;
    }
    //cout<<k<<endl;
    return z;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        b[i]=bat(a[i]);
    }
    //get_it(8);
    sort(a+1,a+n+1);
    N=bat(a[1]);
    for (int i=1; i<=N; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
//const int _p = 1007;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
//ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
using namespace std;

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

ll a[200005],b[200005],ans=oo;
int n;

ll bat(ll k){
    int ans=0;
    while (k){
        ans++;
        k>>=1;
    }
    return ans;
}

int bbt(int x,int y){
	int t=0;
	while (x && x!=y && (x&1)==0) {
		t++;
		x>>=1;
		y>>=1;
	}
	if (x==y) return 2*t; else return oo;
}

ll get_it(int k){
    ll t=a[1],x0=b[1],y,z=oo;
    if (x0<k) t<<=(k-x0); else t>>=(x0-k);
    ll cnt0=abs(k-x0),cnt,x,T;
    x0=k;
    for (int i=0; i<=10; i++){
        cnt=cnt0+i;
        x=x0+i;
        T=(t<<i);
        for (int j=2; j<=n; j++) {
            y=a[j];
            if (b[j]<x) y<<=(x-b[j]); else y>>=(b[j]-x);
            if (T!=y) {
                cnt+=bbt(T,y);
                if (cnt>=oo) break;
            }
            cnt+=abs(x-b[j]);
        }
        z=min(z,cnt);
    }
    //cout<<k<<endl;
    return z;
}

int main() {
    //file_put();

    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        b[i]=bat(a[i]);
    }
    for (int i=1; i<=30; i++) ans=min(ans,get_it(i));
    cout<<ans<<endl;

    return 0;
}