#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, vis[100001], dis[100001], freq[100001];
int sd[100001][2], nsd;

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    nsd = 0;
    memset(vis, 0, sizeof(vis));
    while (b > 0) {
      sd[nsd][0] = b;
      sd[nsd++][1] = d;
      vis[b] = 1;
      freq[b]++;
      dis[b] += d;
      b >>= 1;
      d++;
    }
    for (int j = 0; j < nsd; j++) {
      b = sd[j][0]<<1, d = sd[j][1]+1;
      while (b <= maxi && !vis[b]) {
        sd[nsd][0] = b;
        sd[nsd++][1] = d;
        vis[b] = 1;
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, a, idx, ans = 1e9;
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    idx = i;
    f(a, 0);
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first]++;
      tot[p.first] += p.second;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        ans = min(ans, tot[p.first]);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[200001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 2*maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 2*maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= 2*maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b, d));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }
  for (int i = 1; i <= maxi; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[300001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 200000) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 200000) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, vis[100001], dis[100001], freq[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    set<pair<int, int> > sd;
    memset(vis, 0, sizeof(vis));
    while (b > 0) {
      sd.insert(make_pair(b, d));
      vis[b] = 1;
      freq[b]++;
      dis[b] += d;
      b >>= 1;
      d++;
    }
    for (auto p : sd) {
      b = p.first<<1, d = p.second+1;
      while (b <= maxi && !vis[b]) {
        sd.insert(make_pair(b, d));
        vis[b] = 1;
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    common[i].push_back(make_pair(b, 0));
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      b <<= 1;
      d++;
      while (b <= maxi) {
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, a[100001], idx, ans = 1e9, maxi = 0;
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0 || v > maxi) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    idx = i;
    f(a[i], 0);
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first]++;
      tot[p.first] += p.second;
    }
  }
  int vv = -1;
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        if (ans > tot[p.first]) {
          ans = tot[p.first];
          vv = p.first;
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }
  for (int i = 1; i <= maxi; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 100000) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 100000) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, a, idx, ans = 1e9;
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0 || v > 1e5) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    idx = i;
    f(a, 0);
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first]++;
      tot[p.first] += p.second;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        ans = min(ans, tot[p.first]);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, vis[100001];
map<int, int> freq;
map<int, int> dis;

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    set<pair<int, int> > sd;
    memset(vis, 0, sizeof(vis));
    while (b > 0) {
      sd.insert(make_pair(b, d));
      vis[b] = 1;
      freq[b]++;
      dis[b] += d;
      b >>= 1;
      d++;
    }
    for (auto p : sd) {
      b = p.first<<1, d = p.second+1;
      while (b <= maxi && !vis[b]) {
        sd.insert(make_pair(b, d));
        vis[b] = 1;
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (auto p : freq) {
    if (p.second == n) {
      ans = min(ans, dis[p.first]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 2*maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 2*maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 2*maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }
  for (int i = 1; i <= maxi; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 2*maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, idx, ans = 1e9, a[100001], maxi = 0;
map<int, int> m;
map<int, int> freq;

int count(int v) {
  int tot = 0;
  for (int i = 0; i < n; i++) {
    int d = 0, b = a[i];
    while (v%b) {
      b >>= 1;
      d++;
    }
    while (v != b) {
      b <<= 1;
      d++;
    }
    tot += d;
  }
  return tot;
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i];
    set<int> s;
    while (b > 0) {
      s.insert(b);
      freq[b]++;
      b >>= 1;
    }
    for (auto b : s) {
      b <<= 1;
      while (b <= maxi && !s.count(b)) {
        s.insert(b);
        freq[b]++;
        b <<= 1;
      }
    }
  }
  for (auto p : freq) {
    if (p.second == n) {
      ans = min(ans, count(p.first));
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    common[i].push_back(make_pair(b, 1));
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      b <<= 1;
      d++;
      while (b <= maxi) {
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= 2*maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= 2*maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= 2*maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;

int main()
{

    cin>>n;
    int minInd=0;
    int maxInd=0;
    int res=1e7;

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

    int target=arr[minInd];
    while(target<=1e8)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target*=2;
    }

    target=arr[minInd];
     while(target>=1)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target/=2;
    }
    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;

int main()
{

    cin>>n;
    int minInd=0;
    int maxInd=0;
    int res=1e7;

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

    int target=arr[minInd];
    while(target<=arr[maxInd])
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target*=2;
    }

    target=arr[minInd];
     while(target>=1)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target/=2;
    }
    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
unordered_map <int ,int> m1,m2;
unordered_set<int> vis;
void dfs(int num,int steps)
{
    if(num<1 || num>100000)
        return ;
    m1[num]++;
    m2[num]+=steps;
    vis.insert(num);

    if(!vis.count(num/2))
    dfs(num/2,steps+1);

    if(!vis.count(num*2))
    dfs(num*2,steps+1);
}

int main()
{

    cin>>n;

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

    for(int i=0;i<n;i++)
    {
        vis.clear();
        dfs(arr[i],0);
    }
    auto itr= m1.begin();

    int res=1e8;
    while(itr!=m1.end())
    {
        if(itr->second==n )
            res=min(res,m2[itr->first]);
        itr++;
    }

        cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
int Max;
unordered_map <int ,int> m1,m2;
set<int> vis;
void dfs(int num,int steps)
{
    if(num<1 || num>Max)
        return ;
    m1[num]++;
    m2[num]+=steps;
    vis.insert(num);

    if(!vis.count(num/2))
    dfs(num/2,steps+1);

    if(!vis.count(num*2))
    dfs(num*2,steps+1);
}

int main()
{

    //cin>>n;

    n=100000;
    for(int i=0;i<n;i++)
    {
        arr[i]=i;
        //cin>>arr[i];
        Max=max(Max,arr[i]);
    }

    for(int i=0;i<n;i++)
    {
        vis.clear();
        dfs(arr[i],0);
    }
    auto itr= m1.begin();

    int res=1e8;
    while(itr!=m1.end())
    {
        if(itr->second==n )
            res=min(res,m2[itr->first]);
        itr++;
    }

        cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;

int main()
{

    cin>>n;
    int minInd=0;
    int maxInd=0;
    int res=1e7;

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

    int target=arr[minInd];
    while(target<=1e9)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target*=2;
    }

    target=arr[minInd];
     while(target>=1)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

          while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target/=2;
    }
    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;

int main()
{

    cin>>n;
    int minInd=0;
    int maxInd=0;
    int res=0;

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

        int val=arr[i];
        while(val>1)
        {
            val/=2;
            res++;
        }

    }

    int target=arr[minInd];
    while(target<=arr[maxInd])
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            while(val >target)
            {
                val/=2;
                temp++;
            }

         while(val <target)
            {
                val*=2;
                temp++;
            }

         if(val!=target)
         {
             temp=-1;
             break;
         }
        }
        if(temp!=-1)
            res=min(res,temp);

        target*=2;
    }

    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10],m1[100000+10],m2[100000+10];
int n;
int Max;
vector<int> vec;
bool vis[100000+10];
void bfs(int num)
{
    queue <pair<int,int> > q;
    q.push(make_pair(num,0));
    while(!q.empty())
    {
        pair<int,int> cur=q.front();
        q.pop();

        if(vis[cur.first]) continue;
        if(cur.first<1 || cur.first>Max)
            continue;

        vis[cur.first]=1;
        vec.push_back(cur.first);

        m1[cur.first]++;
        m2[cur.first]+=cur.second;
        q.push(make_pair(cur.first/2,cur.second+1));
        q.push(make_pair(cur.first*2,cur.second+1));

    }

}

int main()
{

    cin>>n;

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

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<vec.size();j++)
            vis[vec[j]]=0;
        vec.clear();
        bfs(arr[i]);
    }

    int res=1e8;
    for(int i=1;i<=1e5;i++)
    {
        if(m1[i]==n )
            res=min(res,m2[i]);
    }

    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
int Max;
unordered_map <int ,int> m1,m2;
unordered_set<int> vis;
void dfs(int num,int steps)
{
    if(num<1 || num>Max)
        return ;
    m1[num]++;
    m2[num]+=steps;
    vis.insert(num);

    if(!vis.count(num/2))
    dfs(num/2,steps+1);

    if(!vis.count(num*2))
    dfs(num*2,steps+1);
}

int main()
{

    cin>>n;

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

    for(int i=0;i<n;i++)
    {
        vis.clear();
        dfs(arr[i],0);
    }
    auto itr= m1.begin();

    int res=1e8;
    while(itr!=m1.end())
    {
        if(itr->second==n )
            res=min(res,m2[itr->first]);
        itr++;
    }

        cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
map <int ,pair<int,int> > m;
unordered_set<int> vis;
void dfs(int num,int steps)
{
	if(vis.count(num)) return ;
	if(num<1 || num>100000)
		return ;
	m[num].first++;
	m[num].second+=steps;
	vis.insert(num);
	dfs(num/2,steps+1);
	dfs(num*2,steps+1);
}

int main()
{

	cin>>n;

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

	for(int i=0;i<n;i++)
	{
		vis.clear();
	dfs(arr[i],0);
	}
	auto itr= m.begin();

	int res=1e8;
	while(itr!=m.end())
	{
		if(itr->second.first==n)
			res=min(res,itr->second.second);
		itr++;
	}

		cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
int Max;
unordered_map <int ,int> m1,m2;
set<int> vis;
void dfs(int num,int steps)
{
    if(num<1 || num>Max)
        return ;
    m1[num]++;
    m2[num]+=steps;
    vis.insert(num);

    if(!vis.count(num/2))
    dfs(num/2,steps+1);

    if(!vis.count(num*2))
    dfs(num*2,steps+1);
}

int main()
{

    cin>>n;

   // n=100000;
    for(int i=0;i<n;i++)
    {
        //arr[i]=i;
        cin>>arr[i];
        Max=max(Max,arr[i]);
    }

    for(int i=0;i<n;i++)
    {
        vis.clear();
        dfs(arr[i],0);
    }
    auto itr= m1.begin();

    int res=1e8;
    while(itr!=m1.end())
    {
        if(itr->second==n )
            res=min(res,m2[itr->first]);
        itr++;
    }

        cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;

int main()
{

    cin>>n;
    int minInd=0;
    int maxInd=0;
    int res=1e7;

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

    int target=arr[minInd];
    while(target<=arr[maxInd])
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            if(val>target)
            {
                while(val >target)
                {
                    val/=2;
                    temp++;
                }
            }
            else
            {
                while(val <target)
                {
                    val*=2;
                    temp++;
                }
            }
            if(val!=target)
            {
                temp=-1;
                break;
            }

        }
        if(temp!=-1)
        {
            res=min(res,temp);

        }

        target*=2;
    }

    target=arr[minInd];
    while(target>=1)
    {
        int temp=0;
        for(int i=0;i<n;i++)
        {
            int val=arr[i];
            if(val>target)
            {
                while(val >target)
                {
                    val/=2;
                    temp++;
                }
            }
            else
            {
                while(val <target)
                {
                    val*=2;
                    temp++;
                }
            }

            if(val!=target)
            {
                temp=-1;
                break;
            }
        }
        if(temp!=-1)
            res=min(res,temp);

        target/=2;
    }
    cout<<res<<endl;
}


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <complex>
#include <sstream>
#include <utility>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define OO (int) 1e9

using namespace std;

long long gcd(long long u, long long v) {return (v != 0)?gcd(v, u%v):u;}

int arr[100000+10];
int n;
unordered_map <int ,int> m1,m2;

unordered_set<int> vis;
void dfs(int num,int steps)
{
    if(vis.count(num)) return ;
    if(num<1 || num>100000)
        return ;
    m1[num]++;
    m2[num]+=steps;
    vis.insert(num);

    dfs(num/2,steps+1);
    dfs(num*2,steps+1);
}

int main()
{

    cin>>n;

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

    for(int i=0;i<n;i++)
    {
        vis.clear();
        dfs(arr[i],0);
    }
    auto itr= m1.begin();

    int res=1e8;
    while(itr!=m1.end())
    {
        if(itr->second==n )
            res=min(res,m2[itr->first]);
        itr++;
    }

        cout<<res<<endl;
}


#include <cstdio>
#include <queue>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int main(){
	int n,ans=1e9,x,y,temp;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			x=q.front().first;
			y=q.front().second;
			q.pop();
			if (x>=100001) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		for (int i=0;i<=100000;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	}
	printf("%d",ans);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <utility>
#include <iomanip>
#include <set>
#include <numeric>
#include <cassert>
#include <ctime>

#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define bits(a) __builtin_popcount(a)

using namespace std;

int cnt[100005], vis[100005], steps[100005];

int main()
{
  int n, res = INF, x, y;
  scanf("%d", &n);
  for(int i=1;i<=n;i++)
  {
    scanf("%d", &x);
    queue<pair<int, int> > q;
    q.push(mp(x, 0));
    while(!q.empty())
    {
      x = q.front().f;
      y = q.front().s;
      q.pop();
      if(x > 100003) continue;
      if(vis[x] == i) continue;
      vis[x] = i;
      steps[x]+=y;
      cnt[x]++;
      q.push(mp(x * 2, y + 1));
      q.push(mp(x / 2, y + 1));
    }
  }
  for(int i=0;i<=100000;i++)
    if(cnt[i] == n)
      if(res > steps[i])
        res = steps[i];
  printf("%d", res);
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <limits>
#include <utility>
#include <iomanip>
#include <set>
#include <numeric>
#include <cassert>
#include <ctime>

using namespace std;

int cnt[100005], vis[100005], steps[100005];

int main()
{
  int n, res = 2000000000, x, y;
  scanf("%d", &n);
  for(int i=1;i<=n;i++)
  {
    scanf("%d", &x);
    queue<pair<int, int> > q;
    q.push(make_pair(x, 0));
    while(!q.empty())
    {
      x = q.front().first;
      y = q.front().second;
      q.pop();
      if(x > 100003) continue;
      if(vis[x] == i) continue;
      vis[x] = i;
      steps[x]+=y;
      cnt[x]++;
      q.push(make_pair(x * 2, y + 1));
      q.push(make_pair(x / 2, y + 1));
    }
  }
  for(int i=0;i<=100000;i++)
    if(cnt[i] == n)
      if(res > steps[i])
        res = steps[i];
  printf("%d", res);
  return 0;
}


#include <cstdio>
#include <queue>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int main(){
	int n,ans=2000000000,x,y,temp;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			x=q.front().first;
			y=q.front().second;
			q.pop();
			if (x>100003) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		for (int i=0;i<=100000;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	}
	printf("%d",ans);
}


#include <cstdio>
#include <queue>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int main(){
	int n,ans=2000000000,x,y,temp;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			x=q.front().first;
			y=q.front().second;
			q.pop();
			if (x>=100001) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		for (int i=0;i<=100000;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	}
	printf("%d",ans);
}


#include <bits/stdc++.h>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int main(){
	int n,ans=1e9,x,y;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		int temp;
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			x=q.front().first;
			y=q.front().second;
			q.pop();
			if (x>=100005) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		for (int i=0;i<=100000;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	}
	printf("%d",ans);
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+5;
int a[MAX_N];
int b[MAX_N];
int c[MAX_N];
int main(){
	ios_base::sync_with_stdio(0);
	int n,ans=1e9;
	cin>>n;
	for (int i=1;i<=n;++i){
		int temp;
		cin>>temp;
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			int x=q.front().first;
			int y=q.front().second;
			q.pop();
			if (x>=MAX_N) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		ans=1e9;
		for (int i=0;i<MAX_N;++i){
			if (a[i]==n){
				ans=min(ans,c[i]);
			}
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+5;
int a[MAX_N];
int b[MAX_N];
int c[MAX_N];
int main(){
	int n,ans=1e9;
	cin>>n;
	for (int i=1;i<=n;++i){
		int temp;
		cin>>temp;
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			int x=q.front().first;
			int y=q.front().second;
			q.pop();
			if (x>=MAX_N) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		ans=1e9;
		for (int i=0;i<MAX_N;++i){
			if (a[i]==n){
				ans=min(ans,c[i]);
			}
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+5;
int a[MAX_N];
int b[MAX_N];
int c[MAX_N];
int main(){
	ios_base::sync_with_stdio(0);
	int n,ans=1e9;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		int temp;
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			int x=q.front().first;
			int y=q.front().second;
			q.pop();
			if (x>=MAX_N) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}
		for (int i=0;i<MAX_N;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	}
	printf("%d",ans);
}


#include <cstdio>
#include <queue>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int main(){
	int n,ans=1e9,x,y,temp;
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		scanf("%d",&temp);
		queue <pair<int,int> > q;
		q.push(make_pair(temp,0));
		while (!q.empty()){
			x=q.front().first;
			y=q.front().second;
			q.pop();
			if (x>=100001) continue;
			if (b[x]==i) continue;
			b[x]=i;
			c[x]+=y;
			a[x]++;
			q.push(make_pair(x*2,y+1));
			q.push(make_pair(x/2,y+1));
		}

	}
	for (int i=0;i<=100000;++i){
			if (a[i]==n){
				if (ans>c[i]) ans=c[i];
			}
		}
	printf("%d",ans);
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111],cost[1111111];
int vis[1111111];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            //iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        //mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && vis[x*2]!=i) {
                //mm[i][x*2] = d+1;
                cnt[x*2]++;
                cost[x*2]+=d+1;
                vis[x*2]=i;
                q.push(MP(x*2,d+1));
            }
            if(vis[x/2]!=i) {
                //OI(x/2);
                vis[x/2] = i;
                cnt[x/2]++;
                cost[x/2]+=d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    for(int M = 0;M<=1000000;++M) {
        //OI(M);
        //M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(f){
            //OI(M);
            ans = min(ans,cost[M]);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        //bool f = 1;
        REP(i,n){
            //if(mm[i].find(M)==mm[i].end())f = 0;
            c+=mm[i][M];
        }
        //if(f){
            //OI(M);
            ans = min(ans,c);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x <= M && mm[i].count(x*2) == 0) {
                mm[i][x*2] = d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].count(x/2) == 0) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        //OI(M);
        int c = 0;
        bool f = 1;
        REP(i,n){
            if(mm[i].count(M)==0)f = 0;
            else c+=mm[i][M];
        }
        if(f){
            //OI(M);
            ans = min(ans,c);
        }
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        //bool f = 1;
        REP(i,n){
            //if(mm[i].find(M)==mm[i].end())f = 0;
            c+=mm[i][M];
        }
        //if(f){
            //OI(M);
            ans = min(ans,c);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        bool f = 1;
        REP(i,n){
            if(mm[i].find(M)==mm[i].end())f = 0;
            else c+=mm[i][M];
        }
        if(f){
            //OI(M);
            ans = min(ans,c);
        }
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111],cost[1111111];
int vis[1111111];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            //iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    fill(vis,vis+1111111,-1);
    int ans = 1<<28;
    REP(i,n) {
        //mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        vis[a[i]] = i;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && vis[x*2]!=i) {
                //mm[i][x*2] = d+1;
                cnt[x*2]++;
                cost[x*2]+=d+1;
                vis[x*2]=i;
                q.push(MP(x*2,d+1));
            }
            if(vis[x/2]!=i) {
                //OI(x/2);
                vis[x/2] = i;
                cnt[x/2]++;
                cost[x/2]+=d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    for(int M = 0;M<=1000000;++M) {
        //OI(M);
        //M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(f){
            //OI(M);
            ans = min(ans,cost[M]);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            //iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    for(int M = 0;M<=1000000;++M) {
        //OI(M);
        //M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        //bool f = 1;
        REP(i,n){
            //if(mm[i].find(M)==mm[i].end())f = 0;
            c+=mm[i][M];
        }
        //if(f){
            //OI(M);
            ans = min(ans,c);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        //OI(M);
        int c = 0;
        bool f = 1;
        REP(i,n){
            if(mm[i].find(M)==mm[i].end())f = 0;
            else c+=mm[i][M];
        }
        if(f){
            //OI(M);
            ans = min(ans,c);
        }
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111],cost[1111111];
map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            //iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                cost[x*2]+=d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                cost[x/2]+=d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    for(int M = 0;M<=1000000;++M) {
        //OI(M);
        //M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(f){
            //OI(M);
            ans = min(ans,cost[M]);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        //bool f = 1;
        REP(i,n){
            //if(mm[i].find(M)==mm[i].end())f = 0;
            c+=mm[i][M];
        }
        //if(f){
            //OI(M);
            ans = min(ans,c);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
int cnt[1111111];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        //popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        cnt[a[i]]++;
        while(!q.empty()) {
            int x = q.front().FT;
            //cnt[x]++;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                cnt[x*2]++;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                cnt[x/2]++;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    for(int M = 0;M<=1000000;++M) {
        //OI(M);
        //M = itr->FT;
        if(cnt[M]!=n)continue;
        //if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        //bool f = 1;
        REP(i,n){
            //if(mm[i].find(M)==mm[i].end())f = 0;
            c+=mm[i][M];
        }
        //if(f){
            //OI(M);
            ans = min(ans,c);
        //}
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        //OI(M);
        int c = 0;
        bool f = 1;
        REP(i,n){
            if(mm[i].find(M)==mm[i].end())f = 0;
            else c+=mm[i][M];
        }
        if(f){
            //OI(M);
            ans = min(ans,c);
        }
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#define REP(i,n) for(int (i)=0;i<(int)(n);++(i))
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define PB push_back
#define FT first
#define SD second
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define OIII(x,y,z) printf("%d %d %d\n",(x),(y),(z))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define OI(x) printf("%d\n",(x))
#define OL(x) cout<<(x)<<endl
#define OLL(x,y) cout<<(x)<<" "<<(y)<<endl
#define OLLL(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define RS(s) scanf("%s",(s))
#define MP(x,y) make_pair((x),(y))
#define SZ(x) ((int)(x).size())
#define FIN(f) freopen(f,"r",stdin)
#define FOUT(f) freopen(f,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int,int> PII;
const int MOD = 1e9+7;
const int maxn = 111111;
int a[maxn];
unordered_map<int,int> mm[maxn];
int main(int argc, char const *argv[])
{
    int n;
    RI(n);
    int M = 0,iid;
    int popm = 60;
    REP(i,n){
        RI(a[i]);
        if(a[i]>M) {
            M = a[i];
            iid = i;
        }
        popm = min(popm,__builtin_popcount(a[i]));
    }
    int ans = 1<<28;
    REP(i,n) {
        mm[i][a[i]] = 0;
        queue<PII> q;
        q.push(MP(a[i],0));
        while(!q.empty()) {
            int x = q.front().FT;
            int d = q.front().SD;
            q.pop();
            //int d = mm[i][x];
            //if(mm[i].count(x)>0)continue;
            if(x*2 <= M && mm[i].find(x*2) == mm[i].end()) {
                mm[i][x*2] = d+1;
                q.push(MP(x*2,d+1));
            }
            if(mm[i].find(x/2) == mm[i].end()) {
                //OI(x/2);
                mm[i][x/2] = d+1;
                q.push(MP(x/2,d+1));
            }
        }
    }
    //OI(mm[1][0]);
    //M*=2;
    //OI(iid);
    REPIT(mm[iid],itr) {
        //OI(M);
        M = itr->FT;
        if(__builtin_popcount(M)>popm)continue;
        //OI(M);
        int c = 0;
        bool f = 1;
        REP(i,n){
            if(mm[i].find(M)==mm[i].end())f = 0;
            else c+=mm[i][M];
        }
        if(f){
            //OI(M);
            ans = min(ans,c);
        }
        //if(M==0)break;
        //M/=2;
    }
    OI(ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define popb pop_back
#define gcd __gcd
#define sz(a) ((int)(a.size()))

typedef pair<int,int> inin;

ll i,j,k,l,fx[1000000],m,b[100000][40],n,c,e,r,lg[200000],sst[100000]={0},t,h,mx,mn,mx1,mx2,mn1,mn2,x,y,x2,y2,i1,j2,i2;
vector<int>v[10000];
set<int>sas;
set<int>::iterator it;
ll a[10000000];
pair<int,int>p[1000000];
inin pp;
ll s[100000];

int main()
{
	//freopen("wow.in","r",stdin);
	//freopen("wow.out","w+",stdout);
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
ll    mxl=0;
    for(i=0; i<n; i++)
    {
        j=0;
       ll d=a[i];
        while(d>0)
        {
            b[i][j]=d&1;
            d>>=1;
            j++;
        }
        lg[i]=j;
        mxl=max(mxl,j);
        reverse(b[i],b[i]+j);
    }
ll    pas=0;
    bool ee=0;
    int mn=-1;
    for(i=0; i<mxl; i++)
    {
        l=b[0][i];
        e=1;
      //  cout<<l<<endl;
        for(j=0; j<n; j++)
        {
            if(i>lg[j])break;
            if(l!=b[j][i])
                e=0;
        }
     //   cout<<e<<endl;
        if(!e)
            mn=i-1;
    }
    if(mn==-1)
        mn=mxl-1;
   // cout<<"mn"<<mn<<endl;
    ll ppas=1000000000000000;
    for(i=0; i<=mxl; i++)
    {
        pas=0;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j]){pas+=abs(lg[j]-i-1); if(sst[j])pas+=(lg[j]-sst[j])*2; continue;}
            if(i>=mn&&!sst[j]&&b[j][i])
                sst[j]=i;
            if(sst[j])
                {pas+=lg[j]-1-i+(i-sst[j]+1)*2;}
            else pas+=abs(lg[j]-i-1);
        }
     //   cout<<i<<" "<<pas<<endl;
        ppas=min(pas,ppas);
    }
    cout<<ppas<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define popb pop_back
#define gcd __gcd
#define sz(a) ((ll)(a.size()))

typedef pair<ll,ll> inin;

ll i,j,k,l,fx[1000000],m,b[200000][40],n,c,e,r,lg[200000],sst[100000]={0},t,h,mx,mn,mx1,mx2,mn1,mn2,x,y,x2,y2,i1,j2,i2;
ll a[10000000];
inin pp;

int main()
{
	//freopen("wow.in","r",stdin);
	//freopen("wow.out","w+",stdout);
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
ll    mxl=0;
    for(i=0; i<n; i++)
    {
        j=0;
       ll d=a[i];
        while(d>0)
        {
            b[i][j]=d&1;
            d>>=1;
            j++;
        }
        lg[i]=j;
        mxl=max(mxl,j);
        reverse(b[i],b[i]+j);
    }
ll    pas=0;
    ll mn=-1;
    for(i=0; i<mxl; i++)
    {
        //l=b[0][i];
        l=-1;
        e=1;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j])continue;
            if(l==-1)l=b[j][i];
            if(l!=b[j][i])
                e=0;
        }
        if(!e)
            mn=i-1;
    }
    if(mn==-1)
        mn=mxl-1;
   // cout<<"mn"<<mn<<endl;
    ll ppas=100000000000000000000;
    for(i=0; i<=mxl; i++)
    {
        pas=0;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j]){pas+=abs(lg[j]-i-1); if(sst[j])pas+=(lg[j]-sst[j])*2; continue;}
            if(i>=mn&&!sst[j]&&b[j][i])
                sst[j]=i;
            if(sst[j])
                {pas+=lg[j]-1-i+(i-sst[j]+1)*2;}
            else pas+=abs(lg[j]-i-1);
        }
        //cout<<i<<" "<<pas<<endl;
        ppas=min(pas,ppas);
    }
    cout<<ppas<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define popb pop_back
#define gcd __gcd
#define sz(a) ((ll)(a.size()))

typedef pair<ll,ll> inin;

ll i,j,k,l,m,b[300000][40],n,c,e,r,lg[300000],sst[300000]={0},t,h,mx,mn,mx1,mx2,mn1,mn2,x,y,x2,y2,i1,j2,i2;
ll a[10000000];
inin pp;

int main()
{
	//freopen("wow.in","r",stdin);
	//freopen("wow.out","w+",stdout);
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
ll    mxl=0;
    sort(a,a+n);
    reverse(a,a+n);
    for(i=0; i<n; i++)
    {
        j=0;
       ll d=a[i];
        while(d>0)
        {
            b[i][j]=d&1;
            d>>=1;
            j++;
        }
        lg[i]=j;
        mxl=max(mxl,j);
        reverse(b[i],b[i]+j);
    }
ll    pas=0;
    ll mn=-1;
    for(i=0; i<mxl; i++)
    {
        //l=b[0][i];
        l=-1;
        e=1;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j]){e=0;continue;}
            if(l==-1)l=b[j][i];
            if(l!=b[j][i])
                e=0;
        }
        if(!e)
           {mn=i;break;}
    }
    if(mn==-1)
        mn=mxl-1;
    //cout<<"mn"<<mn<<endl;
    ll ppas=100000000000000000000;
    for(i=0; i<=mxl; i++)
    {
        pas=0;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j]){pas+=abs(lg[j]-i-1); if(sst[j])pas+=(lg[j]-sst[j])*2; continue;}
            if(i>=mn&&!sst[j]&&b[j][i])
                sst[j]=i;
            if(sst[j])
                {pas+=lg[j]-1-i+(i-sst[j]+1)*2;}
            else pas+=abs(lg[j]-i-1);
        }
       // cout<<i<<" "<<pas<<endl;
        ppas=min(pas,ppas);
    }
    cout<<ppas<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define popb pop_back
#define gcd __gcd
#define sz(a) ((ll)(a.size()))

typedef pair<ll,ll> inin;

ll i,j,k,l,fx[1000000],m,b[200000][40],n,c,e,r,lg[200000],sst[200000]={0},t,h,mx,mn,mx1,mx2,mn1,mn2,x,y,x2,y2,i1,j2,i2;
ll a[10000000];
inin pp;

int main()
{
	//freopen("wow.in","r",stdin);
	//freopen("wow.out","w+",stdout);
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
ll    mxl=0;
    sort(a,a+n);
    reverse(a,a+n);
    for(i=0; i<n; i++)
    {
        j=0;
       ll d=a[i];
        while(d>0)
        {
            b[i][j]=d&1;
            d>>=1;
            j++;
        }
        lg[i]=j;
        mxl=max(mxl,j);
        reverse(b[i],b[i]+j);
//        for(j=0; j<lg[i]; j++)
//            cout<<b[i][j]<<" ";
//        cout<<endl;
    }
ll    pas=0;
    ll mn=-1;
    for(i=0; i<mxl; i++)
    {
        //l=b[0][i];
        l=-1;
        e=1;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j])continue;
            if(l==-1)l=b[j][i];
            if(l!=b[j][i])
                e=0;
        }
        if(!e)
           {mn=i-1;break;}
    }
    if(mn==-1)
        mn=mxl-1;
    //cout<<"mn"<<mn<<endl;
    ll ppas=100000000000000000000;
    for(i=0; i<=mxl; i++)
    {
        pas=0;
        for(j=0; j<n; j++)
        {
            if(i>=lg[j]){pas+=abs(lg[j]-i-1); if(sst[j])pas+=(lg[j]-sst[j])*2; continue;}
            if(i>=mn&&!sst[j]&&b[j][i])
                sst[j]=i;
            if(sst[j])
                {pas+=lg[j]-1-i+(i-sst[j]+1)*2;}
            else pas+=abs(lg[j]-i-1);
        }
       // cout<<i<<" "<<pas<<endl;
        ppas=min(pas,ppas);
    }
    cout<<ppas<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 10000
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
map<int, int> mp;
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = 1;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;
        int a[n+1]={0}, cs[n+1]={0};
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }
        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 9000
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
map<int, int> mp;
int a[MAX], cs[MAX];
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = 1;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;

        for(int i=0; i<MAX; i++){
            a[i] = 0;
            cs[i] = 0;
        }
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }

        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 9000
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
bool mp[64000001];
int a[MAX], cs[MAX];
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = true;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        int n, cnt = 0;
        cin>>n;

        for(int i=0; i<MAX; i++){
            a[i] = 0;
            cs[i] = 0;
        }
        //for(int i=0; i<64000001; i++) mp[i] = false;
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }

        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 8001
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
map<int, bool> mp;
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = true;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;
        int a[n+1]={0}, cs[n+1]={0};
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }
        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 10000
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
map<ll, ll> mp;
void solve(ll a[], ll n){
    for(ll i=2; i<=n; i++){
        for(ll j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = 1;
        }
    }
}

int main(){
    fastio;
    ll t;
    cin>>t;
    while(t--){
        mp.clear();
        ll n, cnt = 0;
        cin>>n;
        ll a[n+1]={0}, cs[n+1]={0};
        for(ll i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }
        solve(cs, n);
        for(ll i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 10000
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
map<int, int> mp;
int a[MAX], cs[MAX];
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = 1;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;

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

        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 8001
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
vector<int> mp;
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp.pb(a[i]-a[j]);
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;
        int a[n+1]={0}, cs[n+1]={0};
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }
        solve(cs, n);
        for(int i=1; i<=n; i++){
            for(int j : mp){
                if(j==a[i]) {
                    cnt++;
                    break;
                }
            }
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 8001
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);

bool solve(int a[], int n, int key){
    int cnt = 0;
    for(int i=2; i<=n; i++){
        int s=0,e=i, fix = i, mid;
        while(s<=e){
            mid = (s+e)/2;
            int val = abs(a[mid]-a[fix]);
            if(val>key) s = mid+1;
            else if(val<key) e = mid-1;
            else{
                if(abs(mid-fix)>1) return true;
                break;
            }
        }
    }
    return false;
}

int main(){
    //fastio;
    int t;
    cin>>t;
    while(t--){
        int n, cnt = 0;
        cin>>n;
        int a[n+1]={0}, cs[n+1]={0}, mark[n+1] = {0};
        for(int i=1; i<=n; i++){
            scanf("%d", &a[i]);
            //cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }

        for(int i=1; i<=n; i++){
            if(mark[a[i]]){
                if(mark[a[i]]==1) cnt++;
            }else{
                if(solve(cs, n, a[i])){
                    cnt++;
                    mark[a[i]] = 1;
                }else
                    mark[a[i]] = 2;
            }
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 8001
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
//bool mp[64000001];
map<int, bool> mp;
int a[MAX], cs[MAX];
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp[a[i]-a[j]] = true;
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        int n, cnt = 0;
        cin>>n;

        for(int i=0; i<MAX; i++){
            a[i] = 0;
            cs[i] = 0;
        }
        //for(int i=0; i<64000001; i++) mp[i] = false;
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }

        solve(cs, n);
        for(int i=1; i<=n; i++){
            if(mp[a[i]]) cnt++;
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pb push_back
#define bug cout<<"BUG"
#define MAX 8001
#define nln cout<<"\n"
#define File_Input freopen("input.txt", "r", stdin)
#define File_Output freopen("output.txt", "w", stdout);
vector<int> mp;
void solve(int a[], int n){
    for(int i=2; i<=n; i++){
        for(int j=0; j<=i-2; j++){
            mp.pb(a[i]-a[j]);
        }
    }
}

int main(){
    fastio;
    int t;
    cin>>t;
    while(t--){
        mp.clear();
        mp.shrink_to_fit();
        int n, cnt = 0;
        cin>>n;
        int a[n+1]={0}, cs[n+1]={0};
        for(int i=1; i<=n; i++){
            cin>>a[i];
            cs[i] += a[i]+cs[i-1];
        }
        solve(cs, n);
        for(int i=1; i<=n; i++){
            for(int j : mp){
                if(j==a[i]) {
                    cnt++;
                    break;
                }
            }
        }

        cout<<cnt;nln;

    }
}


#include<bits/stdc++.h>

#define NN 8100

using namespace std;

typedef long long INT;

int vst[NN],flag[NN],s[NN];
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		memset(vst,0,sizeof vst);
		memset(flag,0,sizeof flag);
		memset(s,0,sizeof s);
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			vst[a[i]]=1;
			s[i]=s[i-1]+a[i];
		}
		for(int i=1;i<n;i++){
			for(int j=i+1;j<=n;j++){
				int sum=s[j]-s[i-1];
				if(vst[sum])flag[sum]=1;
			}
		}
		int cnt=0;
		for(int i=1;i<=n;i++)if(flag[a[i]])cnt++;
		cout<<cnt<<endl;

	}

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
using namespace std;

int a[NN], sum[NN];
int vst[NN];
bool flag[NN];
typedef long long INT;
typedef pair<int, int> pii;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n;
		memset(vst,0, sizeof(vst));
		memset(flag,0, sizeof(flag));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), vst[a[i]]++;
		sum[1] = a[1];
		for(int i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i];
		int cnt = 0;
		for(int i = 0; i <= n - 2; i++){
			for(int j = i + 2; j <= n; j++) {
				if(sum[j] - sum[i] > 8001) continue;
				if(vst[sum[j] - sum[i]] && flag[sum[j] - sum[i]] == 0) cnt += vst[sum[j] - sum[i]], flag[sum[j] - sum[i]] = 1;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=8080;

int a[NN];
map<int,int> mp;

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

	int t;
	int n;
	cin>>t;
	while(t--){
		mp.clear();
		cin>>n;
		for(int i=1;i<=n;i++)scanf("%d",a+i);
		for(int i=1;i<=n;i++){
			int sum=a[i];
			for(int j=i+1;j<=n;j++)sum+=a[j],mp[sum]++;
		}
		int ans=0;
		for(int i=1;i<=n;i++)if(mp[a[i]])ans++,mp[a[i]]--;
		cout<<ans<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=8080;

int a[NN];
map<int,int> mp;

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

	int t;
	int n;
	cin>>t;
	while(t--){
		mp.clear();
		cin>>n;
		for(int i=1;i<=n;i++)scanf("%d",a+i);
		for(int i=1;i<=n;i++){
			int sum=a[i];
			for(int j=i+1;j<=n;j++){if(sum>NN)break;sum+=a[j],mp[sum]++;}
		}
		int ans=0;
		for(int i=1;i<=n;i++)if(mp[a[i]])ans++,mp[a[i]]--;
		cout<<ans<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
using namespace std;

int a[NN], sum[NN];
map<int, int> vst;
typedef long long INT;
typedef pair<int, int> pii;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), vst[a[i]] = 0;;
		sum[1] = a[1];
		for(int i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i];
		for(int i = 0; i <= n - 2; i++){
			for(int j = i + 2; j <= n; j++) vst[sum[j] - sum[i]] = 1;
		}
		int cnt = 0;
		for(int i = 1; i <= n; i++) if(vst[a[i]]) cnt++;
		printf("%d\n", cnt);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NN=8080;

int a[NN];
map<int,int> mp;

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

	int t;
	int n;
	cin>>t;
	while(t--){
		mp.clear();
		cin>>n;
		for(int i=1;i<=n;i++)scanf("%d",a+i);
		for(int i=1;i<=n;i++){
			int sum=a[i];
			for(int j=i+1;j<=n;j++){if(sum>NN)break;sum+=a[j],mp[sum]++;}
		}
		int ans=0;
		for(int i=1;i<=n;i++)if(mp[a[i]])ans++;
		cout<<ans<<endl;
	}

	return 0;
}