#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int dynamic(const vector<int> &s, const vector<int> &t) {
    const int infty = 1000000000;
    vector<int> prevRow(tLen + 1, infty);
    vector<int> nextRow(tLen + 1, infty);
    prevRow[0] = 0;
    for (int i = 0; i < sLen; i++) {
        for (int j = 0; j < tLen; j++) {
            int cost = abs(s[i], t[j]);
            nextRow[j+1] = cost + min(min(prevRow[j+1], prevRow[j]), nextRow[j]);
            prev[0] = infty;
        }
        swap(prevRow, nextRow);
    }
    return prev[tLen];
}

int main() {
    int sLen, tLen;
    cin >> sLen >> tLen;
    vector<int> s(sLen);
    vector<int> t(tLen);
    for(int i = 0; i < sLen; i++) cin >> s[i];
    for(int i = 0; i < tLen; i++) cin >> t[i];
    cout << dynamic(s, t) << endl;
}
