#include <bits/stdc++.h>

using namespace std;

const int MOD = 1077563119;
int n, m;
bool edge[20][20];

int chromaticNumber(const vector<int> &g) {
    int n = g.size();
    if (n == 0) return 0;
    //randomly choose a large prime
    const int modulo = 1077563119;
    int all = 1 << n;
    vector<int> ind(all), s(all);
    for (int i = 0; i < all; i ++) s[i] = ((n - __builtin_popcount(i)) & 1 ? -1 : 1);
    ind[0] = 1;
    for (int i = 1; i < all; i ++) {
        int ctz = __builtin_ctz(i);
        ind[i] = ind[i - (1 << ctz)] + ind[(i - (1 << ctz)) & ~g[ctz]];
        if (ind[i] >= modulo) ind[i] -= modulo;
    }
    //compute the chromatic number (= \sum (-1)^{n - |i|} * ind(i)^k)
    for (int k = 1; k < n; k ++) {
        long long sum = 0;
        for (int i = 0; i < all; i ++) {
            long long cur = ((s[i] * (long long) ind[i]) % modulo);
            s[i] = cur;
            sum += cur;
        }
        if (sum % modulo != 0) return k;
    }
    return n;
}

int main() {
#ifdef ACM
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    int u, v;
    while (m--) {
        cin >> u >> v;
        u--, v--;
        edge[u][v] = edge[v][u] = true;
    }
    vector<int> g(n);
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (!edge[i][j]) {
                g[i] += (1 << j);
                g[j] += (1 << i);
            }
    cout << chromaticNumber(g);
}
#include <bits/stdc++.h>
 
#define TRACE(x) cerr << #x << " = " << x << endl
#define pb push_back
#define mp make_pair
#define fi first
#define se second
 
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
 
#define SZ(x) (int)(x).size()
 
using namespace std;
 
typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef vector<int> VI;
 
template<class Num>
Num mabs(Num A){
  if(A < 0) return -A;
  return A;
}
 
int main(){
  int n, m;
  cin >> n >> m;
  VI hei(n);
  VI moje(m);
  
  REP(i, n)
    cin >> hei[i];
  REP(i, m)
    cin >> moje[i];
  
  sort(hei.begin(), hei.end());
  vector<ll> pref_prije(n + 2, 0), suf_nakon(n + 2, 0);
  for(int i = 0; i < n - 1; i += 2){
    pref_prije[i] = (ll)(hei[i + 1] - hei[i]);
    if(i) pref_prije[i] += pref_prije[i - 1];
    pref_prije[i + 1] = pref_prije[i];
    if(i + 2 >= n - 1)
      pref_prije[i + 2] = pref_prije[i];
  }
  
  for(int i = n - 1; i >= 1; i -= 2){
    suf_nakon[i] = (ll)(hei[i] - hei[i - 1]);
    suf_nakon[i] += suf_nakon[i + 1];
    suf_nakon[i - 1] = suf_nakon[i]; 
    if(i - 2 < 1 && i - 2 >= 0)
      suf_nakon[i - 2] = suf_nakon[i];
  }
  
  //REP(i, n)
  //  cout << pref_prije[i] << ' ';
  //cout << endl;
  //REP(i, n)
  //  cout << suf_nakon[i] << ' ';
  //cout << endl;
  //
  //return 0;
  
  ll ans = (ll)2e18;
  
  REP(i, m){
    int val = moje[i];
    int pos = lower_bound(hei.begin(), hei.end(), val) - hei.begin();
  
    ll tmp = 0ll;
    if(pos % 2 == 0){
      //prije ih je parno
      if(pos > 0) tmp = pref_prije[pos - 1];
      if(pos < n) tmp += (ll)(hei[pos] - val); 
      if(pos + 1 <= n) 
        tmp += suf_nakon[pos + 1]; 
    }
    
    else{
      if(pos >= 2) tmp = pref_prije[pos - 2];
      if(pos > 0) tmp += (ll)(val - hei[pos - 1]);
      if(pos <= n)
        tmp += suf_nakon[pos];
    }
    
    //TRACE(tmp);
    
    ans = min(ans, tmp);
  }
  
  cout << ans << '\n';
  
  return 0;
}

