#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using vi64 = vector<i64>;
using u64 = uint64_t;

#define rep(i, s, n) for (i64 i = (s); i < (n); ++i)

void run() {
  i64 n, m;
  cin >> n >> m;
  vi64 hs(n), ws(m);
  rep(i, 0, n) { cin >> hs[i]; }
  rep(i, 0, m) { cin >> ws[i]; }
  sort(begin(hs), end(hs));
  sort(begin(ws), end(ws));
  hs.push_back(numeric_limits<i64>::max());
  vi64 rsum(n + 1);
  rsum[n] = 0;
  for (i64 i{n - 2}; - 1 < i; i -= 2) {
    rsum[i] = hs[i + 1] - hs[i] + rsum[i + 2];
  }
  i64 ret{numeric_limits<i64>::max()}, iw{0}, lsum{0};
  for (i64 ih{0}; ih < n; ih += 2) {
    i64 cut_min{numeric_limits<i64>::max()};
    for (; iw < m; ++iw) {
      i64 cut{lsum + abs(hs[ih] - ws[iw]) + rsum[ih + 1]};
      if (cut < cut_min) {
        cut_min = cut;
      } else if (cut_min < cut) {
        --iw;
        break;
      }
    }
    iw = min(iw, m - 1);
    ret = min(ret, cut_min);
    lsum += hs[ih + 1] - hs[ih];
  }
  cout << ret << endl;
}

int main() {
  cin.tie(nullptr);
  cout.precision(std::numeric_limits<double>::max_digits10);
  run();
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,0,1,-1,-1};
const ll dx[8]={0,1,0,-1,0,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

int main(){
  ll n;cin >> n;
  vl a(n),b(n),c(n);
  rep(i,n){
    cin >> a[i];a[i]--;
  }
  rep(i,n){
    cin >> b[i];b[i]--;
  }
  rep(i,n)cin >> c[i],c[i]--;
  rep(i,n)c[i]=b[c[i]];
  map<ll,ll> mp;
  rep(i,n)mp[a[i]]++;
  ll ans=0;
  rep(i,n){
    ans+=mp[c[i]];
  }
  cout << ans <<endl;
}
