import sys
n = int(input())

directions = input()

coordinates = [int(x) for x in input().split()]

min_time = sys.maxsize * 2 + 1
max_r = -sys.maxsize * 2
for i in range(len(coordinates)):
    if directions[i] == 'L':
        if min_time > (coordinates[i] - max_r) // 2:
            min_time = (coordinates[i] - max_r) // 2
    else:
        max_r = coordinates[i]

if min_time > max(coordinates):
    print(-1)
else:
    print(min_time)


import math
n = int(input())
s = input()
l1 = list(map(int,input().split()))
min1 = math.inf
ind = 0
for i in range(len(s)-1):
    if s[i]=="R" and s[i+1]=="L":
        d = l1[i+1]-l1[i]
        if d<min1:
            min1 = d
            ind = i
if ind:
    print(min1//2)
else:
    print(-1)


n=  int(input())
s = input()
l1 = list(map(int,input().split()))
index1 = -1
try:
    index1 = s.index("RL")
except:
    pass
if index1==-1:
    print(-1)
else:
    print((l1[index1+1]-l1[index1])//2)


import math
n = int(input())
s = input()
l1 = list(map(int,input().split()))
min1 = math.inf
ind = -1
for i in range(len(s)-1):
    if s[i]=="R" and s[i+1]=="L":
        d = l1[i+1]-l1[i]
        if d<min1:
            min1 = d
            ind = i
if ind!=-1:
    print(min1//2)
else:
    print(-1)


__author__ = 'Alexander'
import sys
n = int(sys.stdin.readline())
direct = sys.stdin.readline()
positions = sys.stdin.readline().split()
if direct.find('RL') == -1:
    sys.stdout.write("-1")
else:
    start = 0
    expl = 1000000000
    pos = direct.find('RL')
    while pos != -1:
        start += pos
        expl = min(int((int(positions[start+1])-int(positions[start]))/2), expl)
        direct = direct[pos+2:]
        start += 2
        pos = direct.find('RL')
    sys.stdout.write(str(expl))


__author__ = 'Alexander'
import sys
n = int(sys.stdin.readline())
direct = sys.stdin.readline()
positions = sys.stdin.readline().split()
if direct.find('RL') == -1:
    sys.stdout.write("-1")
else:
    start = 0
    expl = 1000000000
    pos = direct.find('RL')
    while pos != -1:
        start += pos
        expl = min((int(positions[start+1])-int(positions[start]))/2, expl)
        direct = direct[pos+2:]
        start += 2
        pos = direct.find('RL')
    sys.stdout.write(str(expl))


n=int(input())
m=str(input())
o=[int(i) for i in input().split()]
min=1000000000
for i in range(0,len(m)-1):
    if(m[i]=="R" and m[i+1]=="L"):
        time=((o[i+1]-o[i])/2)
        if(time<min):
            min=time
if(min==1000000000):
    print(-1)
else:
    print(int(min))


n = int(input())
dxs = input()
map_r = set()

for i in range(n):
    dx = dxs[i]
    if dx == 'R':
        map_r.add(i)
nb_rights = len(map_r)
nb_lefts = n - nb_rights
rights = [0] * nb_rights
cur_right = 0
lefts = [0] * nb_lefts
cur_left = 0

tab = list(map(int,input().split()))
for i in range(n):
    if i in map_r:
        rights[cur_right] = tab[i]
        cur_right += 1
    else:
        lefts[cur_left] = tab[i]
        cur_left += 1

min_diff = 10**10

for i in range(min(nb_rights,nb_lefts)):
    right = rights[i]
    left = lefts[i]
    if left > right:
        time = (left - right)//2
        min_diff = min(min_diff,time)

if min_diff == 10**10:
    print(-1)
else:
    print(min_diff)


n=int(input())
napr=input()
stroka=input()
coord=[]
k=''
for i in range(0,len(stroka)):
    if stroka[i]==' ':
        coord.append(int(k))
        k=''
    else:
        k+=stroka[i]
coord.append(int(k))
left=[]
right=[]
answer=5*10**8
for i in range(0,n):
    if napr[i]=='L':
        left.append(coord[i])
    else:
        right.append(coord[i])
for i in range(0,len(right)):
    for j in range(0,len(left)):
        if (right[i]<left[j]):
            if (left[j]-right[i])%2!=0:
                k=round((left[j]-right[i])/2)+1
            else:
                k=round((left[j]-right[i])/2)
            if k<answer:
                answer=k
if answer==5*10**8:
    answer=-1
print(answer)


n=int(input())
napr=input()
stroka=input()
coord=[]
k=''
for i in range(0,len(stroka)):
    if stroka[i]==' ':
        coord.append(int(k))
        k=''
    else:
        k+=stroka[i]
coord.append(int(k))
left=[]
right=[]
answer=10**10
for i in range(0,n):
    if napr[i]=='L':
        left.append(coord[i])
    else:
        right.append(coord[i])
for i in range(0,len(right)):
    for j in range(0,len(left)):
        if (right[i]<left[j]):
            if (left[j]-right[i])%2!=0:
                k=round((left[j]-right[i])/2)+1
            else:
                k=round((left[j]-right[i])/2)
            if k<answer:
                answer=k
if answer==10**10:
    answer=-1
print(answer)


n=int(input())
napr=input()
stroka=input()
coord=[]
k=''
answer=10**10
for i in range(0,len(stroka)):
    if i==len(stroka)-1:
        coord.append(int(k+stroka[i]))
    elif stroka[i]==' ':
        coord.append(int(k))
        k=''
    else:
        k+=stroka[i]
for i in range(1,n):
    if (napr[i]=='L')and(napr[i-1]=='R')and((coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2<answer):
        answer=(coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2
if answer==10**10:
    answer=-1
print(answer)


n=int(input())
napr=input()
stroka=input()
coord=[]
k=''
answer=10**10
for i in range(0,len(stroka)):
    if i==len(stroka)-1:
        coord.append(int(k))
    elif stroka[i]==' ':
        coord.append(int(k))
        k=''
    else:
        k+=stroka[i]
for i in range(1,n):
    if (napr[i]=='L')and(napr[i-1]=='R')and((coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2<answer):
        answer=(coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2
if answer==10**10:
    answer=-1
print(answer)


n = int(input())
s = input()
x = list(map(int, input().split()))
ans = 1000000001
r = s.find('R')
l = s.find('L')
if r!=-1 and l != -1:
    for i in range(0, n):
        if s[i] == 'R':
            r = i
        if s[i] == 'L':
            l = i
        if r < l:
            ans = min(ans, (x[l] - x[r])//2)

if ans == 1000000001:
    ans = -1
print(ans)


n = int(input())
s = input()
x = list(map(int, input().split()))
ans = 1000000001
for i in range(n):
    for j in range(i+1, n):
        if s[i] == 'R' and s[j] == 'L':
            ans = min(ans, (x[j] - x[i])//2)
if ans == 1000000001:
    ans = -1
print(ans)


n=int(input())
s=input()
arr=[int(i)for i in input().split()]
li=[arr[i+1]-arr[i] for i in range(n) if s[i]=='R'and s[i+1]=='L']
if li:
    ans=min(li)//2
    print(ans)
else:
    print(-1)


n=int(input())
s=input()
arr=[int(i)for i in input().split()]
li=[arr[i+1]-arr[i] for i in range(n-1) if s[i]=='R'and s[i+1]=='L']
if li:
    ans=min(li)//2
    print(ans)
else:
    print(-1)


n=int(input())
s=input()
#arr=map(int,input().split())
arr=[int(i)for i in input().split()]
li=[arr[i+1]-arr[i] for i in range(n) if s[i]=='R'and s[i+1]=='L']
if li:
    ans=min(li)/2
    print(ans)
else:
    print(-1)


n=int(input())
s=input()
arr=[int(x) for x in input().split()]
m=99999
for i in range(n-1):
    if(s[i]=='R' and s[i+1]=='L'):
        m=min((arr[i+1]-arr[i])//2,m)
if(m!=99999):
    print(m)
else:
    print("-1")


n=int(input())
s=input()
arr=[int(x) for x in input().split()]
m=1000000007
for i in range(n-1):
    if(s[i]=='R' and s[i+1]=='L'):
        m=min((arr[i+1]-arr[i])//2,m)
if(m!=1000000007):
    print(m)
else:
    print("-1")


n = int(input())
s = list(input())
z = list(map(int, input().split()))
c = 0
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
        c += 1
        break
if (c == 0) | (n == 1):
    print('-1')
    exit()
c = 0
while 1:
    for i in range(n):
        if s[i] == 'R':
            z[i] += 1
        else:
            z[i] -= 1
    c += 1
    for i in range(n-1):
        if z[i] == z[i+1]:
            print(c)
            exit()


n = int(input())
s = list(input())
z = list(map(int, input().split()))
c = 0
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
        c += 1
if (c == 0) | (n == 1):
    print('-1')
    exit()
c = 0
while 1:
    for i in range(n):
        if s[i] == 'R':
            z[i] += 1
        else:
            z[i] -= 1
    c += 1
    for i in range(n-1):
        if z[i] == z[i+1]:
            print(c)
            exit()


n = int(input())
s = list(input())
z = list(map(int, input().split()))
a = list()
c = 0
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
        c += 1
        break
if (c == 0) | (n == 1):
    print('-1')
    exit()
c = 0
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
        while 1:
            z[i] += 1
            z[i+1] -= 1
            c += 1
            if z[i] == z[i+1]:
                a.append(c)
                c = 0
                break
print(min(a))
exit()
while 1:
    for i in range(n):
        if s[i] == 'R':
            z[i] += 1
        else:
            z[i] -= 1
        if i == n-1:
            break
        if s[i+1] == 'R':
            z[i+1] += 1
        else:
            z[i+1] -= 1
        c += 1
        if z[i] == z[i+1]:
            print(c)
            exit()


n = int(input())
s = list(input())
z = list(map(int, input().split()))
a = list()
c = 0
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
        c += 1
        break
if (c == 0) | (n == 1):
    print('-1')
    exit()
for i in range(n-1):
    if (s[i] == 'R') & (s[i+1] == 'L'):
            a.append(z[i+1] - ((z[i] + z[i+1])//2))
print(min(a))
exit()
while 1:
    for i in range(n):
        if s[i] == 'R':
            z[i] += 1
        else:
            z[i] -= 1
        if i == n-1:
            break
        if s[i+1] == 'R':
            z[i+1] += 1
        else:
            z[i+1] -= 1
        c += 1
        if z[i] == z[i+1]:
            print(c)
            exit()


a=list(input())
x=list(map(int,input().split(' ')))
p = [ i for i in range(len(a)-1) if l[i]=='R' and a[i+1]=='L']
if not p :
    print(-1)
else :
    t = [x[i+1]-x[i] for i in p]
    print(min(t)//2)


input()
l=list(input())
x=list(map(int,input().split(' ')))
p = [ i for i in range(len(l)-1) if l[i]=='R' and l[i+1]=='L']
if not p :
    print(-1)
else :
    t = [x[i+1]-x[i] for i in p]
    print(min(t)//2)


l=list(input())
x=list(map(int,input().split(' ')))
p = [ i for i in range(len(l)-1) if l[i]=='R' and l[i+1]=='L']
if not p :
    print(-1)
else :
    t = [x[i+1]-x[i] for i in p]
    print(min(t)//2)


n=int(input())
s=input()
x = list(map(int, input().split(" ")))
for i in  range(1,n):
    if s[i]!=s[i-1]:
        print((x[i]-x[i-1])//2)
        exit()
    if i==n-2:
        break
    else:
        i+=2
print(-1)


n=int(input())
s=input()
x = list(map(int, input().split(" ")))
for i in  range(0,n-1):
    if s[i]=='R'and s[i+1]=='L':
        print((x[i+1]-x[i])//2)
        exit()
print(-1)


n=int(input())
s=input()
a=list(map(int,input().split()))
#print(a)
ans=-1
for i in range(1,n):
    if s[i-1]=='R' and s[i]=='L':
        ans=(min(ans,(a[i]-a[i-1])//2) if ans>=0 else (a[i]-a[i-1])//2)
print(ans)


n=int(input())
s=input()
a=list(map(int,input().split()))
#print(a)
ans=1000000000
for i in range(1,n):
    if s[i-1]=='R' and s[i]=='L':
        ans=min(ans,(a[i]-a[i-1])//2)
print(ans)


n = int(input())
dir = input()
dist = [int(x) for x in input().split()]
sz = len(dir) - 1
time = -1
for i in range(sz):
	if dir[i] == 'R' and dir[i+1] == 'L':
		x = int((dist[i+1] - dist[i]) / 2)
		if time == -1 or x < time:
			time = x
print(time)


n = int(input())
dir = input()
dist = [int(x) for x in input().split()]
sz = len(dir) - 2
time = -1
for i in range(sz):
	if dir[i] == 'R' and dir[i+1] == 'L':
		x = (dist[i+1] - dist[i]) / 2
		if time == -1 or x < time:
			time = x
print(time)


n = int(input())
dir = input()
dist = [int(x) for x in input().split()]
sz = len(dir) - 2
if sz == 0: sz = 1
time = -1
for i in range(sz):
	if dir[i] == 'R' and dir[i+1] == 'L':
		x = int((dist[i+1] - dist[i]) / 2)
		if time == -1 or x < time:
			time = x
print(time)


n = int(input())
dir = input()
dist = [int(x) for x in input().split()]
sz = len(dir) - 2
time = -1
for i in range(sz):
	if dir[i] == 'R' and dir[i+1] == 'L':
		x = int((dist[i+1] - dist[i]) / 2)
		if time == -1 or x < time:
			time = x
print(time)


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            if l_bound==-1:
                break
            #print(l_bound)
            i=l_bound
            continue
    else:
        l_bound=i
    i+=1
if min!=200000:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            if l_bound==-1:
                break
            #print(l_bound)
            i=l_bound
            continue
    i+=1
if min!=200000:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
for i in range(l_bound+1,len(directions)):
    if directions[i]=="L":
        if min>(i-l_bound):
            min=(i-l_bound)
            l_bound = directions.find("R",i)
            i=l_bound
if min!=200000:
    print(min)
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
if l_bound!=-1:
    min =None
    i=l_bound+1
    #for i in range(l_bound+1,len(directions)):
    while i<len(directions):
        #print(i)
        if directions[i]=="L":
            if min==None or min>(items[i]-items[l_bound]):
                #print("L")

                min=(items[i]-items[l_bound])
                #print(min)
                l_bound = directions.find("R",i)
                if l_bound==-1:
                    break
                #print(l_bound)
                i=l_bound
                continue
        else:
            l_bound=i
        i+=1
    if min!=None:
        print(int(min/2))
    else:
        print("-1")
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            #print(l_bound)
            i=l_bound
            continue
    i+=1
if min!=200000:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =None
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min==None or min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            if l_bound==-1:
                break
            #print(l_bound)
            i=l_bound
            continue
    else:
        l_bound=i
    i+=1
if min!=200000:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =None
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min==None or min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            if l_bound==-1:
                break
            #print(l_bound)
            i=l_bound
            continue
    else:
        l_bound=i
    i+=1
if min!=None:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
i=l_bound+1
#for i in range(l_bound+1,len(directions)):
while i<len(directions):
    #print(i)
    if directions[i]=="L":
        if min>(items[i]-items[l_bound]):
            #print("L")

            min=(items[i]-items[l_bound])
            #print(min)
            l_bound = directions.find("R",i)
            if l_bound==-1:
                break
            #print(l_bound)
            i=l_bound
            continue
    else:
        l_bound=[i]
    i+=1
if min!=200000:
    print(int(min/2))
else:
    print("-1")


n=input()
directions = str(input().strip())
items = list(map(int,input().strip().split()))
l_bound = directions.find("R")
min =200000
for i in range(l_bound+1,len(directions)):
    print(i)
    if directions[i]=="L":
        if min>(i-l_bound):
            min=(i-l_bound)
            l_bound = directions.find("R",i)
            i=l_bound
if min!=200000:
    print(min)
else:
    print("-1")


n =  int(input())
dir = list(input())
coord = input().strip().split(' ')
coord = [int(c) for c in coord]
flag = 0
tmin = 10**9
for i in range(n-1):
    if dir[i]=='R' and dir[i+1]=='L':
        flag = 1
        tmin = min(tmin, (coord[i+1]-coord[i])//2)
if flag:
    print(tmin)
else:
    print(-1)


n = int(input())
direction = input()
positions = list(map(int, input().split()))
min_d = positions[-1] - positions[-2]
collide = False

for index in range(n - 1):
    if direction[index] == 'R' and direction[index + 1] == 'L':
        collide = True
        min_d = min(min_d, positions[index + 1] - positions[index])

if collide:
    print(int(min_d/2))
else:
    print(-1)


n = int(input())
direction = input()
positions = list(map(int, input().split()))
min_d = positions[-1]
collide = False

for index in range(n - 1):
    if direction[index] == 'R' and direction[index + 1] == 'L':
        collide = True
        min_d = min(min_d, positions[index + 1] - positions[index])

if collide:
    print(int(min_d/2))
else:
    print(-1)


n = int(input())
m = input()
p = list(map(int,input().split()))
x = []
if (m.find('RL') == -1):
    print(-1)
else:
    for i in range(n):
        if (i != n-1):
            if ((m[i] == 'R') & (m[i+1] == 'L')):
                x.append((p[i+1]-p[i])//2)
            else:
                exit
    print(min(x))


n = int(input())
m = input()
p = list(map(int,input().split()))
x = [0] * (p[n-1]+2)
count = 0
for i in range(n):
    if (m[i] == 'R'):
        if (x[p[i]+1] == 0):
            x[p[i]+1] = p[i]
        else:
            count += 1
    elif (m[i] == 'L'):
        if (x[p[i]-1] == 0):
            x[p[i]-1] = p[i]
        else:
            count += 1
if (count == 0):
    print(-1)
else:
    print(count)


n = int(input())
direcoes = input()

posicoes = input().split()
posicoes = [int(x) for x in posicoes]

if direcoes.find('RL') == -1:
    print('-1')
else:
    tempos = []
    while ((direcoes.find('RL') != -1) and direcoes != ''):
        tempos.append(posicoes[direcoes.find('RL')+1]-posicoes[direcoes.find('RL')])
        direcoes = direcoes.replace('RL', '', 1)
    print(int(min(tempos)/2))


n = int(input())
direcoes = input()

posicoes = input().split()
posicoes = [int(x) for x in posicoes]

if direcoes.find('RL') == -1:
    print('-1')
else:
    tempos = []
    while ((direcoes.find('RL') != -1) and direcoes != ''):
        tempos.append(posicoes[direcoes.find('RL')+1]-posicoes[direcoes.find('RL')])
        direcoes = direcoes.replace('RL', '  ', 1)
    print(int(min(tempos)/2))


n = int(input())
direcoes = input()

posicoes = input().split()
posicoes = [int(x) for x in posicoes]

marcacoes = []

for i in range(0, len(direcoes)-1):
    if direcoes[i] == 'R' and direcoes[i+1] == 'L':
        marcacoes.append(i)

if marcacoes == []:
    print(-1)
else:
    tempos = [(posicoes[x+1]-posicoes[x]) for x in marcacoes]
    print(int(min(tempos)/2))


def main():
    n = int(input())
    d = input()
    o = list(map(int, input().split()))

    rs = []
    ls = []
    for i in range(n):
        if d[i] == 'R':
            rs.append(o[i])
        else:
            ls.append(o[i])

    # print(rs, '\n', ls)

    res = -1
    for i in range(len(rs)):
        j = 0
        while rs[i] > ls[j] and j+1 < len(ls):
            j += 1
        ls = ls[j:]
        cur = (ls[0] - rs[i]) // 2
        if cur < res or res == -1:
            res = cur
        # print(ls[0], rs[i], cur)
    print(res if res > 0 else -1)

if __name__ == '__main__':
    main()


def main():
    n = int(input())
    d = input()
    o = list(map(int, input().split()))

    res = -1
    for i in range(n-1):
        if (d[i], d[i+1]) == ('R', 'L'):
            cur = (o[i+1] - o[i]) // 2
            if res == -1 or res > cur:
                res = cur
    print(res)

if __name__ == '__main__':
    main()


def main():
    n = int(input())
    d = input()
    o = list(map(int, input().split()))

    rs = []
    ls = []
    for i in range(n):
        if d[i] == 'R':
            rs.append(o[i])
        else:
            ls.append(o[i])

    res = -1
    for r in rs:
        for l in ls:
            cur = (l - r) // 2
            if 0 < cur < res or 0 < cur and res == -1:
                res = cur
    print(res)

if __name__ == '__main__':
    main()


def main():
    n = int(input())
    d = input()
    o = list(map(int, input().split()))

    rs = []
    ls = []
    for i in range(n):
        if d[i] == 'R':
            rs.append(o[i])
        else:
            ls.append(o[i])

    # print(rs, '\n', ls)

    res = -1
    for i in range(len(rs)):
        j = 0
        while rs[i] > ls[j] and j+1 < len(ls):
            j += 1
        ls = ls[j:]
        cur = (ls[0] - rs[i]) // 2
        if 0 < cur < res or res == -1:
            res = cur
        # print(ls[0], rs[i], cur)
    print(res if res > 0 else -1)

if __name__ == '__main__':
    main()


from sys import stdin
n =int(input())
str = stdin.readline()
arr = list(map(int,stdin.readline().split()))
r = neg = -1
p = 0
for i in range(n-1):
    if(str[i] == 'R' and str[i+1] == 'L'):
        r = int(( arr[i+1] - arr[i]) / 2)
        if(p == 0 or p > r):
            p = r

if(r == -1):
    print("-1")
else:
    print(p)


from sys import stdin
n =int(input())
str = stdin.readline()
arr = list(map(int,stdin.readline().split()))
r = -1

for i in range(n):
    if(str[i] == 'R' and str[i+1] == 'L'):
        r = int((arr[i+1]-arr[i])/2)
        print(r)
        break
if(r == -1):
    print("-1")


import sys
n = int(input())
string = input()
arr = list(map(int,input().split()))
res = 1000000001
if 'RL' not in string:
    print('-1')
else:
    for i in range(n):
        if i != n-1 and string[i] == 'R' and string[i+1] == 'L':
            if ((arr[i+1] - arr[i])//2) < res:
                res = (arr[i+1] - arr[i])//2

    print(res)


import sys
n = int(input())
string = input()
arr = list(map(int,input().split()))
res = 1000000001
if 'RL' not in string:
    print('-1')
else:
    for i in range(n):
        if string[i] == 'R' and string[i+1] == 'L':
            if (arr[i+1] - arr[i])//2 < res:
                res = (arr[i+1] - arr[i])//2
    print(res)


n = int(input())
s  = input()
x = list(map(int, input().split()))
t = []
for i in range(len(s)):
    if i == len(s)-1:
        flag = False
        break
    if s[i]=='R' and s[i+1]=='L':
        t.append(i)
if flag==False:
    print(-1)
else:
    a = []
    for i in t:
        a.append(x[i+1] - x[i])
    print(min(a)//2)


n = int(input())
s  = input()
x = list(map(int, input().split()))
t = []
flag = False
for i in range(len(s)-1):
    if s[i]=='R' and s[i+1]=='L':
        t.append(i)
        flag = True
if flag==False:
    print(-1)
else:
    a = []
    for i in t:
        a.append(x[i+1] - x[i])
    print(min(a)//2)


import sys
n = int(input())
d = list(input())
k = []
k = [int(i) for i in input().split()]
for i in range(k[-1] - k[0]):
    for l in range(n):
        if d[l] == 'L':
            k[l]-=1
        else:
            k[l]+=1
        for j in range(n):
            if j!= l and k[l] == k[j]:
                print(i+1)
                sys.exit(0)
print(-1)


n=int(input())
s=input()
a=[int(x) for x in input().split()]
mind=1000000001
for i in range(0,n-1):
    if s[i]=='R' and s[i+1]=='L':
        d=a[i+1]-a[i]
        if mind>d:
            mind=d
if mind==1000000001:
    print(-1)
else:
    print (mind//2)


n=int(input())
s=input()
a=[int(x) for x in input().split()]
mind=1000000001
for i in range(0,n-1):
    if s[i]=='R' and s[i+1]=='L':
        d=a[i+1]-a[i]
        if mind>d:
            mind=d
if mind==1000000001:
    print(-1)
else:
    print (mind/2)


n= int(input())
k = input()
arr =[int(x) for x in input().split()]
m=1000000001
for i in range(0,n-1):
    # print(len(m),len(n))
    if k[i]=='R' and k[i+1]=='L':
        m=min(m,(arr[i+1]-arr[i])/2)
        # n+=k[i]
    # else:
    #     m+=k[i]
if m==1000000001:
    print(str(-1))
else:
    print(str(int(m)))


n=int(input())
s=input()
a=list(map(int,input().split()))

res=[a[i+1]-a[i] for i in range(n-1) if s[i]=='R' and s[i+1]=='L']
if(res):
    print(int(min(res)/2))
else: print(-1)


n=int(input())
s=input()
a=list(map(int,input().split()))
Min=2000000000;
for i in range(n-1):
    if(s[i]=='R' and s[i+1]=='L'):
        Min=min(Min,a[i+1]-a[i])
print(int(Min/2))


n=int(input())
s=input()
a=list(map(int,input().split()))
Min=2000000000;
for i in range(n-1):
    if(s[i]=='R' and s[i+1]=='L'):
        Min=min(Min,a[i+1]-a[i])
if(Min!=2000000000):
    print(int(Min/2))
else: print(-1)


n=int(input())
s=input()
a=list(map(int,input().split()))

res=[a[i+1]-a[i] for i in range(n-1) if s[i]=='R' and s[i+1]=='L']
if(res):
    print(min(res)/2)
else: print(-1)


n = int(input())
dirs = input()
pos = list(map(int, input().split()))

times = []
for i in range(len(dirs)-1):
	if dirs[i] == 'R' and dirs[i+1] == 'L':
		time = 0
		while True:
			if pos[i] == pos[i+1]:
				times.append(time)
				break
			pos[i] +=1
			pos[i+1] -=1
			time+=1

if times:
	print(min(times))
else:
	print(-1)


n = int(input())
dirs = input()
pos = list(map(int, input().split()))

times = []
for i in range(len(dirs)-1):
	if dirs[i] == 'R' and dirs[i+1] == 'L':
		times.append((pos[i+1] - pos[i]) / 2)

if times:
	print(int(min(times)))
else:
	print(-1)


N = int(input())
if N == 1:
    print(-1)
    exit()
S = input()
X = list(map(int, input().split()))

stk = []
ans = int(1e9)
for s, x in zip(S, X):
    if len(stk) == 0:
        stk.append((s, x))
        continue

    top = stk[-1]
    if top[0] == "R" and s == "L":
        ans = min(ans, x - (top[1] + x) // 2)
        stk = stk[:-1]
    else:
        stk.append((s, x))
if ans == int(1e9):
    print(-1)
else:
    print(ans)


N = int(input())
if N == 1:
    print(-1)
    exit()
S = input()
X = list(map(int, input().split()))

stk = []
ans = int(1e9)
for s, x in zip(S, X):
    if len(stk) == 0:
        stk.append((s, x))
        continue

    top = stk[-1]
    if top[0] == "R" and s == "L":
        ans = min(ans, x - (top[1] + x) // 2)
        stk.pop()
    else:
        stk.append((s, x))
if ans == int(1e9):
    print(-1)
else:
    print(ans)


import math
n=int(input())
s=input()
lt=list(input().split())
if "RL" not in s:
    print(-1)
else:
    a=s.index("RL")
    sv=int(lt[a+1])-int(lt[a])
    s=s[a+1:]
    try:
        while(len(s)>1):
            a=s.index("RL")
            r=int(lt[a+1])-int(lt[a])
            if r<sv:
                sv=r
            s=s[a+1:]
    except:
        print(end="")
    print(math.ceil(sv/2))


import math
n=int(input())
s=input()
lt=list(input().split())
l=len(lt)-1
mn=10**10
for i in range(l):
    if s[i]=='R' and s[i+1]=='L':
        a=int(lt[i+1])-int(lt[i])
        if a<mn:
            mn=a
if mn==10**10:
    print(-1)
else:
    print(math.ceil(mn/2))


import math
n=int(input())
s=input()
lt=list(input().split())
if "RL" not in s:
    print(-1)
else:
    a=s.index("RL")
    sv=int(lt[a+1])-int(lt[a])
    lt=lt[a+2:]
    s=s[a+2:]
    try:
        while(len(s)>1):
            a=s.index("RL")
            r=int(lt[a+1])-int(lt[a])
            if r<sv:
                sv=r
            s=s[a+2:]
            lt=lt[a+2:]
    except:
        print(end="")
    print(math.ceil(sv/2))


import math

n = int(input())
s = input()

arr = input().split()

l = -1
ind = 0
counter = 0

len_n = len(s)

while(counter<len_n):
    ind = s.find('RL', ind)
    counter += 1
    if (ind == -1):
        break
    else:
        x = int(arr[ind+1]) + int(arr[ind])
        tmp = int(arr[ind+1])-int(x/2)
        if l == -1:
            l = tmp
        if (tmp < l):
            l = tmp
    ind += 2

print(l)


import math

n = int(input())
s = input()

arr = input().split()

ind = s.find('RL')

if (ind == -1):
    print(-1)
else:
    x = int(arr[ind+1]) + int(arr[ind])
    print(int(arr[ind+1])-int(x/2))


//Chaos///

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#if DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
#define mod 1000000007
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.begin();i!=a.end();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define start int tt;cin>>tt;while(tt--)
#define tc int tt;tt=read();while(tt--)
#define nl "\n"
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define dec(v) sort(v.begin(),v.end(),greater<int>());
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define re(a,n) repp(jjj,n)a[jjj]=read();
#define retree(a,m) repp(i,m){int tmp1=read()-1,tmp2=read()-1;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define case(ans) cout << "Case #" << kk << ':' << ' ' << ans << nl
#define er erase
#define sz(a) (int)(a.size())
#define big cpp_int
#define ll128 int128_t //Write (ll128) when using this type.
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define p_q priority_queue
#define bp(i) __builtin_popcountll(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < mod) a += mod;}
template<class T1,class T2>
T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
bool matchpar(ll a, ll b){return (a&1) == (b&1);}
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
template <class T1>void kill(T1 ans){cout << ans << endl;exit(0);}
/*
int n;
const int NNNN=1000*1000+5;
ll BITTree1[NNNN],BITTree2[NNNN];
ll getSum(ll BITree[],int index){
    ll sum = 0;
    index++;
    while (index>0){
        sum += BITree[index];
        index -= index & (-index);
    }
    return sum;
}
void updateBIT(ll BITree[],int index,ll val){
    index++;
    while (index <= n){
        BITree[index] += val;
        index += index & (-index);
    }
}
int sum(int x){
    return getSum(BITTree1,x)*x-getSum(BITTree2,x);
}
void updateRange(int l,int r,int val){
    updateBIT(BITTree1,l,val);
    updateBIT(BITTree1,r+1,-val);
    updateBIT(BITTree2,l,val*(l-1));
    updateBIT(BITTree2,r+1,-val*r);
}
ll rangeSum(int l, int r){
    return sum(r)-sum(l-1);
}
*/
/*
const int NN=2*1000*100+4;
int dsu[NN],sub[NN];
void pre(){
repp(i,NN)dsu[i]=i,sub[i]=1;
}
int root(int a){
    while(dsu[a]!=a){
        dsu[a]=dsu[dsu[a]];
        a=dsu[a];
    }
    return a;
}
void uni(int a,int b){
    int _a=root(a);
    int _b=root(b);
    if(_a==_b)return ;
    if(sub[_a]>sub[_b]){
        dsu[_b]=dsu[_a];
        sub[_a]+=sub[_b];
    }
    else {
        dsu[_a]=dsu[_b];
        sub[_b]+=sub[_a];
    }
}
*/
/*
const int LOGN=19,NNN=1000*1000+5;
int DP[LOGN][NN],level[NNN];
void dfs0(int u)
{
	for(auto it=a[u].begin();it!=a[u].end();it++)
		if(*it!=DP[0][u])
		{
			DP[0][*it]=u;
			level[*it]=level[u]+1;
			dfs0(*it);
		}
}
void preprocess()
{
	dfs0(0);
	for(int i=1;i<LOGN;i++)
		for(int j=0;j<n;j++)
			DP[i][j] = DP[i-1][DP[i-1][j]];
}
int lca(int a,int b)
{
	if(level[a]>level[b])swap(a,b);
	int d = level[b]-level[a];
	for(int i=0;i<LOGN;i++)
		if(d&(1<<i))
			b=DP[i][b];
	if(a==b)return a;
	for(int i=LOGN-1;i>=0;i--)
		if(DP[i][a]!=DP[i][b])
			a=DP[i][a],b=DP[i][b];
	return DP[0][a];
}
*/
/*
ll poww(ll a, ll b)
{
    ll val = 1;
    a %= mod;
    while (b > 0)
    {
        if (b % 2) val = mult(a, val);
        a = mult(a, a);
        b >>= 1;
    }
    return val % mod;
}
ll inv(ll a)
{
    return poww(a , mod - 2);
}
*/
/*
const int jj=2e5+5;
ll fac[jj];
void pre(){
    fac[0] = 1;
    for (int i=1 ; i<jj; i++)
    fac[i] = fac[i-1]*i%mod;
}
ll nc(int n,int r)
{
   if (r==0)
      return 1;
   return (fac[n]*inv(fac[r])%mod*inv(fac[n-r])%mod)%mod;
}
*/
/*
bool iscomp[100000];
vi primes;
void sieve(int n){
    for(int i=2;i<=n;++i){
        if(!iscomp[i]){
            primes.eb(i);
            for(int j=i*i;j<=n;j+=i)iscomp[j]=1;
        }
    }
}
*/
/*
vl prime(ll n){
    vl v;
    for(int i=2;i*i<=n;++i){
        if(n%i==0)v.eb(i);
        while(n%i==0)n/=i;
    }
    if(n!=1)v.eb(n);
    return v;
}
*/
/*
int cc(ll x){
    int ans=0;
    while(x){ans++;x/=2;}
    return ans;
}
*/
// auto cmp = [](ip a, ip b){return a.X==b.X?a.Y>b.Y:a.X>b.X;};
 //   set<ip,decltype(cmp)> s(cmp);

// int dx[4]={1,0,0,-1};
// int dy[4]={0,1,-1,0};

//-----------------------------------------------------end--------------------------------------//

const int N = 1e5 + 5;
int cnt[N] , sum[N];

void solve1(int val , int ch = 0){
    if(val == 0) return ;
    while(val < N){
        sum[val] += ch ++;
        cnt[val] ++;
        val <<= 1;
    }
}

void solve(int val){
    solve1(val);
    int ch = 0;
    while(val){
        sum[val] += ch ++;
        cnt[val] ++;
        if(val & 1) solve1(val - 1 , ch + 1);
        val >>= 1;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    int a[n];
    in(a,n);
    repp(i,n) solve(a[i]);
    int ans = inf;
    repp(i,N){
        if(cnt[i] != n) continue;
        remin(ans , sum[i]);
    }
    cout << ans;
}


//Chaos///

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#if DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
#define mod 1000000007
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.begin();i!=a.end();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define start int tt;cin>>tt;while(tt--)
#define tc int tt;tt=read();while(tt--)
#define nl "\n"
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define dec(v) sort(v.begin(),v.end(),greater<int>());
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define re(a,n) repp(jjj,n)a[jjj]=read();
#define retree(a,m) repp(i,m){int tmp1=read()-1,tmp2=read()-1;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define case(ans) cout << "Case #" << kk << ':' << ' ' << ans << nl
#define er erase
#define sz(a) (int)(a.size())
#define big cpp_int
#define ll128 int128_t //Write (ll128) when using this type.
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define p_q priority_queue
#define bp(i) __builtin_popcountll(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < mod) a += mod;}
template<class T1,class T2>
T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
bool matchpar(ll a, ll b){return (a&1) == (b&1);}
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
template <class T1>void kill(T1 ans){cout << ans << endl;exit(0);}
/*
int n;
const int NNNN=1000*1000+5;
ll BITTree1[NNNN],BITTree2[NNNN];
ll getSum(ll BITree[],int index){
    ll sum = 0;
    index++;
    while (index>0){
        sum += BITree[index];
        index -= index & (-index);
    }
    return sum;
}
void updateBIT(ll BITree[],int index,ll val){
    index++;
    while (index <= n){
        BITree[index] += val;
        index += index & (-index);
    }
}
int sum(int x){
    return getSum(BITTree1,x)*x-getSum(BITTree2,x);
}
void updateRange(int l,int r,int val){
    updateBIT(BITTree1,l,val);
    updateBIT(BITTree1,r+1,-val);
    updateBIT(BITTree2,l,val*(l-1));
    updateBIT(BITTree2,r+1,-val*r);
}
ll rangeSum(int l, int r){
    return sum(r)-sum(l-1);
}
*/
/*
const int NN=2*1000*100+4;
int dsu[NN],sub[NN];
void pre(){
repp(i,NN)dsu[i]=i,sub[i]=1;
}
int root(int a){
    while(dsu[a]!=a){
        dsu[a]=dsu[dsu[a]];
        a=dsu[a];
    }
    return a;
}
void uni(int a,int b){
    int _a=root(a);
    int _b=root(b);
    if(_a==_b)return ;
    if(sub[_a]>sub[_b]){
        dsu[_b]=dsu[_a];
        sub[_a]+=sub[_b];
    }
    else {
        dsu[_a]=dsu[_b];
        sub[_b]+=sub[_a];
    }
}
*/
/*
const int LOGN=19,NNN=1000*1000+5;
int DP[LOGN][NN],level[NNN];
void dfs0(int u)
{
	for(auto it=a[u].begin();it!=a[u].end();it++)
		if(*it!=DP[0][u])
		{
			DP[0][*it]=u;
			level[*it]=level[u]+1;
			dfs0(*it);
		}
}
void preprocess()
{
	dfs0(0);
	for(int i=1;i<LOGN;i++)
		for(int j=0;j<n;j++)
			DP[i][j] = DP[i-1][DP[i-1][j]];
}
int lca(int a,int b)
{
	if(level[a]>level[b])swap(a,b);
	int d = level[b]-level[a];
	for(int i=0;i<LOGN;i++)
		if(d&(1<<i))
			b=DP[i][b];
	if(a==b)return a;
	for(int i=LOGN-1;i>=0;i--)
		if(DP[i][a]!=DP[i][b])
			a=DP[i][a],b=DP[i][b];
	return DP[0][a];
}
*/
/*
ll poww(ll a, ll b)
{
    ll val = 1;
    a %= mod;
    while (b > 0)
    {
        if (b % 2) val = mult(a, val);
        a = mult(a, a);
        b >>= 1;
    }
    return val % mod;
}
ll inv(ll a)
{
    return poww(a , mod - 2);
}
*/
/*
const int jj=2e5+5;
ll fac[jj];
void pre(){
    fac[0] = 1;
    for (int i=1 ; i<jj; i++)
    fac[i] = fac[i-1]*i%mod;
}
ll nc(int n,int r)
{
   if (r==0)
      return 1;
   return (fac[n]*inv(fac[r])%mod*inv(fac[n-r])%mod)%mod;
}
*/
/*
bool iscomp[100000];
vi primes;
void sieve(int n){
    for(int i=2;i<=n;++i){
        if(!iscomp[i]){
            primes.eb(i);
            for(int j=i*i;j<=n;j+=i)iscomp[j]=1;
        }
    }
}
*/
/*
vl prime(ll n){
    vl v;
    for(int i=2;i*i<=n;++i){
        if(n%i==0)v.eb(i);
        while(n%i==0)n/=i;
    }
    if(n!=1)v.eb(n);
    return v;
}
*/
/*
int cc(ll x){
    int ans=0;
    while(x){ans++;x/=2;}
    return ans;
}
*/
// auto cmp = [](ip a, ip b){return a.X==b.X?a.Y>b.Y:a.X>b.X;};
 //   set<ip,decltype(cmp)> s(cmp);

// int dx[4]={1,0,0,-1};
// int dy[4]={0,1,-1,0};

//-----------------------------------------------------end--------------------------------------//

const int N = 1e5 + 5;
int cnt[N] , sum[N];

void solve1(int val , int ch = 0){
    if(val == 0) return ;
    while(val < N){
        sum[val] += ch ++;
        cnt[val] ++;
        val <<= 1;
    }
}

void solve(int val){
    solve1(val << 1 , 1);
    int ch = 0;
    while(val){
        sum[val] += ch ++;
        cnt[val] ++;
        if(val & 1) solve1(val - 1 , ch + 1);
        val >>= 1;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n = read();
    int a[n];
    re(a,n);
    repp(i,n) solve(a[i]);
    int ans = inf;
    repp(i,N){
        if(cnt[i] != n) continue;
        remin(ans , sum[i]);debug(sum[i] , i);
    }
    cout << ans;
}


//Chaos///

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#if DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
#define mod 1000000007
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.begin();i!=a.end();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define start int tt;cin>>tt;while(tt--)
#define tc int tt;tt=read();while(tt--)
#define nl "\n"
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define dec(v) sort(v.begin(),v.end(),greater<int>());
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define re(a,n) repp(jjj,n)a[jjj]=read();
#define retree(a,m) repp(i,m){int tmp1=read()-1,tmp2=read()-1;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define case(ans) cout << "Case #" << kk << ':' << ' ' << ans << nl
#define er erase
#define sz(a) (int)(a.size())
#define big cpp_int
#define ll128 int128_t //Write (ll128) when using this type.
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define p_q priority_queue
#define bp(i) __builtin_popcountll(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < mod) a += mod;}
template<class T1,class T2>
T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
bool matchpar(ll a, ll b){return (a&1) == (b&1);}
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
template <class T1>void kill(T1 ans){cout << ans << endl;exit(0);}
/*
int n;
const int NNNN=1000*1000+5;
ll BITTree1[NNNN],BITTree2[NNNN];
ll getSum(ll BITree[],int index){
    ll sum = 0;
    index++;
    while (index>0){
        sum += BITree[index];
        index -= index & (-index);
    }
    return sum;
}
void updateBIT(ll BITree[],int index,ll val){
    index++;
    while (index <= n){
        BITree[index] += val;
        index += index & (-index);
    }
}
int sum(int x){
    return getSum(BITTree1,x)*x-getSum(BITTree2,x);
}
void updateRange(int l,int r,int val){
    updateBIT(BITTree1,l,val);
    updateBIT(BITTree1,r+1,-val);
    updateBIT(BITTree2,l,val*(l-1));
    updateBIT(BITTree2,r+1,-val*r);
}
ll rangeSum(int l, int r){
    return sum(r)-sum(l-1);
}
*/
/*
const int NN=2*1000*100+4;
int dsu[NN],sub[NN];
void pre(){
repp(i,NN)dsu[i]=i,sub[i]=1;
}
int root(int a){
    while(dsu[a]!=a){
        dsu[a]=dsu[dsu[a]];
        a=dsu[a];
    }
    return a;
}
void uni(int a,int b){
    int _a=root(a);
    int _b=root(b);
    if(_a==_b)return ;
    if(sub[_a]>sub[_b]){
        dsu[_b]=dsu[_a];
        sub[_a]+=sub[_b];
    }
    else {
        dsu[_a]=dsu[_b];
        sub[_b]+=sub[_a];
    }
}
*/
/*
const int LOGN=19,NNN=1000*1000+5;
int DP[LOGN][NN],level[NNN];
void dfs0(int u)
{
	for(auto it=a[u].begin();it!=a[u].end();it++)
		if(*it!=DP[0][u])
		{
			DP[0][*it]=u;
			level[*it]=level[u]+1;
			dfs0(*it);
		}
}
void preprocess()
{
	dfs0(0);
	for(int i=1;i<LOGN;i++)
		for(int j=0;j<n;j++)
			DP[i][j] = DP[i-1][DP[i-1][j]];
}
int lca(int a,int b)
{
	if(level[a]>level[b])swap(a,b);
	int d = level[b]-level[a];
	for(int i=0;i<LOGN;i++)
		if(d&(1<<i))
			b=DP[i][b];
	if(a==b)return a;
	for(int i=LOGN-1;i>=0;i--)
		if(DP[i][a]!=DP[i][b])
			a=DP[i][a],b=DP[i][b];
	return DP[0][a];
}
*/
/*
ll poww(ll a, ll b)
{
    ll val = 1;
    a %= mod;
    while (b > 0)
    {
        if (b % 2) val = mult(a, val);
        a = mult(a, a);
        b >>= 1;
    }
    return val % mod;
}
ll inv(ll a)
{
    return poww(a , mod - 2);
}
*/
/*
const int jj=2e5+5;
ll fac[jj];
void pre(){
    fac[0] = 1;
    for (int i=1 ; i<jj; i++)
    fac[i] = fac[i-1]*i%mod;
}
ll nc(int n,int r)
{
   if (r==0)
      return 1;
   return (fac[n]*inv(fac[r])%mod*inv(fac[n-r])%mod)%mod;
}
*/
/*
bool iscomp[100000];
vi primes;
void sieve(int n){
    for(int i=2;i<=n;++i){
        if(!iscomp[i]){
            primes.eb(i);
            for(int j=i*i;j<=n;j+=i)iscomp[j]=1;
        }
    }
}
*/
/*
vl prime(ll n){
    vl v;
    for(int i=2;i*i<=n;++i){
        if(n%i==0)v.eb(i);
        while(n%i==0)n/=i;
    }
    if(n!=1)v.eb(n);
    return v;
}
*/
/*
int cc(ll x){
    int ans=0;
    while(x){ans++;x/=2;}
    return ans;
}
*/
// auto cmp = [](ip a, ip b){return a.X==b.X?a.Y>b.Y:a.X>b.X;};
 //   set<ip,decltype(cmp)> s(cmp);

// int dx[4]={1,0,0,-1};
// int dy[4]={0,1,-1,0};

//-----------------------------------------------------end--------------------------------------//

const int N = 2e5 + 5;

void solve1(uml &tmp ,int val , int ch = 0){
    if(val == 0) return ;
    while(val < N){
        tmp[val] = ch ++;
        val <<= 1;
    }
}

uml solve(int val){
    uml tmp;
    int tmp1 = val;
    int ch = 0;
    while(val){
        tmp[val] = ch ++;
        if(val & 1) solve1(tmp , val - 1 , ch + 1);
        val >>= 1;
    }
    solve1(tmp , tmp1);
    return tmp;
}

int cnt[N];

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    int a[n];
    in(a,n);
    uml mp[n];
    repp(i,n) mp[i] = solve(a[i]);
    int ans = inf;
    repp(i,n)repa(j,mp[i]) cnt[j.X] ++;
    repp(i,N){
        if(cnt[i] != n) continue;
        int tmp1 = 0;
        repp(j,n) tmp1 += mp[j][i];
        remin(ans , tmp1);
    }
    cout << ans;
}


//Chaos///

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#if DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
#define mod 1000000007
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.begin();i!=a.end();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define start int tt;cin>>tt;while(tt--)
#define tc int tt;tt=read();while(tt--)
#define nl "\n"
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define dec(v) sort(v.begin(),v.end(),greater<int>());
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define re(a,n) repp(jjj,n)a[jjj]=read();
#define retree(a,m) repp(i,m){int tmp1=read()-1,tmp2=read()-1;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define case(ans) cout << "Case #" << kk << ':' << ' ' << ans << nl
#define er erase
#define sz(a) (int)(a.size())
#define big cpp_int
#define ll128 int128_t //Write (ll128) when using this type.
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define p_q priority_queue
#define bp(i) __builtin_popcountll(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < mod) a += mod;}
template<class T1,class T2>
T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
bool matchpar(ll a, ll b){return (a&1) == (b&1);}
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
template <class T1>void kill(T1 ans){cout << ans << endl;exit(0);}
/*
int n;
const int NNNN=1000*1000+5;
ll BITTree1[NNNN],BITTree2[NNNN];
ll getSum(ll BITree[],int index){
    ll sum = 0;
    index++;
    while (index>0){
        sum += BITree[index];
        index -= index & (-index);
    }
    return sum;
}
void updateBIT(ll BITree[],int index,ll val){
    index++;
    while (index <= n){
        BITree[index] += val;
        index += index & (-index);
    }
}
int sum(int x){
    return getSum(BITTree1,x)*x-getSum(BITTree2,x);
}
void updateRange(int l,int r,int val){
    updateBIT(BITTree1,l,val);
    updateBIT(BITTree1,r+1,-val);
    updateBIT(BITTree2,l,val*(l-1));
    updateBIT(BITTree2,r+1,-val*r);
}
ll rangeSum(int l, int r){
    return sum(r)-sum(l-1);
}
*/
/*
const int NN=2*1000*100+4;
int dsu[NN],sub[NN];
void pre(){
repp(i,NN)dsu[i]=i,sub[i]=1;
}
int root(int a){
    while(dsu[a]!=a){
        dsu[a]=dsu[dsu[a]];
        a=dsu[a];
    }
    return a;
}
void uni(int a,int b){
    int _a=root(a);
    int _b=root(b);
    if(_a==_b)return ;
    if(sub[_a]>sub[_b]){
        dsu[_b]=dsu[_a];
        sub[_a]+=sub[_b];
    }
    else {
        dsu[_a]=dsu[_b];
        sub[_b]+=sub[_a];
    }
}
*/
/*
const int LOGN=19,NNN=1000*1000+5;
int DP[LOGN][NN],level[NNN];
void dfs0(int u)
{
	for(auto it=a[u].begin();it!=a[u].end();it++)
		if(*it!=DP[0][u])
		{
			DP[0][*it]=u;
			level[*it]=level[u]+1;
			dfs0(*it);
		}
}
void preprocess()
{
	dfs0(0);
	for(int i=1;i<LOGN;i++)
		for(int j=0;j<n;j++)
			DP[i][j] = DP[i-1][DP[i-1][j]];
}
int lca(int a,int b)
{
	if(level[a]>level[b])swap(a,b);
	int d = level[b]-level[a];
	for(int i=0;i<LOGN;i++)
		if(d&(1<<i))
			b=DP[i][b];
	if(a==b)return a;
	for(int i=LOGN-1;i>=0;i--)
		if(DP[i][a]!=DP[i][b])
			a=DP[i][a],b=DP[i][b];
	return DP[0][a];
}
*/
/*
ll poww(ll a, ll b)
{
    ll val = 1;
    a %= mod;
    while (b > 0)
    {
        if (b % 2) val = mult(a, val);
        a = mult(a, a);
        b >>= 1;
    }
    return val % mod;
}
ll inv(ll a)
{
    return poww(a , mod - 2);
}
*/
/*
const int jj=2e5+5;
ll fac[jj];
void pre(){
    fac[0] = 1;
    for (int i=1 ; i<jj; i++)
    fac[i] = fac[i-1]*i%mod;
}
ll nc(int n,int r)
{
   if (r==0)
      return 1;
   return (fac[n]*inv(fac[r])%mod*inv(fac[n-r])%mod)%mod;
}
*/
/*
bool iscomp[100000];
vi primes;
void sieve(int n){
    for(int i=2;i<=n;++i){
        if(!iscomp[i]){
            primes.eb(i);
            for(int j=i*i;j<=n;j+=i)iscomp[j]=1;
        }
    }
}
*/
/*
vl prime(ll n){
    vl v;
    for(int i=2;i*i<=n;++i){
        if(n%i==0)v.eb(i);
        while(n%i==0)n/=i;
    }
    if(n!=1)v.eb(n);
    return v;
}
*/
/*
int cc(ll x){
    int ans=0;
    while(x){ans++;x/=2;}
    return ans;
}
*/
// auto cmp = [](ip a, ip b){return a.X==b.X?a.Y>b.Y:a.X>b.X;};
 //   set<ip,decltype(cmp)> s(cmp);

// int dx[4]={1,0,0,-1};
// int dy[4]={0,1,-1,0};

//-----------------------------------------------------end--------------------------------------//

const int N = 2e5 + 5;

uml solve(int val){
    uml tmp;
    int tmp1 = val;
    int ch = 0;
    while(val){
        tmp[val] = ch ++;
        val >>= 1;
    }
    val = tmp1;
    ch = 0;
    while(val < N){
        tmp[val] = ch ++;
        val <<= 1;
    }
    return tmp;
}

int cnt[N];

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    int a[n];
    in(a,n);
    uml mp[n];
    repp(i,n) mp[i] = solve(a[i]);
    int ans = inf;
    repp(i,n)repa(j,mp[i]) cnt[j.X] ++;
    repp(i,N){
        if(cnt[i] != n) continue;
        int tmp1 = 0;
        repp(j,n) tmp1 += mp[j][i];
        remin(ans , tmp1);
    }
    cout << ans;
}


//Chaos///

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#if DEBUG && !ONLINE_JUDGE

    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}

        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };

    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }

    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }

    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
#define mod 1000000007
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.begin();i!=a.end();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define start int tt;cin>>tt;while(tt--)
#define tc int tt;tt=read();while(tt--)
#define nl "\n"
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define dec(v) sort(v.begin(),v.end(),greater<int>());
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define re(a,n) repp(jjj,n)a[jjj]=read();
#define retree(a,m) repp(i,m){int tmp1=read()-1,tmp2=read()-1;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define case(ans) cout << "Case #" << kk << ':' << ' ' << ans << nl
#define er erase
#define sz(a) (int)(a.size())
#define big cpp_int
#define ll128 int128_t //Write (ll128) when using this type.
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define p_q priority_queue
#define bp(i) __builtin_popcountll(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < mod) a += mod;}
template<class T1,class T2>
T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
bool matchpar(ll a, ll b){return (a&1) == (b&1);}
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
template <class T1>void kill(T1 ans){cout << ans << endl;exit(0);}
/*
int n;
const int NNNN=1000*1000+5;
ll BITTree1[NNNN],BITTree2[NNNN];
ll getSum(ll BITree[],int index){
    ll sum = 0;
    index++;
    while (index>0){
        sum += BITree[index];
        index -= index & (-index);
    }
    return sum;
}
void updateBIT(ll BITree[],int index,ll val){
    index++;
    while (index <= n){
        BITree[index] += val;
        index += index & (-index);
    }
}
int sum(int x){
    return getSum(BITTree1,x)*x-getSum(BITTree2,x);
}
void updateRange(int l,int r,int val){
    updateBIT(BITTree1,l,val);
    updateBIT(BITTree1,r+1,-val);
    updateBIT(BITTree2,l,val*(l-1));
    updateBIT(BITTree2,r+1,-val*r);
}
ll rangeSum(int l, int r){
    return sum(r)-sum(l-1);
}
*/
/*
const int NN=2*1000*100+4;
int dsu[NN],sub[NN];
void pre(){
repp(i,NN)dsu[i]=i,sub[i]=1;
}
int root(int a){
    while(dsu[a]!=a){
        dsu[a]=dsu[dsu[a]];
        a=dsu[a];
    }
    return a;
}
void uni(int a,int b){
    int _a=root(a);
    int _b=root(b);
    if(_a==_b)return ;
    if(sub[_a]>sub[_b]){
        dsu[_b]=dsu[_a];
        sub[_a]+=sub[_b];
    }
    else {
        dsu[_a]=dsu[_b];
        sub[_b]+=sub[_a];
    }
}
*/
/*
const int LOGN=19,NNN=1000*1000+5;
int DP[LOGN][NN],level[NNN];
void dfs0(int u)
{
	for(auto it=a[u].begin();it!=a[u].end();it++)
		if(*it!=DP[0][u])
		{
			DP[0][*it]=u;
			level[*it]=level[u]+1;
			dfs0(*it);
		}
}
void preprocess()
{
	dfs0(0);
	for(int i=1;i<LOGN;i++)
		for(int j=0;j<n;j++)
			DP[i][j] = DP[i-1][DP[i-1][j]];
}
int lca(int a,int b)
{
	if(level[a]>level[b])swap(a,b);
	int d = level[b]-level[a];
	for(int i=0;i<LOGN;i++)
		if(d&(1<<i))
			b=DP[i][b];
	if(a==b)return a;
	for(int i=LOGN-1;i>=0;i--)
		if(DP[i][a]!=DP[i][b])
			a=DP[i][a],b=DP[i][b];
	return DP[0][a];
}
*/
/*
ll poww(ll a, ll b)
{
    ll val = 1;
    a %= mod;
    while (b > 0)
    {
        if (b % 2) val = mult(a, val);
        a = mult(a, a);
        b >>= 1;
    }
    return val % mod;
}
ll inv(ll a)
{
    return poww(a , mod - 2);
}
*/
/*
const int jj=2e5+5;
ll fac[jj];
void pre(){
    fac[0] = 1;
    for (int i=1 ; i<jj; i++)
    fac[i] = fac[i-1]*i%mod;
}
ll nc(int n,int r)
{
   if (r==0)
      return 1;
   return (fac[n]*inv(fac[r])%mod*inv(fac[n-r])%mod)%mod;
}
*/
/*
bool iscomp[100000];
vi primes;
void sieve(int n){
    for(int i=2;i<=n;++i){
        if(!iscomp[i]){
            primes.eb(i);
            for(int j=i*i;j<=n;j+=i)iscomp[j]=1;
        }
    }
}
*/
/*
vl prime(ll n){
    vl v;
    for(int i=2;i*i<=n;++i){
        if(n%i==0)v.eb(i);
        while(n%i==0)n/=i;
    }
    if(n!=1)v.eb(n);
    return v;
}
*/
/*
int cc(ll x){
    int ans=0;
    while(x){ans++;x/=2;}
    return ans;
}
*/
// auto cmp = [](ip a, ip b){return a.X==b.X?a.Y>b.Y:a.X>b.X;};
 //   set<ip,decltype(cmp)> s(cmp);

// int dx[4]={1,0,0,-1};
// int dy[4]={0,1,-1,0};

//-----------------------------------------------------end--------------------------------------//

const int N = 1e5 + 5;
uml mp[N];
void solve1(uml &tmp ,int val , int ch = 0){
    if(val == 0) return ;
    while(val < N){
        tmp[val] = ch ++;
        val <<= 1;
    }
}

uml solve(int val){
    uml tmp;
    int tmp1 = val;
    int ch = 0;
    while(val){
        tmp[val] = ch ++;
        if(val & 1) solve1(tmp , val - 1 , ch + 1);
        val >>= 1;
    }
    solve1(tmp , tmp1);
    return tmp;
}

int cnt[N];

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    int a[n];
    in(a,n);
    repp(i,n) mp[i] = solve(a[i]);
    int ans = inf;
    repp(i,n)repa(j,mp[i]) cnt[j.X] ++;
    repp(i,N){
        if(cnt[i] != n) continue;
        int tmp1 = 0;
        repp(j,n) tmp1 += mp[j][i];
        remin(ans , tmp1);
    }
    cout << ans;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

int appear[100005];
int cnt[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    unordered_map<int, int> m;
    for(int a = 0; true; a++) {
      if((curr >> a) == 0) break;
      int now = curr >> a;
      for(int b = 0; now <= 100000; b++) {
        if(!m.count(now)) {
          m[now] = a+b;
        }
        else {
          m[now] = min(m[now], a+b);
        }
        now <<= 1;
      }
    }
    for(unordered_map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      appear[it->first]++;
      cnt[it->first] += it->second;
    }
  }
  int ret = 1e9;
  for(int i = 1; i <= 100000; i++) {
    if(appear[i] == n) {
      ret = min(ret, cnt[i]);
    }
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

int appear[100005];
int cnt[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    unordered_map<int, int> m;
    for(int a = 0; true; a++) {
      if((curr >> a) == 0) break;
      int now = curr >> a;
      for(int b = 0; now <= 100000; b++) {
        if(!m.count(now)) {
          m[now] = a+b;
        }
        else {
          break;
        }
        now <<= 1;
      }
    }
    for(unordered_map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      appear[it->first]++;
      cnt[it->first] += it->second;
    }
  }
  int ret = 1e9;
  for(int i = 1; i <= 100000; i++) {
    if(appear[i] == n) {
      ret = min(ret, cnt[i]);
    }
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

int appear[100005];
int cnt[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    unordered_map<int, int> m;
    m[curr] = 0;
    queue<int> q;
    q.push(curr);
    while(!q.empty()) {
      curr = q.front();
      appear[curr]++;
      cnt[curr] += m[curr];
      q.pop();
      if(2*curr <= 100000 && m.count(2*curr) == 0) {
        m[2*curr] = m[curr] + 1;
        q.push(2*curr);
      }
      if(curr/2 > 0 && m.count(curr/2) == 0) {
        m[curr/2] = m[curr] + 1;
        q.push(curr/2);
      }
    }
  }
  int ret = 1e9;
  for(int i = 1; i <= 100000; i++) {
    if(appear[i] == n) {
      ret = min(ret, cnt[i]);
    }
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

int appear[100005];
int cnt[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    unordered_map<int, int> m;
    for(int a = 0; true; a++) {
      if((curr >> a) == 0) break;
      int now = curr >> a;
      if(appear[now] < i) continue;
      for(int b = 0; now <= 100000; b++) {
        if(!m.count(now)) {
          m[now] = a+b;
        }
        else {
          break;
        }
        now <<= 1;
      }
    }
    for(unordered_map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      appear[it->first]++;
      cnt[it->first] += it->second;
    }
  }
  int ret = 1e9;
  for(int i = 1; i <= 100000; i++) {
    if(appear[i] == n) {
      ret = min(ret, cnt[i]);
    }
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

unordered_map<int, int> poss[100005];

int appear[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    poss[i][curr] = 0;
    queue<int> q;
    q.push(curr);
    while(!q.empty()) {
      curr = q.front();
      appear[curr]++;
      q.pop();
      if(2*curr <= 100000 && poss[i].count(2*curr) == 0) {
        poss[i][2*curr] = poss[i][curr] + 1;
        q.push(2*curr);
      }
      if(curr/2 > 0 && poss[i].count(curr/2) == 0) {
        poss[i][curr/2] = poss[i][curr] + 1;
        q.push(curr/2);
      }
    }
  }
  int ret = 1e9;
  for(unordered_map<int, int>::iterator it = poss[0].begin(); it != poss[0].end(); it++) {
    int want = it->first;
    if(appear[want] != n) continue;
    int now = it->second;
    for(int i = 1; i < n && now < ret; i++) {
      if(poss[i].count(want)) {
        now += poss[i][want];
      }
      else {
        now = 1e9;
      }
    }
    ret = min(ret, now);
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

unordered_map<int, int> poss[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    poss[i][curr] = 0;
    queue<int> q;
    q.push(curr);
    while(!q.empty()) {
      curr = q.front();
      q.pop();
      if(2*curr <= 100000 && poss[i].count(2*curr) == 0) {
        poss[i][2*curr] = poss[i][curr] + 1;
        q.push(2*curr);
      }
      if(curr/2 > 0 && poss[i].count(curr/2) == 0) {
        poss[i][curr/2] = poss[i][curr] + 1;
        q.push(curr/2);
      }
    }
  }
  int ret = 1e9;
  for(unordered_map<int, int>::iterator it = poss[0].begin(); it != poss[0].end(); it++) {
    int want = it->first;
    int now = it->second;
    for(int i = 1; i < n && now < ret; i++) {
      if(poss[i].count(want)) {
        now += poss[i][want];
      }
      else {
        now = 1e9;
      }
    }
    ret = min(ret, now);
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// DID YOU FIX GLOBAL STATE

typedef long long ll;
const double PI = 2 * acos(0);
const int MOD = 1000000007;

int appear[100005];
int cnt[100005];

unordered_map<int, int> cache[100005];

void solve() {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    int curr;
    scanf("%d", &curr);
    unordered_map<int, int> &m = cache[curr];
    if(m.size() == 0) {
      for(int a = 0; true; a++) {
        if((curr >> a) == 0) break;
        int now = curr >> a;
        if(appear[now] < i) continue;
        for(int b = 0; now <= 100000; b++) {
          if(!m.count(now)) {
            m[now] = a+b;
          }
          else {
            break;
          }
          now <<= 1;
        }
      }
    }
    for(unordered_map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      appear[it->first]++;
      cnt[it->first] += it->second;
    }
  }
  int ret = 1e9;
  for(int i = 1; i <= 100000; i++) {
    if(appear[i] == n) {
      ret = min(ret, cnt[i]);
    }
  }
  printf("%d\n", ret);
}

int main() {
  solve();
  return 0;
}


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

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

		__count[x]++;

		// cout<<x<<endl;

		unordered_set<int> vis;
		vis.insert(x);

		// multiply by 2
		c = 0;
		j <<= 1;
		while(j < MX){
			vis.insert(j);
			c++;
			cost[j] += c;
			// cout<<"mark "<<j<<" "<<c<<endl;
			__count[j]++;
			j <<= 1;
		}

		// divide by 2
		j = x;
		c = 0;
		bool odd = false;
		while(j > 0){
			odd = (j & 1);
			if(j & 1){
				for(int k = j / 2, extra_cost = 1; k < MX; k <<= 1, extra_cost++){
					if(vis.find(k) == vis.end()){
						vis.insert(k);
						__count[k]++;
						cost[k] += c + extra_cost;
						// cout<<"mark "<<k<<" "<<c + extra_cost<<endl;
					}
					else break;
				}
			}
			c++;
			j >>= 1;
			if(!odd){
				__count[j]++;
				cost[j] += c;
				// cout<<"mark "<<j<<" "<<c<<endl;
			}
		}
		// cout<<"---\n";
	}

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

	write(ans);
	return 0;
}


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

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

		__count[x]++;

		// divide by 2
		c = 0;
		temp >>= 1;
		while(temp > 0){
			c++;
			cost[temp] += c;
			__count[temp]++;
			temp >>= 1;
		}

		temp = x;

		// multiply by 2
		c = 0;
		temp <<= 1;
		while(temp < MX){
			c++;
			cost[temp] += c;
			__count[temp]++;
			temp <<= 1;
		}
	}

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

	write(ans);
	return 0;
}


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

	// n = 100000;
	int max_size = 0;

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

		// cout<<x<<endl

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

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

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

		max_size = max(max_size, (int)cost_cur_x.size());

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

	// cout<<max_size<<endl;

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

	write(ans);
	return 0;
}


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

	// n = 100000;
	// int max_size = 0;

	F(i, n) read(a[i]);

	sort(a, a + n);

	n = unique(a, a + n) - a;

	F(i, n){
		// read(x);
		x = a[i];
		// x = 65533;
		j = x;

		// cout<<x<<endl

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

		// multiply by 2
		c = 1;
		for(k = (j << 1); k < MX; k <<= 1, c++){
			if(__count[k] != i) continue;
			cost_cur_x[k] = c;
		}

		// divide by 2
		j = (x >> 1);
		c = 0;
		while(j > 0){
			c++;
			for(k = j, extra_cost = 0; k < MX; k <<= 1, extra_cost++){
				if(__count[k] != i) continue;
				total_cost = c + extra_cost;
				auto f = cost_cur_x.find(k);
				if(f == cost_cur_x.end()) cost_cur_x[k] = total_cost;
				else if(total_cost < f->second) f->second = total_cost;
				else break;
			}
			j >>= 1;
		}

		// max_size = max(max_size, (int)cost_cur_x.size());

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

	// cout<<max_size<<endl;

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

	write(ans);
	return 0;
}


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

	// n = 100000;
	// int max_size = 0;

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

		// cout<<x<<endl

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

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

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

		// max_size = max(max_size, (int)cost_cur_x.size());

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

	// cout<<max_size<<endl;

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

	write(ans);
	return 0;
}


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

	// n = 100000;
	// int max_size = 0;

	F(i, n) read(a[i]), freq[a[i]]++;

	sort(a, a + n);
	n = unique(a, a + n) - a;

	F(i, n){
		// read(x);
		x = a[i];
		fq = freq[x];
		// x = 65533;
		j = x;

		// cout<<x<<endl

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

		// multiply by 2
		c = 1;
		for(k = (j << 1); k < MX; k <<= 1, c++){
			if(__count[k] != i) continue;
			cost_cur_x[k] = c * fq;
		}

		// divide by 2
		j = (x >> 1);
		c = 0;
		while(j > 0){
			c++;
			for(k = j, extra_cost = 0; k < MX; k <<= 1, extra_cost++){
				if(__count[k] != i) continue;
				total_cost = (c + extra_cost) * fq;
				auto f = cost_cur_x.find(k);
				if(f == cost_cur_x.end()) cost_cur_x[k] = total_cost;
				else if(total_cost < f->second) f->second = total_cost;
				else break;
			}
			j >>= 1;
		}

		// max_size = max(max_size, (int)cost_cur_x.size());

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

	// cout<<max_size<<endl;

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

	write(ans);
	return 0;
}


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

	// n = 100000;
	// int max_size = 0;

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

		// cout<<x<<endl

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

		// multiply by 2
		c = 1;
		for(int k = (j << 1); k < MX; k <<= 1, c++){
			if(__count[k] != i) continue;
			cost_cur_x[k] = c;
		}

		// divide by 2
		j = (x >> 1);
		c = 0;
		while(j > 0){
			c++;
			for(int k = j, extra_cost = 0; k < MX; k <<= 1, extra_cost++){
				if(__count[k] != i) continue;
				int total_cost = c + extra_cost;
				auto f = cost_cur_x.find(k);
				if(f == cost_cur_x.end()) cost_cur_x[k] = total_cost;
				else if(total_cost < f->second) f->second = total_cost;
				else break;
			}
			j >>= 1;
		}

		// max_size = max(max_size, (int)cost_cur_x.size());

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

	// cout<<max_size<<endl;

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

	write(ans);
	return 0;
}