#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#define REP(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
constexpr long long llinf = 1LL << 62;
using ll = long long;

namespace FastIO {
struct Strs {
  char strs[10000 * 4];
  constexpr Strs() : strs() {
    for (int i = 0; i < 10000; ++i) {
      int j = i;
      for (int t = 3; t >= 0; --t) {
        strs[i * 4 + t] = j % 10 + '0';
        j /= 10;
      }
    }
  }
} constexpr strs;
static constexpr size_t buf_size = 1 << 17;
char buf_in[buf_size], buf_out[buf_size];
size_t pt_in = 0, pt_out = 0, tail_in = 0;
inline size_t num_digits(long long x) {
  if (x >= (long long)1e9) {
    if (x >= (long long)1e18) return 19;
    if (x >= (long long)1e17) return 18;
    if (x >= (long long)1e16) return 17;
    if (x >= (long long)1e15) return 16;
    if (x >= (long long)1e14) return 15;
    if (x >= (long long)1e13) return 14;
    if (x >= (long long)1e12) return 13;
    if (x >= (long long)1e11) return 12;
    if (x >= (long long)1e10) return 11;
    return 10;
  } else {
    if (x >= (long long)1e8) return 9;
    if (x >= (long long)1e7) return 8;
    if (x >= (long long)1e6) return 7;
    if (x >= (long long)1e5) return 6;
    if (x >= (long long)1e4) return 5;
    if (x >= (long long)1e3) return 4;
    if (x >= (long long)1e2) return 3;
    if (x >= (long long)1e1) return 2;
    return 1;
  }
}
inline void load() {
  memcpy(buf_in, buf_in + pt_in, tail_in - pt_in);
  size_t width = tail_in - pt_in;
  tail_in = width + fread(buf_in + width, 1, buf_size - width, stdin);
  pt_in = 0;
}
inline void flush() {
  fwrite(buf_out, 1, pt_out, stdout);
  pt_out = 0;
}
inline void scan(char& c) { c = buf_in[pt_in++]; }
template <class T>
inline void scan(T& x) {
  if (pt_in + 32 > tail_in) load();
  char c;
  bool minus = 0;
  x = 0;
  scan(c);
  while (c >= '0') {
    x = x * 10 + (c & 15);
    scan(c);
  }
  if (minus) x = -x;
}
inline void print(char c) { buf_out[pt_out++] = c; }
template <class T>
inline void print(T x) {
  if (pt_out > buf_size - 32) flush();
  if (x < 0) {
    print('-');
    x = -x;
  }
  size_t digits = num_digits(x);
  int i;
  for (i = pt_out + digits - 4; i > (int)pt_out; i -= 4) {
    memcpy(buf_out + i, strs.strs + (x % 10000) * 4, 4);
    x /= 10000;
  }
  memcpy(buf_out + pt_out, strs.strs + x * 4 + (pt_out - i), 4 + i - pt_out);
  pt_out += digits;
}
inline void print(const char* s) {
  if (pt_out > buf_size - 32) flush();
  for (int i = 0; s[i] != 0; ++i) print(s[i]);
}
template <class T>
inline void println(T x) {
  print(x);
  print('\n');
}
struct SetUp {
  SetUp() { load(); }
  ~SetUp() { flush(); }
} setup;
}  // namespace FastIO

using FastIO::print;
using FastIO::println;
using FastIO::scan;

constexpr int mx = 200001;

int main() {
  int N, M;
  scan(N);
  scan(M);
  int H[mx], W[mx];
  REP(i, N) scan(H[i]);
  REP(i, M) scan(W[i]);
  sort(H, H + N);
  sort(W, W + M);
  ll left[N + 1], right[N + 1];
  left[0] = right[N] = 0;
  for (int i = 2; i <= N; i += 2) left[i] = left[i - 2] + H[i - 1] - H[i - 2];
  for (int i = N - 2; i >= 0; i -= 2) right[i] = right[i + 2] + H[i + 1] - H[i];
  int p = 0;
  ll ans = llinf;
  REP(i, M) {
    while (p < N && H[p] <= W[i]) p++;
    if (p % 2 == 0) {
      ans = min(ans, left[p] + right[p + 1] + H[p] - W[i]);
    } else {
      ans = min(ans, left[p - 1] + right[p] + W[i] - H[p - 1]);
    }
  }
  println(ans);
  return 0;
}
#include <bits/stdc++.h>
#include <vector>
using namespace std;

long long mypow(long a, long b) {
    long long powed = 1;
    if (b == 0) { 
        return 1;
    } else {
        for (long i=0; i<b; i++) {
            powed = powed * a;
        }
    return powed;
    }
}

int main() {
    long long N;
    cin >> N;

    // bの最大値を求める
    long long bmax = 0;
    while (true) {
        if (mypow(2, bmax) <= N) {
            bmax = bmax;
        } else {
            break;
        }
        bmax += 1;
    }

    long long apbpc_min = 9223372036854775807;
    // bが大きい方から順番に最少となるa,b,cを試す
    for (long long b=bmax; b >= 0; b--) {
        // aの最大値を求める
        long long amax = floorl(N / mypow(2, b));
        long long apbpc_before;
        long long c = N - amax * mypow(2, b);
        long long apbpc = amax + b + c;
        if (apbpc_min > apbpc) {
            apbpc_min = apbpc;
        }
    }
    cout << apbpc_min << endl;

    return 0;
}