#include <stdio.h>

#include <algorithm>
#include <vector>

using namespace std;

#define cm (1 << 17)
char cn[cm], *ci = cn + cm, ct;

inline char getcha() {
  if (ci - cn == cm) {
    fread(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}

inline int getint() {
  int A = 0;
  while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
  return A;
}

inline int up(const void* a, const void* b) {
  return *(int*)a < *(int*)b ? -1 : *(int*)a > *(int*)b ? 1 : 0;
}
inline void sortup(int* a, int n) {
  qsort(a, n, sizeof(int), up);
}

#define mxN 200005
int h[mxN], w[mxN];
long long sum_l[mxN], sum_r[mxN];

int main() {
  int n = getint(), m = getint(), pos = 0;
  for (int i = 0; i < n; i++) {
    h[i] = getint();
  }
  for (int i = 0; i < m; i++) {
    w[i] = getint();
  }
  sortup(h, n);
  sortup(w, m);
  sum_l[0] = sum_r[0] = 0;
  for (int i = 0; i < n / 2; i++) {
    sum_l[i + 1] = sum_l[i] + h[2 * i + 1] - h[2 * i];
    sum_r[i + 1] = sum_r[i] + h[2 * i + 2] - h[2 * i + 1];
  }
  long long ans = 1LL << 60, right = sum_r[n / 2];
  for (int i = 0; i < m; i++) {
    while (pos < n && h[pos] < w[i]) pos++;
    ans = min(ans, sum_l[pos / 2] + right - sum_r[pos / 2] +
                       abs(h[pos - pos % 2] - w[i]));
  }
  printf("%lld", ans);
}
#include <bits/stdc++.h>
using namespace std;

template <int mod = (int)(1e9 + 7)>
struct ModInt {
  int x;
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y)
      : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  constexpr ModInt &operator+=(const ModInt &p) noexcept {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &p) noexcept {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &p) noexcept {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &p) noexcept {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt &p) const noexcept {
    return ModInt(*this) += p;
  }
  constexpr ModInt operator-(const ModInt &p) const noexcept {
    return ModInt(*this) -= p;
  }
  constexpr ModInt operator*(const ModInt &p) const noexcept {
    return ModInt(*this) *= p;
  }
  constexpr ModInt operator/(const ModInt &p) const noexcept {
    return ModInt(*this) /= p;
  }
  constexpr bool operator==(const ModInt &p) const noexcept { return x == p.x; }
  constexpr bool operator!=(const ModInt &p) const noexcept { return x != p.x; }
  constexpr ModInt inverse() const noexcept {
    int a = x, b = mod, u = 1, v = 0, t = 0;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t n) const {
    ModInt res(1), mul(x);
    while (n) {
      if (n & 1) res *= mul;
      mul *= mul;
      n >>= 1;
    }
    return res;
  }
  friend constexpr ostream &operator<<(ostream &os, const ModInt &p) noexcept {
    return os << p.x;
  }
  friend constexpr istream &operator>>(istream &is, ModInt &a) noexcept {
    int64_t t = 0;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  constexpr int get_mod() { return mod; }
};
using mint = ModInt<>;

int n;
vector<long long> b;
vector<vector<mint>> dp;

mint solve();

int main() {
  cin >> n;
  b.assign(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  cout << solve() << endl;
  return 0;
}

mint solve() {
  mint res = 0;
  dp.assign(n + 1, vector<mint>(n + 1, 0));
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = n; j >= 1; --j) {
      mint now = dp[j - 1][b[i] % j];
      if (j != n) dp[j][b[i] % (j + 1)] += now;
      if (i == n) res += now;
    }
  }
  return res;
}