import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author programajor
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            long[] array = new long[n];
            Set<Integer> less = new HashSet<>();
            Set<Integer> more = new HashSet<>();
            List<List<Integer>> reminders = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                reminders.add(new ArrayList<>());
            }
            input(in, n, m, array, reminders);
            fillSets(n, m, less, more, reminders);
            long count = 0;
            for (int lessIndex : less) {
                int remainingElements = n / m - reminders.get(lessIndex).size();
                for (int borrower : more) {
                    if (remainingElements > 0) {
                        int numbersBorrowed = Math.min(reminders.get(borrower).size(), remainingElements);
                        if (numbersBorrowed == reminders.get(borrower).size() - n / m) {
                            more.remove(borrower);
                        }
                        for (int j = 0; j < numbersBorrowed; j++) {
                            reminders.get(lessIndex).add(reminders.get(borrower).get(0));
                            int difference = lessIndex == 0 ? m - lessIndex : lessIndex - borrower;
                            array[reminders.get(borrower).get(0)] += difference;
                            count += difference;
                            reminders.get(borrower).remove(0);
                        }
                        remainingElements -= numbersBorrowed;
                    }
                }
            }
            out.println(count);
            for (long num : array) {
                out.print(num + " ");
            }
        }

        private void fillSets(int n, int m, Set<Integer> less, Set<Integer> more, List<List<Integer>> reminders) {
            for (int i = 0; i < reminders.size(); i++) {
                List<Integer> list = reminders.get(i);
                if (list.size() * m < n) {
                    less.add(i);
                } else if (list.size() * m > n) {
                    more.add(i);
                }
            }
        }

        private void input(InputReader in, int n, int m, long[] array, List<List<Integer>> reminders) {
            for (int i = 0; i < n; i++) {
                long x = in.nextLong();
                array[i] = x;
                reminders.get((int) (x % m)).add(i);
            }
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

long long int n, m;
long long int a[100100] = {0};
long long int b[100100] = {0};
long long int cum_a[100100] = {0};
long long int cum_b[100100] = {0};

long long int bin_search(long long int elem)
{
    long long int l = 0, r = m;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (b[mid] > elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int bin_search2(long long int elem)
{
    long long int l = 0, r = n;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (a[mid] >= elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int sum_a(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < n; ++i)
    {
        if (elem > a[i])
            ans += (elem - a[i]);
    }
    return ans;
}

long long int sum_b(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < m; ++i)
    {
        if (elem < b[i])
            ans += (- elem + b[i]);
    }
    return ans;
}

struct Elem
{
    long long int val, ind;
};

bool operator < (const Elem& a, const Elem& b)
{
    return a.val < b.val;
}

Elem arr[200100] = {0};

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int p, q;
    long long int i, j;
    scanf("%d%d", &p, &q);
    n = p;
    m = q;
    int ind = 0;
    for (i = 0; i < n; ++i)
    {
        int c;
        scanf("%d", &c);
        //c = rand();
        a[i] = (long long int)(c);
        Elem v;
        v.ind = 0;
        v.val = a[i];
        arr[ind++] = v;
    }
    for (i = 0; i < m; ++i)
    {
        int c;
        scanf("%d", &c);
        //c = rand();
        b[i] = (long long int)(c);
        Elem v;
        v.ind = 0;
        v.val = [i];
        arr[ind++] = v;
    }

    sort(a, a + n);
    sort(b, b + m);

    for (i = 1; i < n; ++i)
    {
        cum_a[i] = cum_a[i - 1] + i * (a[i] - a[i - 1]);
    }

    for (i = m - 2; i >= 0; --i)
    {
        cum_b[i] = cum_b[i + 1] + (m - 1 - i) * (b[i + 1] - b[i]);
    }

    long long int ans = 1e9 * 1e9;
    for (i = 0; i < n; ++i)
    {
        long long int index = bin_search(a[i]) - 1;
        long long int r_ans = sum_a(a[i]) + sum_b(a[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[index] - (a[i] - b[index]) * (m - 1 - index) + cum_a[i];
            ans = min(ans, cur_ans);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
        }
        else
        {
            long long int cur_ans = cum_b[0] + cum_a[i] + m * (b[0] - a[i]);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    for (i = 0; i < m; ++i)
    {
        long long int index = bin_search2(b[i]) - 1;
        int r_ans = sum_a(b[i]) + sum_b(b[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[i] + cum_a[index] + (b[i] - a[index]) * (index + 1);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[i];
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

long long int n, m;
long long int a[100100] = {0};
long long int b[100100] = {0};
long long int cum_a[100100] = {0};
long long int cum_b[100100] = {0};

long long int bin_search(long long int elem)
{
    long long int l = 0, r = m;
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (b[mid] > elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int bin_search2(long long int elem)
{
    long long int l = 0, r = n;
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (a[mid] >= elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int sum_a(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < n; ++i)
    {
        if (elem > a[i])
            ans += (elem - a[i]);
    }
    return ans;
}

long long int sum_b(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < m; ++i)
    {
        if (elem < b[i])
            ans += (- elem + b[i]);
    }
    return ans;
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int p, q;
    long long int i, j;
    scanf("%d%d", &p, &q);
    n = p;
    m = q;
    for (int i = 0; i < n; ++i)
    {
        int c;
        scanf("%d", &c);
        a[i] = (long long int)(c);
    }
    for (int i = 0; i < m; ++i)
    {
        int c;
        scanf("%d", &c);
        b[i] = (long long int)(c);
    }

    sort(a, a + n);
    sort(b, b + m);

    for (i = 1; i < n; ++i)
    {
        cum_a[i] = cum_a[i - 1] + i * (a[i] - a[i - 1]);
    }

    for (i = m - 2; i >= 0; --i)
    {
        cum_b[i] = cum_b[i + 1] + (m - 1 - i) * (b[i + 1] - b[i]);
    }

    long long int ans = 1e18;
    for (i = 0; i < n; ++i)
    {
        long long int index = bin_search(a[i]) - 1;
        if (index != -1)
        {
            long long int cur_ans = cum_b[index] - (a[i] - b[index]) * (m - 1 - index) + cum_a[i];
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[0] + cum_a[i] + m * (b[0] - a[i]);
            ans = min(ans, cur_ans);
        }
    }

    for (i = 0; i < m; ++i)
    {
        long long int index = bin_search2(b[i]) - 1;
        if (index != -1)
        {
            long long int cur_ans = cum_b[i] + cum_a[index] + (b[i] - a[index]) * (index + 1);
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[i];
            ans = min(ans, cur_ans);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

long long int n, m;
long long int a[100100] = {0};
long long int b[100100] = {0};
long long int cum_a[100100] = {0};
long long int cum_b[100100] = {0};

long long int bin_search(long long int elem)
{
    long long int l = 0, r = m;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (b[mid] > elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int bin_search2(long long int elem)
{
    long long int l = 0, r = n;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (a[mid] >= elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int sum_a(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < n; ++i)
    {
        if (elem > a[i])
            ans += (elem - a[i]);
    }
    return ans;
}

long long int sum_b(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < m; ++i)
    {
        if (elem < b[i])
            ans += (- elem + b[i]);
    }
    return ans;
}

struct Elem
{
    long long int val, ind;
};

bool operator < (const Elem& a, const Elem& b)
{
    return a.val < b.val;
}

Elem arr[200100] = {0};

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int p, q;
    long long int i, j;
    scanf("%d%d", &p, &q);
    n = p;
    m = q;
    int ind = 0;
    for (i = 0; i < n; ++i)
    {
        int c;
        scanf("%d", &c);
        //c = rand();
        a[i] = (long long int)(c);
        Elem v;
        v.ind = 0;
        v.val = a[i];
        arr[ind++] = v;
    }
    for (i = 0; i < m; ++i)
    {
        int c;
        scanf("%d", &c);
        //c = rand();
        b[i] = (long long int)(c);
        Elem v;
        v.ind = 0;
        v.val = [i];
        arr[ind++] = v;
    }

    sort(a, a + n);
    sort(b, b + m);

    for (i = 1; i < n; ++i)
    {
        cum_a[i] = cum_a[i - 1] + i * (a[i] - a[i - 1]);
    }

    for (i = m - 2; i >= 0; --i)
    {
        cum_b[i] = cum_b[i + 1] + (m - 1 - i) * (b[i + 1] - b[i]);
    }

    long long int ans = 1e18;
    for (i = 0; i < n; ++i)
    {
        long long int index = bin_search(a[i]) - 1;
        long long int r_ans = sum_a(a[i]) + sum_b(a[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[index] - (a[i] - b[index]) * (m - 1 - index) + cum_a[i];
            ans = min(ans, cur_ans);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
        }
        else
        {
            long long int cur_ans = cum_b[0] + cum_a[i] + m * (b[0] - a[i]);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    for (i = 0; i < m; ++i)
    {
        long long int index = bin_search2(b[i]) - 1;
        int r_ans = sum_a(b[i]) + sum_b(b[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[i] + cum_a[index] + (b[i] - a[index]) * (index + 1);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[i];
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

long long int n, m;
long long int a[100100] = {0};
long long int b[100100] = {0};
long long int cum_a[100100] = {0};
long long int cum_b[100100] = {0};

long long int bin_search(long long int elem)
{
    long long int l = 0, r = m;
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (b[mid] > elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int bin_search2(long long int elem)
{
    long long int l = 0, r = n;
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (a[mid] >= elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int p, q;
    long long int i, j;
    scanf("%d%d", &p, &q);
    n = p;
    m = q;
    for (int i = 0; i < n; ++i)
    {
        int c;
        scanf("%d", &c);
        a[i] = (long long int)(c);
    }
    for (int i = 0; i < m; ++i)
    {
        int c;
        scanf("%d", &c);
        b[i] = (long long int)(c);
    }

    sort(a, a + n);
    sort(b, b + m);

    for (i = 1; i < n; ++i)
    {
        cum_a[i] = cum_a[i - 1] + i * (a[i] - a[i - 1]);
    }

    for (i = m - 2; i >= 0; --i)
    {
        cum_b[i] = cum_b[i + 1] + (m - 1 - i) * (b[i + 1] - b[i]);
    }

    long long int ans = 1e9;
    for (i = 0; i < n; ++i)
    {
        long long int index = bin_search(a[i]) - 1;
        if (index != -1)
        {
            long long int cur_ans = cum_b[index] - (a[i] - b[index]) * (m - 1 - index) + cum_a[i];
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[0] + cum_a[i] + m * (b[0] - a[i]);
            ans = min(ans, cur_ans);
        }
    }

    for (i = 0; i < m; ++i)
    {
        long long int index = bin_search2(b[i]) - 1;
        if (index != -1)
        {
            long long int cur_ans = cum_b[i] + cum_a[index] + (b[i] - a[index]) * (index + 1);
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[i];
            ans = min(ans, cur_ans);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

long long int n, m;
long long int a[100100] = {0};
long long int b[100100] = {0};
long long int cum_a[100100] = {0};
long long int cum_b[100100] = {0};

long long int bin_search(long long int elem)
{
    long long int l = 0, r = m;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (b[mid] > elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int bin_search2(long long int elem)
{
    long long int l = 0, r = n;
    while (l < r)
    {
        long long int mid = (l + r) / 2;
        if (a[mid] >= elem)
        {
            r = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    return l;
}

long long int sum_a(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < n; ++i)
    {
        if (elem > a[i])
            ans += (elem - a[i]);
    }
    return ans;
}

long long int sum_b(long long int elem)
{
    long long int ans = 0;
    for (long long int i = 0; i < m; ++i)
    {
        if (elem < b[i])
            ans += (- elem + b[i]);
    }
    return ans;
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    int p, q;
    long long int i, j;
    scanf("%d%d", &p, &q);
    n = p;
    m = q;
    for (i = 0; i < n; ++i)
    {
        int c;
        //scanf("%d", &c);
        c = rand();
        a[i] = (long long int)(c);
    }
    for (i = 0; i < m; ++i)
    {
        int c;
        //scanf("%d", &c);
        c = rand();
        b[i] = (long long int)(c);
    }

    sort(a, a + n);
    sort(b, b + m);

    for (i = 1; i < n; ++i)
    {
        cum_a[i] = cum_a[i - 1] + i * (a[i] - a[i - 1]);
    }

    for (i = m - 2; i >= 0; --i)
    {
        cum_b[i] = cum_b[i + 1] + (m - 1 - i) * (b[i + 1] - b[i]);
    }

    long long int ans = 1e18;
    for (i = 0; i < n; ++i)
    {
        long long int index = bin_search(a[i]) - 1;
        long long int r_ans = sum_a(a[i]) + sum_b(a[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[index] - (a[i] - b[index]) * (m - 1 - index) + cum_a[i];
            ans = min(ans, cur_ans);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
        }
        else
        {
            long long int cur_ans = cum_b[0] + cum_a[i] + m * (b[0] - a[i]);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    for (i = 0; i < m; ++i)
    {
        long long int index = bin_search2(b[i]) - 1;
        int r_ans = sum_a(b[i]) + sum_b(b[i]);
        if (index != -1)
        {
            long long int cur_ans = cum_b[i] + cum_a[index] + (b[i] - a[index]) * (index + 1);
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
        else
        {
            long long int cur_ans = cum_b[i];
            if (cur_ans != r_ans)
            {
                int t = 1;
            }
            ans = min(ans, cur_ans);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <utility>
using namespace std;

int n,m;
vector<__int64> A, SA, B, SB;

__int64 SumA(__int64 x)
{
	int l=0,r=n-1;
	while(l<r)
	{
		int M=(l+r+1)/2;
		if (A[M]<x) l=M;
		else if (A[M]>x) r=M-1; else l=r=M;
	}
	return SA[l]+max<__int64>(0,__int64(x-A[l])*(l+1));
}

__int64 SumB(__int64 x)
{
	int l=0,r=m-1;
	while(l<r)
	{
		int M=(l+r+1)/2;
		if (B[M]<x) r=M-1;
		else if (B[M]>x) l=M; else l=r=M;
	}
	return SB[l]+max<__int64>(0,__int64(B[l]-x)*(l+1));
}

int main()
{
	cin >> n >> m;
	A.assign(n,0);SA.assign(n,0);
	B.assign(m,0);SB.assign(m,0);
	for(int i=0;i<n;++i) cin >> A[i];
	for(int i=0;i<m;++i) cin >> B[i];
	sort(A.begin(),A.end());
	sort(B.begin(),B.end(),greater<__int64>());

	for(int i=1;i<n;++i) SA[i]=SA[i-1]+i*(A[i]-A[i-1]);
	for(int i=1;i<m;++i) SB[i]=SB[i-1]+i*(B[i-1]-B[i]);

	__int64 ret=-1;
	for(int i=0;i<n;++i)
	{
		__int64 tmp = SA[i]+SumB(A[i]);
		if (ret==-1 || tmp<ret) ret=tmp;
	}
	for(int i=0;i<m;++i)
	{
		__int64 tmp = SB[i]+SumA(B[i]);
		if (ret==-1 || tmp<ret) ret=tmp;
	}

	cout << ret;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <utility>
using namespace std;

int n,m;
vector<int> A, SA, B, SB;

__int64 SumA(int x)
{
	int l=0,r=n-1;
	while(l<r)
	{
		int M=(l+r+1)/2;
		if (A[M]<x) l=M;
		else if (A[M]>x) r=M-1; else l=r=M;
	}
	return SA[l]+max(0,(x-A[l])*(l+1));
}

__int64 SumB(int x)
{
	int l=0,r=m-1;
	while(l<r)
	{
		int M=(l+r+1)/2;
		if (B[M]<x) r=M-1;
		else if (B[M]>x) l=M; else l=r=M;
	}
	return SB[l]+max(0,(B[l]-x)*(l+1));
}

int main()
{
	cin >> n >> m;
	A.assign(n,0);SA.assign(n,0);
	B.assign(m,0);SB.assign(m,0);
	for(int i=0;i<n;++i) cin >> A[i];
	for(int i=0;i<m;++i) cin >> B[i];
	sort(A.begin(),A.end());
	sort(B.begin(),B.end(),greater<int>());

	for(int i=1;i<n;++i) SA[i]=SA[i-1]+i*(A[i]-A[i-1]);
	for(int i=1;i<m;++i) SB[i]=SB[i-1]+i*(B[i-1]-B[i]);

	__int64 ret=-1;
	for(int i=0;i<n;++i)
	{
		__int64 tmp = SA[i]+SumB(A[i]);
		if (ret==-1 || tmp<ret) ret=tmp;
	}
	for(int i=0;i<m;++i)
	{
		__int64 tmp = SB[i]+SumA(B[i]);
		if (ret==-1 || tmp<ret) ret=tmp;
	}

	cout << ret;
	return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 1e5+5;
const int MOD = 1000000007;

int arr1[MAX], arr2[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n, m;
    while(cin >> n >> m){
        FOR(i, n)
            cin >> arr1[i];
        FOR(i, m)
            cin >> arr2[i];
        int ans = 0;
        FOR(i, min(n, m))
            if(arr1[i] < arr2[i])
                ans += arr2[i] - arr1[i];
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 1e5+5;
const int MOD = 1000000007;

int arr1[MAX], arr2[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n, m;
    while(cin >> n >> m){
        FOR(i, n)
            cin >> arr1[i];
        FOR(i, m)
            cin >> arr2[i];
        ll ans = 0;
        sort(arr1, arr1+n);
        sort(arr2, arr2+m, greater <int>());
        FOR(i, min(n, m))
            if(arr2[i] > arr1[i])
                ans += arr2[i] - arr1[i];
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define SS stringstream
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 1e5+5;
const int MOD = 1000000007;

int arr1[MAX], arr2[MAX];

int main(){
//  freopen("in.txt", "r", stdin);
    int n, m;
    while(cin >> n >> m){
        FOR(i, n)
            cin >> arr1[i];
        FOR(i, m)
            cin >> arr2[i];
        int ans = 0;
        sort(arr1, arr1+n);
        sort(arr2, arr2+m, greater <int>());
        FOR(i, min(n, m))
            if(arr2[i] > arr1[i])
                ans += arr2[i] - arr1[i];
        cout << ans << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <ctime>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd paur<double, double>
#define fi first
#define se second
#define forn(i,a,b) for( int i = (a); i < (b); i++ )
#define rep(i,n) forn(i,0,n)
#define forever for(;;)

const int MXN = 100100;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

const int mod = 1000000007;

int a [MXN];
int b [MXN];

int main(){
    int n,m;
    scanf("%d%d", &n, &m);

    rep(i,n){
        scanf("%d", &a[i]);
    }
    rep(i,m){
        scanf("%d", &b[i]);
    }

    sort(a,a+n);
    sort(b,b+m);

    int a1 = 0, b1 = m-1;
    ll res = 0, cnta = 1, cntb = 1;
    if(a[a1]>=b[b1]){
        printf("0");
        return 0;
    }

    for(;;){
        while( a1<n-1 && a[a1+1]==a[a1] ) a1++, cnta++;
        while( b1>0 && b[b1-1]==b[b1] ) b1--, cntb++;
        if(cnta<cntb){
            if(a1==n-1 || b[b1]<=a[a1+1]){
                res += (b[b1]-a[a1])*cnta;
                break;
            }
            else res += (a[a1+1]-a[a1])*cnta;
            a[a1] = a[a1+1];
        }
        else{
            if(b1==0 || a[a1]>=b[b1-1]){
                res += (b[b1]-a[a1])*cntb;
                break;
            }
            else res += (b[b1]-b[b1-1])*cntb;
            b[b1] = b[b1-1];
        }
    }
    cout<<res;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

long a[100001], b[100001];
int  main()
{
    long n,m,i,j;
    long long s=0;
    cin>>n>>m;
    for (i=0; i<n; i++)
        cin>>a[i];
    for (i=0; i<m; i++)
        cin>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    j=m;
    for (i=0; i<min(n,m); i++)
    {
        if (a[i]<b[j])
            s+=b[i]-a[j];
        j--;
    }
    cout<<s;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

long a[100001], b[100001];
int  main()
{
	long n,m,i,j;
	long long s=0;
	cin>>n>>m;
	for (i=1; i<=n; i++)
		cin>>a[i];
	for (i=1; i<=m; i++)
		cin>>b[i];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	for (i=1; i<=min(n,m); i++)
	{
		if (a[i]<b[j])
			s+=abs(a[i]-b[j]);
		j--;
	}
	cout<<s;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

long a[100001], b[100001];
int  main()
{
	long n,m,i,j;
	long long s=0;
	cin>>n>>m;
	for (i=1; i<=n; i++)
		cin>>a[i];
	for (i=1; i<=m; i++)
		cin>>b[i];
	sort(a+1,a+n+1);
	sort(b+1,b+m+1);
	for (i=1; i<=min(n,m); i++)
	{
		if (a[i]<b[j])
			s+=b[i]-a[j];
		j--;
	}
	cout<<s;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

long a[100001], b[100001];
int  main()
{
    long n,m,i,j;
    long long s=0;
    cin>>n>>m;
    for (i=0; i<n; i++)
        cin>>a[i];
    for (i=0; i<m; i++)
        cin>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    j=m;
    for (i=0; i<min(n,m); i++)
    {
        j--;
        if (a[i]<b[j])
            s+=b[j]-a[i];
    }
    cout<<s;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

long a[100001], b[100001];
int  main()
{
    long n,m,i,j;
    long long s=0;
    cin>>n>>m;
    for (i=0; i<n; i++)
        cin>>a[i];
    for (i=0; i<m; i++)
        cin>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    j=m;
    for (i=0; i<min(n,m); i++)
    {
        if (a[i]<b[j])
            s+=b[j]-a[i];
        j--;
    }
    cout<<s;
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(ll k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int m = (l + r) / 2;
				ll f1 = count_moves(m);
				ll f2 = count_moves(m + 1);

				if (f1 <= f2) {
					r = m;
				}
				else {
					l = m + 1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(ll k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int m = (l + r) / 2;
				ll f1 = count_moves(m);
				ll f2 = count_moves(m + 1);

				if (f1 < f2) {
					r = m + 1;
				}
				else {
					l = m;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(int k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int d = max((r - l) / 3, 1);
				int m1 = l + d;
				int m2 = r - d;
				ll f1 = count_moves(m1);
				ll f2 = count_moves(m2);

				if (f1 < f2) {
					r = m2;
				}
				else {
					l = m1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(ll k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0] - 1, r = b[0] + 1;
			while (l + 1 < r) {
				int d = max((r - l) / 3, 1);
				int m1 = l + d;
				int m2 = r - d;
				ll f1 = count_moves(m1);
				ll f2 = count_moves(m2);

				if (f1 <= f2) {
					r = m2;
				}
				else {
					l = m1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(int k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + n, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int d = max((r - l) / 3, 1);
				int m1 = l + d;
				int m2 = r - d;
				ll f1 = count_moves(m1);
				ll f2 = count_moves(m2);

				if (f1 < f2) {
					r = m2;
				}
				else {
					l = m1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(ll k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int d = max((r - l) / 3, 1);
				int m1 = l + d;
				int m2 = r - d;
				ll f1 = count_moves(m1);
				ll f2 = count_moves(m2);

				if (f1 <= f2) {
					r = m2;
				}
				else {
					l = m1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(int k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + n, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			ll mn = min(count_moves(l), count_moves(r));
			while (l + 1 < r) {
				int d = max((r - l) / 3, 1);
				int m1 = l + d;
				int m2 = r - d;
				ll f1 = count_moves(m1);
				ll f2 = count_moves(m2);

				if (f1 < f2) {
					r = m2;
					mn = f1;
				}
				else {
					l = m1;
					mn = f2;
				}
			}
			cout << mn << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

int n, m;
int a[N], b[N];

ll count_moves(ll k) {
	ll cnt = 0;
	for (int i = 0; i < m && b[i] > k; i++) {
		cnt += b[i] - k;
	}

	for (int i = 0; i < n && a[i] < k; i++) {
		cnt += k - a[i];
	}
	return cnt;
}

int main() {
#ifdef _DEBUG
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}

		sort(a, a + n);
		sort(b, b + m, greater<int>());

		if (a[0] >= b[0]) {
			cout << 0 << endl;
		}
		else {
			int l = a[0], r = b[0];
			while (l + 1 < r) {
				int m = (l + r) / 2;
				ll f1 = count_moves(m);
				ll f2 = count_moves(m + 1);

				if (f1 < f2) {
					r = m;
				}
				else {
					l = m + 1;
				}
			}
			cout << min(count_moves(l), count_moves(r)) << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

int n, m;
int a[100001];
int b[100001];

long long f(int x)
{
    long long res = 0;
    for (int i = 0; a[i] < x && i < n; ++i)
        res += x - a[i];
    for (int j = 0; b[j] > x && j < m; ++j)
        res += b[j] - x;
    return res;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", a + i);
    for (int j = 0; j < m; ++j)
        scanf("%d", b + j);
    sort(a, a + n);
    sort(b, b + n, greater<int>());
    if (a[0] >= b[0])
        return !printf("0");
    int l = a[0];
    int r = b[0];
    while (r - l > 3)
    {
        int m1 = l + (r - l) / 3;
        int m2 = r - (r - l) / 3;
        if (f(m1) < f(m2))
            l = m1;
        else r = m2;
    }
    long long ans = LLONG_MAX;
    for (int i = l; i <= r; ++i)
        ans = min(ans, f(i));
    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cassert>
using namespace std;

int n, m;
int a[100001];
int b[100001];
long long prefa[100001], suffb[100001];
int mina = 2e9;
int maxb = -2e9;

int bs1(int x)
{
    int l = 0;
    int r = n - 1;
    int res = 0;
    while (l <= r)
    {
        int m = l + r;
        m >>= 1;
        if (a[m] <= x)
            res = m, l = m + 1;
        else r = m - 1;
    }
    return res;
}

int bs2(int x)
{
    int l = 0;
    int r = m - 1;
    int res = m - 1;
    while (l <= r)
    {
        int m = l + r;
        m >>= 1;
        if (b[m] >= x)
            res = m, r = m - 1;
        else l = m + 1;
    }
    return res;
}

long long f(int x)
{
    int q = bs1(x);
    int w = bs2(x);
    return (q + 1) * 1ll * x - prefa[q] + suffb[w] - (m - w) * 1ll * x;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", a + i);
        mina = min(mina, a[i]);
    }
    for (int i = 0; i < m; ++i)
    {
        scanf("%d", b + i);
        maxb = max(maxb, b[i]);
    }
    sort(a, a + n);
    sort(b, b + m);
    for (int i = 0; i < n; ++i)
    {
        prefa[i] = a[i];
        if (i)
            prefa[i] += prefa[i - 1];
    }
    for (int i = m - 1; i > -1; --i)
    {
        suffb[i] = b[i];
        suffb[i] += suffb[i + 1];
    }
    if (mina >= maxb)
        return !printf("0");
    int l = mina;
    int r = maxb;
    while (r - l < 3)
    {
        int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;

        if (f(m1) < f(m2))
            l = m1;
        else
            r = m2;
    }
    long long ans = LLONG_MAX;
    for (int i = l; i <= r; ++i)
        ans = min(ans, f(i));
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

int n, m;
int a[100001];
int b[100001];

long long f(int x)
{
    long long res = 0;
    for (int i = 0; a[i] < x && i < n; ++i)
        res += x - a[i];
    for (int j = 0; b[j] > x && j < m; ++j)
        res += b[j] - x;
    return res;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", a + i);
    for (int j = 0; j < m; ++j)
        scanf("%d", b + j);
    sort(a, a + n);
    sort(b, b + m, greater<int>());
    if (a[0] >= b[0])
        return !printf("0");
    int l = a[0];
    int r = b[0];
    while (r - l > 3)
    {
        int m1 = l + (r - l) / 3;
        int m2 = r - (r - l) / 3;
        if (f(m1) > f(m2))
            l = m1;
        else r = m2;
    }
    long long ans = LLONG_MAX;
    for (int i = l; i <= r; ++i)
        ans = min(ans, f(i));
    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cassert>
using namespace std;

int n, m;
int a[100001];
int b[100001];
long long prefa[100001], suffb[100001];
int mina = 2e9;
int maxb = -2e9;

int bs1(int x)
{
    int l = 0;
    int r = n - 1;
    int res = 0;
    while (l <= r)
    {
        int m = l + r;
        m >>= 1;
        if (a[m] <= x)
            res = m, l = m + 1;
        else r = m - 1;
    }
    return res;
}

int bs2(int x)
{
    int l = 0;
    int r = m - 1;
    int res = m - 1;
    while (l <= r)
    {
        int m = l + r;
        m >>= 1;
        if (b[m] >= x)
            res = m, r = m - 1;
        else l = m + 1;
    }
    return res;
}

long long f(int x)
{
    int q = bs1(x);
    int w = bs2(x);
    return (q + 1) * 1ll * x - prefa[q] + suffb[w] - (m - w) * 1ll * x;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", a + i);
        mina = min(mina, a[i]);
    }
    for (int i = 0; i < m; ++i)
    {
        scanf("%d", b + i);
        maxb = max(maxb, b[i]);
    }
    sort(a, a + n);
    sort(b, b + m);
    for (int i = 0; i < n; ++i)
    {
        prefa[i] = a[i];
        if (i)
            prefa[i] += prefa[i - 1];
    }
    for (int i = m - 1; i > -1; --i)
    {
        suffb[i] = b[i];
        suffb[i] += suffb[i + 1];
    }
    if (mina >= maxb)
        return !printf("0");
    int l = mina;
    int r = maxb;
    while (r - l > 3)
    {
        int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;

        if (f(m1) > f(m2))
            l = m1;
        else
            r = m2;
    }
    long long ans = LLONG_MAX;
    for (int i = l; i <= r; ++i)
        ans = min(ans, f(i));
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int a[100002];
int b[100002];

ll a_sums[100002];
ll b_sums[100002];

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
  {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < m; ++i)
  {
    scanf("%d", &b[i]);
  }

  sort(a, a + n);
  sort(b, b + m);

  for (int i = 1; i < n; ++i)
  {
    a_sums[i] = ((ll)a[i] - a[i-1]) * i + a_sums[i - 1];
  }

  for (int j = m -2; j >= 0; --j)
  {
    b_sums[j] = ((ll)b[j + 1] - b[j]) * (m - j - 1) + b_sums[j + 1];
  }

  ll best = -1;

  for (int i = 0; i < n; ++i)
  {
    ll cur_res = a_sums[i];

    int * p = upper_bound(b, b + m, a[i]);
    int count = (b + m) - p;

    if (count > 0)
    {
      cur_res += b_sums[m - count];
      if (*p > a[i])
        cur_res += (*p - a[i]) * (ll)count;
    }

    if (best == -1 || cur_res < best)
      best = cur_res;
  }

  printf("%I64d\n", best);

  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int a[100002];
int b[100002];
int alls[200002];

ll a_sums[100002];
ll b_sums[100002];

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
  {
    scanf("%d", &a[i]);
    alls[i] = a[i];
  }

  for (int i = 0; i < m; ++i)
  {
    scanf("%d", &b[i]);
    alls[n + i] = b[i];
  }

  sort(a, a + n);
  sort(b, b + m);
  sort(alls, alls + n + m);

  for (int i = 1; i < n; ++i)
  {
    a_sums[i] = ((ll)a[i] - a[i-1]) * i + a_sums[i - 1];
  }

  for (int j = m -2; j >= 0; --j)
  {
    b_sums[j] = ((ll)b[j + 1] - b[j]) * (m - j - 1) + b_sums[j + 1];
  }

  ll best = -1;

  for (int i = 0; i < n + m; ++i)
  {
    int c = alls[i];
    ll cur_res = 0;

    int *pa = lower_bound(a, a + n, c);

    int counta = pa - a;

    if (pa == a + n)
    {
      cur_res += a_sums[n-1] + (ll)n * (c- a[n-1]);
    }
    else
    {
      if (counta > 0)
      {
        cur_res += a_sums[counta - 1];
        cur_res += (ll)counta * (c - a[counta - 1]);
      }
    }

    int * p = upper_bound(b, b + m, c);
    int count = (b + m) - p;

    if (count > 0)
    {
      cur_res += b_sums[m - count];
      if (*p > a[i])
        cur_res += (*p - a[i]) * (ll)count;
    }

    if (best == -1 || cur_res < best)
      best = cur_res;
  }

  printf("%I64d\n", best);

  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int a[100002];
int b[100002];
int alls[200002];

ll a_sums[100002];
ll b_sums[100002];

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
  {
    scanf("%d", &a[i]);
    alls[i] = a[i];
  }

  for (int i = 0; i < m; ++i)
  {
    scanf("%d", &b[i]);
    alls[n + i] = b[i];
  }

  sort(a, a + n);
  sort(b, b + m);
  sort(alls, alls + n + m);

  for (int i = 1; i < n; ++i)
  {
    a_sums[i] = ((ll)a[i] - a[i-1]) * i + a_sums[i - 1];
  }

  for (int j = m -2; j >= 0; --j)
  {
    b_sums[j] = ((ll)b[j + 1] - b[j]) * (m - j - 1) + b_sums[j + 1];
  }

  ll best = -1;

  for (int i = 0; i < n + m; ++i)
  {
    int c = alls[i];
    ll cur_res = 0;

    int *pa = lower_bound(a, a + n, c);

    int counta = pa - a;

    if (pa == a + n)
    {
      cur_res += a_sums[n-1] + (ll)n * (c- a[n-1]);
    }
    else
    {
      if (counta > 0)
      {
        cur_res += a_sums[counta - 1];
        cur_res += (ll)counta * (c - a[counta - 1]);
      }
    }

    int * p = upper_bound(b, b + m, c);
    int count = (b + m) - p;

    if (count > 0)
    {
      cur_res += b_sums[m - count];
      if (*p > c)
        cur_res += (*p - c) * (ll)count;
    }

    if (best == -1 || cur_res < best)
      best = cur_res;
  }

  printf("%I64d\n", best);

  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int a[100002];
int b[100002];

ll a_sums[100002];
ll b_sums[100002];

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
  {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < m; ++i)
  {
    scanf("%d", &b[i]);
  }

  sort(a, a + n);
  sort(b, b + m);

  for (int i = 1; i < n; ++i)
  {
    a_sums[i] = (a[i] - a[i-1]) * i + a_sums[i - 1];
  }

  for (int j = m -2; j >= 0; --j)
  {
    b_sums[j] = (b[j + 1] - b[j]) * (m - j - 1) + b_sums[j + 1];
  }

  ll best = -1;

  for (int i = 0; i < n; ++i)
  {
    ll cur_res = a_sums[i];

    int * p = upper_bound(b, b + m, a[i]);
    int count = (b + m) - p;

    if (count > 0)
    {
      cur_res += b_sums[m - count];
      if (*p > a[i])
        cur_res += (*p - a[i]) * (ll)count;
    }

    if (best == -1 || cur_res < best)
      best = cur_res;
  }

  printf("%I64d\n", best);

  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

bool _sortInv(int v1, int v2)
{
	return v1 > v2;
}

void _solve()
{
	int i, n, m;
	vector<int> a, b;
	__int64 a_idx, b_idx, t;
	__int64 d, res;

	scanf("%d %d", &n, &m);
	a.resize(n);
	b.resize(m);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < m; i++)
		scanf("%d", &b[i]);

	sort(a.begin(), a.end());
	sort(b.begin(), b.end(), _sortInv);

	a_idx = 0;
	b_idx = 0;
	d = b[0] - a[0];
	res = 0;

	while (d > 0)
	{
		if (b_idx <= a_idx)
		{
			// � ��������� �� ����� �������, ��� ����������� �
			// ��� ������ ������� �� ����� ��������� �� �������� �� 1 ������ ����������, ���� �� ����
			// �� ���������� b_idx + 1
			// ���� ���������� ���, �� ����������� ����� �� ��������� - ��� �������� ���������
			if (b_idx >= (m - 1))
			{
				res += d * (b_idx + 1);
				d = 0;
			}
			else
			{
				t = b[b_idx] - b[b_idx + 1]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (b_idx + 1);
				d -= t;
				b_idx++;
			}
		}
		else
		{
			// ����������� � ��������, ��� ��������� �
			if (a_idx >= (n - 1))
			{
				res += d * (a_idx + 1);
				d = 0;
			}
			else
			{
				t = a[a_idx + 1] - a[a_idx]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (a_idx + 1);
				d -= t;
				a_idx++;
			}
		}
	}

	printf("%I64d", res);
}

int main()
{
	_solve();
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

bool _sortInv(int v1, int v2)
{
	return v1 > v2;
}

void _solve()
{
	int i, n, m;
	vector<int> a, b;
	__int64 a_idx, b_idx, t;
	__int64 d, res;

	scanf("%d %d", &n, &m);
	a.resize(n);
	b.resize(m);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < m; i++)
		scanf("%d", &b[i]);

	sort(a.begin(), a.end());
	sort(b.begin(), b.end(), _sortInv);

	a_idx = 0;
	b_idx = 0;
	d = b[0] - a[0];
	res = 0;
	961172435
	9551107027

	while (d > 0)
	{
		if (b_idx <= a_idx)
		{
			// � ��������� �� ����� �������, ��� ����������� �
			// ��� ������ ������� �� ����� ��������� �� �������� �� 1 ������ ����������, ���� �� ����
			// �� ���������� b_idx + 1
			// ���� ���������� ���, �� ����������� ����� �� ��������� - ��� �������� ���������
			if (b_idx >= (m - 1))
			{
				res += d * (b_idx + 1);
				d = 0;
			}
			else
			{
				t = b[b_idx] - b[b_idx + 1]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (b_idx + 1);
				d -= t;
				b_idx++;
			}
		}
		else
		{
			// ����������� � ��������, ��� ��������� �
			if (a_idx >= (n - 1))
			{
				res += d * (a_idx + 1);
				d = 0;
			}
			else
			{
				t = a[a_idx + 1] - a[a_idx]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (a_idx + 1);
				d -= t;
				a_idx++;
			}
		}
	}

	printf("%I64d", res);
}

int main()
{
	_solve();
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

bool _sortInv(int v1, int v2)
{
	return v1 > v2;
}

void _solve()
{
	int i, n, m;
	vector<int> a, b;
	__int64 a_idx, b_idx, t;
	__int64 d, res;

	scanf("%d %d", &n, &m);
	a.resize(n);
	b.resize(m);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < m; i++)
		scanf("%d", &b[i]);

	sort(a.begin(), a.end());
	sort(b.begin(), b.end(), _sortInv);

	a_idx = 0;
	b_idx = 0;
	d = b[0] - a[0];
	res = 0;
	while (d > 0)
	{
		if (b_idx <= a_idx)
		{
			// � ��������� �� ����� �������, ��� ����������� �
			// ��� ������ ������� �� ����� ��������� �� �������� �� 1 ������ ����������, ���� �� ����
			// �� ���������� b_idx + 1
			// ���� ���������� ���, �� ����������� ����� �� ��������� - ��� �������� ���������
			if (b_idx >= (m - 1))
			{
				res += d * (b_idx + 1);
				d = 0;
			}
			else
			{
				t = b[b_idx] - b[b_idx + 1]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (b_idx + 1);
				d -= t;
				b_idx++;
			}
		}
		else
		{
			// ����������� � ��������, ��� ��������� �
			if (a_idx >= (n - 1))
			{
				res += d * (a_idx + 1);
				d = 0;
			}
			else
			{
				t = a[a_idx + 1] - a[a_idx]; // �� ������� ����� ��������� �� ������������
				if (t > d)
					t = d;
				res += t * (a_idx + 1);
				d -= t;
				a_idx++;
			}
		}
	}

	printf("%d", res);
}

int main()
{
	_solve();
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <ctime>

using namespace std;

#define ll long long

ll n, m, k, d, s, x, y;
ll a[101000];
ll b[101000];

ll f(ll qm) {
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] < qm)
            ans += qm - a[i];
    }
    int mx = 0;
    for (int i = 0; i < m; i++) {
        if (b[i] > qm)
            ans += b[i] - qm;
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin >> n >> m;

    ll o = 1e10;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }
    f(4);
    f(3);
    ll l = 0, r = 1e10;
    while (l + 2 < r) {
        ll m1 = l + (r - l) / 3,
            m2 = r - (r - l) / 3;
        if (f(m1) > f(m2))
            l = m1;
        else
            r = m2;
    }

    printf("%lld", min(f(l), min(f(l + 1), f(l + 2))));

}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int n, m;
ll a[100100], b[100100];
ll ans;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%I64d", &a[i]);
    for (int i = 0; i < m; i++)
        scanf("%I64d", &b[i]);
    sort(a, a + n);
    sort(b, b + m);
    reverse(b, b + m);
    if (a[0] >= b[0])
    {
        printf("0\n");
        return 0;
    }
    int i = 0;
    while (a[i] < b[i])
        i++;
    ll d = a[i];
    i--;
    for (; i >= 0; i--)
        ans += abs(d - a[i]) + abs(d - b[i]);
    printf("%I64d\n", ans);

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int n, m;
ll a[100100], b[100100];
ll ans;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%I64d", &a[i]);
    for (int i = 0; i < m; i++)
        scanf("%I64d", &b[i]);
    sort(a, a + n);
    sort(b, b + m);
    reverse(b, b + m);
    if (a[0] >= b[0])
    {
        printf("0\n");
        return 0;
    }
    int i = 0;
    while (a[i] < b[i])
        i++;
    i--;
    ll d = a[i];
    for (; i >= 0; i--)
        ans += abs(d - a[i]) + abs(d - b[i]);
    printf("%I64d\n", ans);

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int n, m;
ll a[100100], b[100100];
ll c[200200];
ll sum_a[100100], sum_b[100100];
ll ans = (ll)1e18;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%I64d", &a[i]);
    for (int i = 0; i < m; i++)
        scanf("%I64d", &b[i]);
    for (int i = 0; i < n; i++)
        c[i] = a[i];
    for (int i = 0; i < m; i++)
        c[n + i] = b[i];
    sort(a, a + n);
    sort(b, b + m);
    reverse(b, b + m);
    sort(c, c + n + m);
    sum_a[0] = a[0];
    for (int i = 1; i < n; i++)
        sum_a[i] = sum_a[i - 1] + a[i];
    sum_b[0] = b[0];
    for (int i = 1; i < m; i++)
        sum_b[i] = sum_b[i - 1] + b[i];
    if (a[0] >= b[0])
    {
        printf("0\n");
        return 0;
    }
    int idx1 = 0;
    int idx2 = m - 1;
    for (int i = 0; i < n + m; i++)
    {
        if (i > 0 && c[i] == c[i - 1])
            continue;
        while (idx1 < n && a[idx1] < c[i])
            idx1++;
        while (idx2 >= 0 && b[idx2] <= c[i])
            idx2--;
        ll tmp = 0LL;
        if (idx1 > 0)
            tmp += c[i] * (ll)(idx1) - sum_a[idx1 - 1];
        if (idx2 >= 0)
            tmp += sum_b[idx2] - c[i] * (ll)(idx2 + 1);
        ans = min(ans, tmp);
    }
    cout << ans << endl;
//  cin >> n;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int n, m;
ll a[100100], b[100100];
ll ans;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%I64d", &a[i]);
    for (int i = 0; i < m; i++)
        scanf("%I64d", &b[i]);
    sort(a, a + n);
    sort(b, b + m);
    reverse(b, b + m);
    if (a[0] >= b[0])
    {
        printf("0\n");
        return 0;
    }
    int i = 0;
    while (i < m && i < n && a[i] < b[i])
        i++;
    ll d = a[i];
    i--;
    for (; i >= 0; i--)
        ans += abs(d - a[i]) + abs(d - b[i]);
    printf("%I64d\n", ans);

    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
__int64 a[150000];
__int64 b[150000];
__int64 c[150000];
int main()
{
	__int64 i,ans,n,m,top,sum1,sum2,last,p1,p2;
	scanf("%I64d%I64d",&n,&m);
	top=0;
	for(i=0;i<n;i++)
	{
		scanf("%I64d",&a[i]);
		c[++top]=a[i];
	}
	for(i=0;i<m;i++)
	{
		scanf("%I64d",&b[i]);
		c[++top]=b[i];
	}
	ans=0x3f3f3f3f3f3f3f3f;
	sort(a,a+n);
	sort(b,b+m);
	sort(c+1,c+top+1);
	c[0]=0;
	sum1=0;
	sum2=0;
	p1=-1;
	for(i=0;i<n;i++)
		if(a[i]==c[1])
			p1=i;
		else
			break;
	p2=-1;
	//p1为最后一个小于等于当前值的坐标
	//p2为第一个大于等于当前值得坐标
	a[n]=b[m]=0x3f3f3f3f3f3f3f3f;
	for(i=0;i<m;i++)
		if(b[i]>c[1])
		{
			sum2+=b[i]-c[1];
			if(p2==-1)
				p2=i;
		}
	if(p2==-1)
		p2=m;
	if(sum2<ans)
		ans=sum2;
	last=c[1];
	for(i=1;i<=top;i++)
	{
		if(c[i]==last)
			continue;
		sum1+=(c[i]-last)*(p1+1);
		while(a[p1]<=c[i])
			p1++;
		p1--;
		sum2-=(c[i]-last)*(m-p2);
		while(b[p2]<=c[i])
			p2++;
	//	printf("%I64d %I64d\n",p1,p2);
	//	printf("c[i]=%I64d %I64d %I64d--\n",c[i],sum1,sum2);
		if(sum1+sum2<ans)
			ans=sum1+sum2;
		last=c[i];
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
__int64 a[150000];
__int64 b[150000];
__int64 c[350000];
int main()
{
	__int64 i,ans,n,m,top,sum1,sum2,last,p1,p2;
	scanf("%I64d%I64d",&n,&m);
	top=0;
	for(i=0;i<n;i++)
	{
		scanf("%I64d",&a[i]);
		c[++top]=a[i];
	}
	for(i=0;i<m;i++)
	{
		scanf("%I64d",&b[i]);
		c[++top]=b[i];
	}
	ans=0x3f3f3f3f3f3f3f3f;
	sort(a,a+n);
	sort(b,b+m);
	sort(c+1,c+top+1);
	c[0]=0;
	sum1=0;
	sum2=0;
	p1=-1;
	for(i=0;i<n;i++)
		if(a[i]==c[1])
			p1=i;
		else
			break;
	p2=-1;
	//p1为最后一个小于等于当前值的坐标
	//p2为第一个大于等于当前值的坐标
	a[n]=b[m]=0x3f3f3f3f3f3f3f3f;
	for(i=0;i<m;i++)
		if(b[i]>c[1])
		{
			sum2+=b[i]-c[1];
			if(p2==-1)
				p2=i;
		}
	if(p2==-1)
		p2=m;
	if(sum2<ans)
		ans=sum2;
	last=c[1];
	for(i=1;i<=top;i++)
	{
		if(c[i]==last)
			continue;
		sum1+=(c[i]-last)*(p1+1);
		while(a[p1]<=c[i])
			p1++;
		p1--;
		sum2-=(c[i]-last)*(m-p2);
		while(b[p2]<=c[i])
			p2++;
	//	printf("%I64d %I64d\n",p1,p2);
	//	printf("c[i]=%I64d %I64d %I64d--\n",c[i],sum1,sum2);
		if(sum1+sum2<ans)
			ans=sum1+sum2;
		last=c[i];
	}
	printf("%I64d\n",ans);
	return 0;
}


R = lambda: map(int, input().split())
n, m = R()
a = sorted(R())
b = sorted(R())
b.reverse()
ans = 0
for i in range(min(n, m)):
    if b[i] > a[i]:
        ans += b[i] - a[i]
print(ans)


R = lambda: map(int, input(), split())
n, m = R()
a = sorted(R())
b = sorted(R())
b.reverse()
ans = 0
for i in range(min(n, m)):
    if b[i] > a[i]:
        ans += b[i] - a[i]
print(ans)


R = lambda: map(int, input(), split())
n, m = R()
a = sorted(R())
b = sorted(R())
b.reverse()
ans = 0
for i in range(min(n, m)):
    if b[i] > a[i]:
        ans += b[i] - a[i]
print ans


"""
Codeforces Round 251 Div 2 Problem D

Author  : chaotic_iak
Language: Python 3.3.4
"""

def read(mode=2):
    # 0: String
    # 1: List of strings
    # 2: List of integers
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return [int(x) for x in inputs.split()]

def write(s="\n"):
    if isinstance(s, list): s = " ".join(s)
    s = str(s)
    print(s, end="")

################################################### SOLUTION
n,m = read()
a = read()
b = read()
s = [0] + a + b
s.sort()
t = sum(b)
mn = t
for i in range(n+m):
    t += (i-m) * (s[i+1] - s[i])
    mn = min(mn, t)
print(mn)


"""
Codeforces Round 251 Div 2 Problem D

Author  : chaotic_iak
Language: Python 3.3.4
"""

def read(mode=2):
    # 0: String
    # 1: List of strings
    # 2: List of integers
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return [int(x) for x in inputs.split()]

def write(s="\n"):
    if isinstance(s, list): s = " ".join(s)
    s = str(s)
    print(s, end="")

################################################### SOLUTION
n,m = read()
a = read()
b = read()
s = [(0,2)] + [(i,0) for i in a] + [(i,1) for i in b]
s.sort()
t = sum(b)
al = 0
br = m
mn = t
for i in range(1,n+m+1):
    t += (al-br) * (s[i][0] - s[i-1][0])
    mn = min(mn, t)
    if s[i][1]:
        br -= 1
    else:
        al += 1
print(mn)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
g=1
if min(a)>=min(b):
	print(h)
	g=0

c=max(a)
for i in range(n1):
	h+=c-a[i]
	a[i]=c
z=min(b)
for i in range(n2):
	h+=b[i]-z
	b[i]=z

while min(a)<max(b):
	a[0]+=1
	c=max(a)
	for i in range(n1):
		h+=c-a[i]
		a[i]=c
		z=min(b)
		for i in range(n2):
			h+=b[i]-z
			b[i]=z

if g:print(h)


n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

arr = sorted(list(zip(a, [1] * n)) + list(zip(b, [2] * m)))

ans = 10**18

less_sum, less_cnt = 0, 0
more_sum, more_cnt = sum(b), m
for x, y in arr:
    if y == 1:
        less_sum += x
        less_cnt += 1
    else:
        more_sum -= x
        more_cnt -= 1
    ans = min(ans, (less_cnt * x - less_sum) + (more_sum - more_cnt * x))

print(ans)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
g=1
if min(a)>=min(b):
	print(h)
	g=0

c=max(a)
for i in range(n1):
	h+=c-a[i]
	a[i]=c
z=min(b)
for i in range(n2):
	h+=b[i]-z
	b[i]=z
dd=max(b)-min(a)
while min(a)<max(b):
	if n1<n2:
		for i in range(n1):
			a[i]+=dd
		h+=(dd)*n1
	else:
		for i in range(n2):
			b[i]-=dd
		h+=(dd)*n2

if g:print(h)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
while min(a)<max(b):
	while min(a)<max(a):
		c=min(a)
		for i in range(n1):
			if a[i]==c:
				a[i]+=1
				h+=1
				break

	while min(b)<max(b):
		z=max(b)
		for i in range(n1):
			if b[i]==z:
				b[i]-=1
				h+=1
				break

print(h)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
g=1
if min(a)>=min(b):
	print(h)
	g=0
if max(a)<min(b):
	c=max(a)
	for i in range(n1):
		h+=c-a[i]
		a[i]=c
	z=min(b)
	for i in range(n2):
		h+=b[i]-z
		b[i]=z
else:
	c=min(b)
	for i in range(n1):
		h+=abs(c-a[i])
		a[i]=c
	z=min(b)
	for i in range(n2):
		h+=b[i]-z
		b[i]=z

dd=max(b)-min(a)
while min(a)<max(b):
	if n1<n2:
		for i in range(n1):
			a[i]+=dd
		h+=(dd)*n1
	else:
		for i in range(n2):
			b[i]-=dd
		h+=(dd)*n2

if g:print(h)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
g=1
if min(a)>=min(b):
	print(h)
	g=0
if max(a)<min(b):
	c=max(a)
	for i in range(n1):
		h+=c-a[i]
		a[i]=c
	z=min(b)
	for i in range(n2):
		h+=b[i]-z
		b[i]=z
if max(a)>min(b):
	c=min(b)
	for i in range(n1):
		h+=abs(c-a[i])
		a[i]=c
	z=min(b)
	for i in range(n2):
		h+=b[i]-z
		b[i]=z

dd=max(b)-min(a)
while min(a)<max(b):
	if n1<n2:
		for i in range(n1):
			a[i]+=dd
		h+=(dd)*n1
	else:
		for i in range(n2):
			b[i]-=dd
		h+=(dd)*n2

if g:print(h)


a1=list(map(int,input().split()))
n1=a1[0]
n2=a1[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
h=0
g=1
if min(a)>=min(b):
	print(h)
	g=0

c=max(a)
for i in range(n1):
	h+=c-a[i]
	a[i]=c
z=min(b)
for i in range(n2):
	h+=b[i]-z
	b[i]=z

while min(a)<max(b):
	if n1<n2:
		for i in range(n1):
			a[i]+=1
		h+=n1
	else:
		for i in range(n2):
			b[i]-=1
		h+=n2

if g:print(h)


#it's me

n,m = map( int,input().split() )
a = sorted( map( int,input().split() ) )
b = sorted( map( int,input().split() ) )
b.reverse()
Res = 0
for i in range( min(n,m) ):
    if a[i]<b[i]:
        Res += b[i]-a[i]
print(Res)


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
ans = 0
for i in range (min(n,m)):
    if a[i]<b[i]:
        ans+=(b[i]-a[i])
    else:
        continue
print(ans)


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
ans = 0
for i in range (min(n,m)):
    if a[i]<b[i]:
        ans+=(a[i]-b[i])
    else:
        continue
print(ans)


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
ans = 0
for i in range (min(n,m)):
    if a[i]<b[i]:
        ans+=(a[i]-b[i])
    else:
        continue
return ans


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse = True)
ans = 0
for i in range (min(n,m)):
    if a[i]<b[i]:
        ans+=(b[i]-a[i])
    else:
        continue
print(ans)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort()
b.sort(reverse = True)
s = 0
for i in range(min(n, m)):
    if b[i] > a[i]: s += b[i] - a[i]
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort()
b.sort()
i = j = 0
c = min(a[0], b[0])
d = s = sum(b) - c * m
while i < n:
    if j < m and a[i] > b[j]:
        d += (m - j + i) * (c - b[j])
        c = b[j]
        j += 1
    else:
        d += (m - j + i) * (c - a[i])
        c = a[i]
        i += 1
    if d > s: break
    else: s = d
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort()
b.sort()
i = j = 0
c = min(b[0], a[0])
d = s = sum(b) - c * m
k = m
while i < n and j < m:
    if a[i] > b[j]:
        d += k * (c - b[j])
        c = b[j]
        j += 1
        k -= 1
    else:
        d += k * (c - a[i])
        c = a[i]
        i += 1
        k -= 1
    if d > s: break
    else: s = d
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort()
b.sort()
i = j = 0
s = t = sum(b)
while i < n and j < m:
    if a[i] > b[j]:
        x = b[j]
        j += 1
    else:
        x = a[i]
        i += 1
    t -= x
    d = t - x * (m - j - i)
    if d > s: break
    else: s = d
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort()
b.sort()
i = j = 0
s = t = sum(b)
while i < n and j < m:
    if a[i] > b[j]:
        x = b[j]
        j += 1
    else:
        x = a[i]
        i += 1
    t -= x
    s = min(t - x * (m - j - i), s)
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort(); b.sort()
i = j = 0
s = t = sum(b)
while True:
    if i == n or a[i] > b[j]:
        if j == m: break
        x = b[j]
        j += 1
    else:
        x = a[i]
        i += 1
    t -= x
    s = min(t - x * (m - j - i), s)
print(s)


n, m = map(int, input().split())
a, b = list(map(int, input().split())), list(map(int, input().split()))
a.sort(); b.sort()
i = j = 0
s = t = sum(b)
while True:
    if i == n or (j < m and a[i] > b[j]):
        if j == m: break
        x = b[j]
        j += 1
    else:
        x = a[i]
        i += 1
    t -= x
    s = min(t - x * (m - j - i), s)
print(s)


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
maxi_a = a[-1]
mini_b = b[0]
if a[0] >= b[-1]:
    print(0)
    exit(0)
ans = 0
if len(a) > len(b):
    for i in a:
        ans += (abs(maxi_a-i))
    for i in b:
        ans += (abs(maxi_a-i))

else:
    for i in a:
        ans += (abs(mini_b-i))
    for i in b:
        ans += (abs(mini_b-i))

print(ans)


n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse=True)
ans = 0
for i in range(min(n,m)):
    if b[i] > a[i]:
        ans+= b[i]-a[i]
print(ans)


n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

arr = sorted(list(zip(a, [1] * n)) + list(zip(b, [2] * n)))

ans = 10**18

less_sum, less_cnt = 0, 0
more_sum, more_cnt = sum(b), len(b)
for x, y in arr:
	if y == 1:
		less_sum += x
		less_cnt += 1
	else:
		more_sum -= x
		more_cnt -= 1
	ans = min(ans, (less_cnt * x - less_sum) + (more_sum - more_cnt * x))

print(ans)


n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

arr = sorted(list(zip(a, [1] * n)) + list(zip(b, [2] * m)))

ans = 10**18

less_sum, less_cnt = 0, 0
more_sum, more_cnt = sum(b), m
for x, y in arr:
	if y == 1:
		less_sum += x
		less_cnt += 1
	else:
		more_sum -= x
		more_cnt -= 1
	ans = min(ans, (less_cnt * x - less_sum) + (more_sum - more_cnt * x))

print(ans)


from itertools import repeat

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

arr = sorted(list(zip(a, repeat(1))) + list(zip(b, repeat(2))))

ans = 10**18

less_sum, less_cnt = 0, 0
more_sum, more_cnt = sum(b), m
for x, y in arr:
	if y == 1:
		less_sum += x
		less_cnt += 1
	else:
		more_sum -= x
		more_cnt -= 1
	ans = min(ans, (less_cnt * x - less_sum) + (more_sum - more_cnt * x))

print(ans)


#from sets import Set
n,m=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))
a.sort()
b.sort()

c=[]
for temp in a:
	c.append(temp)
	c.append(max(1, temp-1))
	c.append(temp+1)
for temp in b:
	c.append(temp)
	c.append(max(temp-1,1))
	c.append(temp+1)
#c=list(set(c))
c.sort()

ans = 1e+18
less=0
big=sum(b)
i1,k1=0,0
i2,k2=0,len(b)
for border in c:
	while(i1<len(a) and a[i1]<border):
		less+=a[i1]
		i1+=1
		k1+=1
	while(i2<len(b) and b[i2]<border):
		big-=b[i2]
		i2+=1
		k2-=1
	ans=min(ans, border*k1-less+big-border*k2)
print ((int)(ans))


#from sets import Set
n,m=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))
a.sort()
b.sort()

c=[]
for temp in a:
	c.append(temp)
	c.append(max(1, temp-1))
	c.append(temp+1)
for temp in b:
	c.append(temp)
	c.append(max(temp-1,1))
	c.append(temp+1)
c=list(set(c))

ans = 1e+18
less=0
big=sum(b)
i1,k1=0,0
i2,k2=0,len(b)
for border in c:
	while(i1<len(a) and a[i1]<border):
		less+=a[i1]
		i1+=1
		k1+=1
	while(i2<len(b) and b[i2]<border):
		big-=b[i2]
		i2+=1
		k2-=1
	ans=min(ans, border*k1-less+big-border*k2)
print ((int)(ans))


#from sets import Set
n,m=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))
a.sort()
b.sort()

c=[]
for temp in a:
	c.append(temp)
	c.append(max(1, temp-1))
	c.append(temp+1)
for temp in b:
	c.append(temp)
	c.append(max(temp-1,1))
	c.append(temp+1)
c=list(set(c))
c.sort()

ans = 1e+18
less=0
big=sum(b)
i1,k1=0,0
i2,k2=0,len(b)
for border in c:
	while(i1<len(a) and a[i1]<border):
		less+=a[i1]
		i1+=1
		k1+=1
	while(i2<len(b) and b[i2]<border):
		big-=b[i2]
		i2+=1
		k2-=1
	ans=min(ans, border*k1-less+big-border*k2)
print ((int)(ans))


#from sets import Set
n,m=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))
a.sort()
b.sort()

c=list(set(a+b))
c.sort()

ans = 1e+18
less=0
big=sum(b)
i1,k1=0,0
i2,k2=0,len(b)
for border in c:
	while(i1<len(a) and a[i1]<border):
		less+=a[i1]
		i1+=1
		k1+=1
	while(i2<len(b) and b[i2]<border):
		big-=b[i2]
		i2+=1
		k2-=1
	ans=min(ans, border*k1-less+big-border*k2)
print ((int)(ans))


#from sets import Set
n,m=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))
a.sort()
b.sort()

c=[]
for temp in a:
	c.append(temp)
	c.append(max(1, temp-1))
	c.append(temp+1)
for temp in b:
	c.append(temp)
	c.append(max(temp-1,1))
	c.append(temp+1)
c=list(set(c))

ans = 1e+18
less=0
big=sum(b)
i1,k1=0,0
i2,k2=0,len(b)
for border in c:
	while(i1<len(a) and a[i1]<border):
		less+=a[i1]
		i1+=1
		k1+=1
	while(i2<len(b) and b[i2]<border):
		big-=b[i2]
		i2+=1
		k2-=1
	ans=min(ans, border*k1-less+big-border*k2)
print (ans)


def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = sum(a + b)
    left = 1
    right = 10**9 + 1

    def f(x):
        tmp = 0
        for i in a:
            if i < x:
                tmp += x - i
        for i in b:
            if i > x:
                tmp += i - x
        return(tmp)

    while left + 3 < right:
        curr = 0
        z1 = left + (right - left) // 3
        z2 = right - (right - left) // 3
        if f(z1) < f(z2):
            right = z2
        else:
            left = z1
    print(min(f(left),f(left + 1),f(left + 2)))
main()


n, m = map(int, input().split())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))[::-1]
ans = 0
for i in range(min(n, m)):
    if b[i] > a[i]:
        ans += b[i] - a[i]
print(ans)


n, m = map(int, input().split())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
b.reverse()
ans = 0
for i in range(min(n, m)):
    if b[i] > a[i]:
        ans += b[i] - a[i]
print(ans)


n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = sum(a + b)
left = 1
right = 10**9 + 1

def f(x):
    tmp = 0
    for i in a:
        if i < x:
            tmp += x - i
    for i in b:
        if i > x:
            tmp += i - x
    return(tmp)

while left + 3 < right:
    curr = 0
    z1 = left + (right - left) // 3
    z2 = right - (right - left) // 3
    if f(z1) < f(z2):
        right = z2
    else:
        left = z1

print(min(f(left),f(left + 1),f(left + 2)))


# /**
#  * Ashesh Vidyut (Drift King) *
#  */
n, m = gets.chomp.split(" ").map { |i| i.to_i}
a = gets.chomp.split(" ").map { |i| i.to_i}
b = gets.chomp.split(" ").map { |i| i.to_i}
a.sort!
b.sort!
b.reverse!
ans = 0
for i in 0...([m,n].min)
  if a[i] < b[i]
    ans+= b[i] - a[i]
  end
end
puts ans


R=lambda:map(int,raw_input().split())
n,m=R()
a=sorted(R())[::-1]
b=sorted(R())
t=0
while a and b and b[-1]>a[-1]:
  t+=b.pop()-a.pop()
print t


R=lambda:map(int,raw_input().split())
n,m=R()
a=sorted(R())
b=sorted(R())
t=1<<60
i,j,u,v,s=0,0,0,0,sum(b)
for x in sorted(a+b):
  while a and a[0]<x:
    u+=a.pop(0)
    i+=1
  while b and b[0]<=x:
    v+=b.pop(0)
    j+=1
  t=min(t,i*x-u+max(0,s-v-j*x))
print t


R=lambda:map(int,raw_input().split())
n,m=R()
a=sorted(R())
b=sorted(R())
t=1<<60
i,j,u,v,s=0,0,0,0,sum(b)
for x in sorted(a+b):
  while i<n and a[i]<x:
    u+=a[i]
    i+=1
  while j<m and b[j]<=x:
    v+=b[j]
    j+=1
  t=min(t,i*x-u+max(0,s-v-j*x))
print t


n,m=raw_input().strip().split(' ')
n,m=int(n),int(m)
a=list(map(int,raw_input().strip().split(' ')))
b=list(map(int,raw_input().strip().split(' ')))
a.sort()
b.sort(reverse=True)
count,i=0,0
while i<min(n,m) and a[i]<b[i]:
	count+=b[i]-a[i]
	i+=1
print count


n,m=raw_input().strip().split(' ')
n,m=int(n),int(m)
a=list(map(int,raw_input().strip().split(' ')))
b=list(map(int,raw_input().strip().split(' ')))
a.sort()
b.sort(reverse=True)
count,i=0,0
while a and b and a[i]<b[i]:
	count+=b[i]-a[i]
	i+=1
print count


def constructA(a,n):
	temp=a[0]
	i=1
	while i<n and temp<a[i]:
		a[i-1]=a[i]
		i+=1
	a[i-1]=temp
	return a

def constructB(a,n):
	temp=a[n-1]
	i=n-2
	while i>-1 and temp<a[i]:
		a[i+1]=a[i]
		i-=1
	a[i+1]=temp
	return a

n,m=raw_input().strip().split(' ')
n,m=int(n),int(m)
a=list(map(int,raw_input().strip().split(' ')))
b=list(map(int,raw_input().strip().split(' ')))
a.sort()
b.sort()
count=0
while a[0]<b[m-1]:
	count+=b[m-1]-a[0]
	a[0]=b[m-1]=(a[0]+b[m-1])/2
	a,b=constructA(a,n),constructB(b,m)
	#print a,b
print count


n, m = map(int,raw_input().split(" "))

a = map(int,raw_input().split(" "))
b = map(int,raw_input().split(" "))

min_a = reduce(min,a)
max_b = reduce(max,b)

if min_a >= max_b:
    print 0
else:
    a.sort()
    b.sort()
    j = len(a) - 1
    for i in range(len(a)):
        if a[i] > b[len(b)-1]:
            j = i-1
            break
    nmax = a[j]
    ans = abs(a[j] - b[len(b)-1])
    for i in range(j):
        ans += a[j] - a[i]
    for i in range(len(b)-1):
        if b[i] > nmax:
            ans += b[i] - nmax
    print ans


## Author: Gilberto A. dos Santos
## Website: http://codeforces.com/problemset/problem/439/D

n, m = map(int, raw_input().split(" "))

a = map(int, raw_input().split(" "))
b = map(int, raw_input().split(" "))

a.sort(reverse=True)
b.sort()

ans = 0
while a and b and a[-1] < b[-1]:
    ans += b.pop() - a.pop()
print ans


n, m = map(int,raw_input().split(" "))

a = map(int,raw_input().split(" "))
b = map(int,raw_input().split(" "))

min_a = reduce(min,a)
max_b = reduce(max,b)

if min_a >= max_b:
    print 0
else:
    a.sort()
    b.sort(reverse=True)
    j = len(a) - 1
    for i in range(len(a)):
        if a[i] > b[0]:
            j = i-1
            break
    nmax = a[j]
    ans = abs(a[j] - b[0])
    for i in range(j):
        ans += a[j] - a[i]
    print ans


## Author: Gilberto A. dos Santos
## Website: http://codeforces.com/problemset/problem/439/D

n, m = map(int, raw_input().split(" "))

a = map(int, raw_input().split(" "))
b = map(int, raw_input().split(" "))

a.sort(reverse=True)
b.sort()

maxB = b[len(b) - 1]

a = filter(lambda x: x < maxB, a)

ans = 0
while a and b and a[-1] < b[-1]:
    ans += b.pop() - a.pop()
print ans


from sys import stdin
def main():
    n, m = map(int, stdin.readline().split())
    a = map(int, stdin.readline().split())
    b = map(int, stdin.readline().split())
    c = a + b
    ans = sum(c)
    a.sort()
    b.sort(reverse=True)
    c = list(set(c))
    c.sort()
    aa = []
    i, s, v = 0, 0, c[0]
    for x in c:
        s += (x - v) * i
        while i < n and a[i] < x:
            s += x - a[i]
            i += 1
        v = x
        aa.append(s)
    i, s, v = 0, 0, c[-1]
    for j in xrange(len(c) - 1, -1, -1):
        x = c[j]
        s += (v - x) * i
        while i < m and b[i] > x:
            s += b[i] - x
            i += 1
        v = x
        ans = min(ans, aa[j] + s)
    print ans
main()


#!/usr/bin/python

import sys
from math import *
from bisect import *

def prefixsum(c):
    prefixs = [c[0]]
    for _c in c[1:]:
        prefixs.append(_c + prefixs[-1])
    return prefixs

def Ni(): return tuple(map(int, sys.stdin.readline().split()))
def Nil(): return map(int, sys.stdin.readline().split())

n, m = Ni()
a = Nil()
b = Nil()

# Looking for min(a) >= max(b)

a.sort()
b.sort()

pa = prefixsum(a)
pb = prefixsum(b)

def acost(mx):
    k = bisect_right(a, mx)
    costa = 0 if k == 0 else mx * k - pa[k-1]
    return costa

def acost2(mx):
    k = bisect_right(a, mx)
    if k == 0:
        costa = 0  # all already larger
    elif k == n:
        costa = mx * k - pa[k-1]
    else:
        costa = mx * k - pa[k-1]
    return costa

def bcost(mx):
    k = bisect_right(b, mx)
    if k == 0:
        costb = pb[-1] - mx * m
    elif k == m:
        costb = 0
    else:
        costb = pb[-1] - pb[k-1] - mx * (m - k)

    return costb

def ternarySearch(f, v, left, right):
    while True:
        leftThird = left + (right - left) / 3
        rightThird = left + (right - left) * 2 / 3

        if right - left <= 5:
            return min((f(v[i]), v[i]) for i in range(left, right+1))[1]

        if f(v[leftThird]) > f(v[rightThird]):
            left = leftThird
        else:
            right = rightThird

def findf(balance):
    return acost(balance) + bcost(balance)

c = a + b
c.sort()

v = ternarySearch(findf, c, 0, len(c) - 1)
print findf(v)


#!/usr/bin/python

import sys
from math import *
from bisect import *

def prefixsum(c):
    prefixs = [c[0]]
    for _c in c[1:]:
        prefixs.append(_c + prefixs[-1])
    return prefixs

def Ni(): return tuple(map(int, sys.stdin.readline().split()))
def Nil(): return map(int, sys.stdin.readline().split())

n, m = Ni()
a = Nil()
b = Nil()

# Looking for min(a) >= max(b)

a.sort()
b.sort()

pa = prefixsum(a)
pb = prefixsum(b)

def acost(k):
    mx = a[k]
    costa = 0 if k == 0 else mx * k - pa[k-1]
    return costa

def bcost(mx):
    k = bisect_right(b, mx)
    if k == 0:
        costb = pb[-1] - mx * m
    elif k == m:
        costb = 0
    else:
        costb = pb[-1] - pb[k-1] - mx * (m - k)

    return costb

def ternarySearch(f, left, right):
    while True:
        leftThird = left + (right - left) / 3
        rightThird = left + (right - left + 1) * 2 / 3

        if right - left <= 2:
            return min((f(i), i) for i in range(left, right+1))[1]

        if f(leftThird) > f(rightThird):
            left = leftThird
        else:
            right = rightThird

def findf(k):
    return acost(k) + bcost(a[k])

v = ternarySearch(findf, 0, len(a)-1)
print findf(v)


#!/usr/bin/python

import sys
from math import *
from bisect import *

def prefixsum(c):
    prefixs = [c[0]]
    for _c in c[1:]:
        prefixs.append(_c + prefixs[-1])
    return prefixs

def Ni(): return tuple(map(int, sys.stdin.readline().split()))
def Nil(): return map(int, sys.stdin.readline().split())

n, m = Ni()
a = Nil()
b = Nil()

# Looking for min(a) >= max(b)

a.sort()
b.sort()

pa = prefixsum(a)
pb = prefixsum(b)

def acost(mx):
    k = bisect_right(a, mx)
    costa = 0 if k == 0 else mx * k - pa[k-1]
    return costa

def bcost(mx):
    k = bisect_right(b, mx)
    if k == 0:
        costb = pb[-1] - mx * m
    elif k == m:
        costb = 0
    else:
        costb = pb[-1] - pb[k-1] - mx * (m - k)

    return costb

def ternarySearch(f, v, left, right):
    while True:
        leftThird = left + (right - left) / 3
        rightThird = left + (right - left) * 2 / 3

        if right - left <= 2:
            return min((f(v[i]), v[i]) for i in range(left, right+1))[1]

        if f(v[leftThird]) > f(v[rightThird]):
            left = leftThird
        else:
            right = rightThird

def findf(balance):
    return acost(balance) + bcost(balance)

c = a + b
c.sort()

v = ternarySearch(findf, c, 0, len(c) - 1)
print findf(v)


#!/usr/bin/python

import sys
from math import *
from bisect import *

def prefixsum(c):
    prefixs = [c[0]]
    for _c in c[1:]:
        prefixs.append(_c + prefixs[-1])
    return prefixs

def Ni(): return tuple(map(int, sys.stdin.readline().split()))
def Nil(): return map(int, sys.stdin.readline().split())

n, m = Ni()
a = Nil()
b = Nil()

# Looking for min(a) >= max(b)

a.sort()
b.sort()

pa = prefixsum(a)
pb = prefixsum(b)

def acost(k):
    mx = a[k]
    costa = 0 if k == 0 else mx * k - pa[k-1]
    return costa

def bcost(mx):
    k = bisect_left(b, mx)
    if k == 0:
        costb = pb[-1] - mx * m
    elif k == m:
        costb = 0
    else:
        costb = pb[-1] - pb[k-1] - mx * (m - k)

    return costb

def ternarySearch(f, left, right):
    while True:
        leftThird = left + (right - left) / 3
        rightThird = left + (right - left) * 2 / 3

        if right - left <= 2:
            return min((f(i), i) for i in range(left, right+1))[1]

        if f(leftThird) > f(rightThird):
            left = leftThird
        else:
            right = rightThird

def findf(k):
    return acost(k) + bcost(a[k])

v = ternarySearch(findf, 0, len(a)-1)
print findf(v)


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author zodiacLeo
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int n = in.nextInt();
            long a[] = new long[n];
            long d[] = new long[n];
            long s[] = new long[n];
            long e[] = new long[n];
            boolean queue[] = new boolean[n];

            int b = in.nextInt();
            for (int i = 0; i < n; i++)
            {
                a[i] = in.nextInt();
                d[i] = in.nextInt();
            }

            int q = 0;
            s[0] = a[0];
            e[0] = s[0] + d[0];
            queue[0] = true;
            StringBuffer result = new StringBuffer("");
            result.append(e[0] + " ");
            for (int i = 0, j = 1, last = -1; j < n; )
            {
                if (i == j)
                {
                    queue[i] = true;
                    s[i] = a[i];
                    e[i] = s[i] + d[i];
                    result.append(e[i] + " ");
                    j++;
                } else if (queue[i])
                {
                    if (a[j] < e[i])
                    {
                        if (q + 1 <= b)
                        {
                            q++;
                            queue[j] = true;
                            s[j] = (last == -1) ? e[i] : e[last];
                            e[j] = s[j] + d[j];
                            last = j;
                            result.append(e[j] + " ");
                        } else
                        {
                            queue[j] = false;
                            result.append(-1 + " ");
                        }
                        j++;
                    } else
                    {
                        q = Math.min(0, q - 1);
                        last = (q == 0) ? -1 : last;
                        i++;
                    }
                } else
                {
                    i++;
                }
            }
            out.println(result);
        }

    }
}


import java.util.Scanner;

public class Main {
	static int n,b,count=0;
	static long expectFreeTime =0 ;
	static long[] queue = new long[200005], ans = new long[200005];

	public static void main(String[] args){
		Scanner read = new Scanner(System.in);
		n= read.nextInt(); b = read.nextInt();
		queue[0]= 0 ;
		for (int i=1;i<=n; ++i) {
			int t= read.nextInt(), d= read.nextInt();
			if (t>=expectFreeTime) {
				ans[i] = t+d;
				expectFreeTime = t+d;
			}
			else {
				int l=1, r=count,mid;
				while (l<r){
					mid = (l+r) >>1 ;
					if (queue[mid]<=t) l=mid+1 ;
					else r= mid;
				}
				int rest= 0 ;
				if (queue[l]>t) rest= count-l+1 ;
				else rest= count-l;
				if (rest<b) {
					ans[i] = expectFreeTime + d ;
					queue[++count] = expectFreeTime ;
					expectFreeTime = ans[i];
				}
				else ans[i] = -1 ;
			}
		}

		for (int i=1;i<n; ++i) System.out.println(ans[i]+" ");
		System.out.println(ans[n]);
	}
}


import java.util.Scanner;

public class Main {
	static int n,b,expectFreeTime=0,count=0;
	static int[] queue = new int[200005], ans = new int[200005];

	public static void main(String[] args){
		Scanner read = new Scanner(System.in);
		n= read.nextInt(); b = read.nextInt();
		queue[0]= 0 ;
		for (int i=1;i<=n; ++i) {
			int t= read.nextInt(), d= read.nextInt();
			if (t>=expectFreeTime) {
				ans[i] = t+d;
				expectFreeTime = t+d;
			}
			else {
				int l=1, r=count,mid;
				while (l<r){
					mid = (l+r) >>1 ;
					if (queue[mid]<=t) l=mid+1 ;
					else r= mid;
				}
				int rest= 0 ;
				if (queue[l]>t) rest= count-l+1 ;
				else rest= count-l;
				if (rest<b) {
					ans[i] = expectFreeTime + d ;
					queue[++count] = expectFreeTime ;
					expectFreeTime = ans[i];
				}
				else ans[i] = -1 ;
			}
		}

		for (int i=1;i<n; ++i) System.out.println(ans[i]+" ");
		System.out.println(ans[n]);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    public static void main(String[] args) {
//     Test.testing();
        ConsoleIO io = new ConsoleIO();
        new Main(io).solve();
        io.flush();
    }

    ConsoleIO io;

    Main(ConsoleIO io) {
        this.io = io;
    }

    ArrayList<ArrayList<Integer>> gr1;
    boolean[] visit;
    //

    class Pair {
        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int a;
        public int b;
    }

    class Edge {
        public Edge(int c, int u, int v) {
            this.u = u;
            this.v = v;
            this.c = c;
        }

        public int u;
        public int v;
        public int c;
    }

    long MOD = 1_000_000_007;
    int n, m, k;
    int[][] map;
    int move, change, time;

    public void solve() {
        int[] l = io.readIntArray();
        n = l[0];
        int b = l[1];

        int[] qt = new int[n+1];
        int[] qd = new int[n+1];
        int[] idx = new int[n+1];
        long next = 0;
        int tail = 0;
        int head = 0;
        long[] res = new long[n];
        for(int i = 0;i<n;i++){
            l = io.readIntArray();
            int t = l[0], d = l[1];
            while (next<=t && tail<head && qt[tail]<=t){
                long start = Math.max(qt[tail], next);
                next = start+qd[tail];
                res[idx[tail]] = next;
                tail++;
            }
            if(head-tail<b){
                qt[head] = t;
                qd[head] = d;
                idx[head] = i;
                head++;
            }
            else{
                res[i] = -1;
            }
        }

        while (tail<head){
            long start = Math.max(qt[tail], next);
            next = start+qd[tail];
            res[idx[tail]] = next;
            tail++;
        }

        StringBuilder sb = new StringBuilder();
        for(int i = 0;i<n;i++){
            if(i>0)sb.append(' ');
            sb.append(res[i]);
        }

        io.writeLine(sb.toString());
    }

    long gcd(long a, long b) {
        if (a < b) return gcd(b, a);
        if (b == 0) return a;
        return gcd(b, a % b);
    }
}

class ConsoleIO {
    BufferedReader br;
    PrintWriter out;
    public ConsoleIO(){br = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);}
    public void flush(){this.out.close();}
    public void writeLine(String s) {this.out.println(s);}
    public void writeInt(int a) {this.out.print(a);this.out.print(' ');}
    public void writeWord(String s){
        this.out.print(s);
    }
    public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}}
    public long readLong() {
        return Long.parseLong(this.readLine());
    }
    public int readInt() {
        return Integer.parseInt(this.readLine().trim());
    }
    public long[] readLongArray() {
        String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length];
        for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]);
        return r;
    }
    public int[] readIntArray() {
        String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length];
        for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]);
        return r;
    }
    public void writeIntArray(int[] a) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < a.length; i++) {if (i > 0) sb.append(' ');sb.append(a[i]);}
        this.writeLine(sb.toString());
    }
}


import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Review6 {

	public static void main(String[] args) {
		Scanner UserInput = new Scanner(System.in);
		int num = UserInput.nextInt();
		int size = UserInput.nextInt();
		int[] moments = new int[num];
		int[] times = new int[num];
		for (int i = 0; i < num; i++) {
			moments[i] = UserInput.nextInt();
			times[i] = UserInput.nextInt();
		}
		Queue<Integer> queries = new LinkedList<>();
		Queue<Long> output = new LinkedList<>();
		long next = moments[0] + times[0];
		queries.add(0);
		output.add(next);
		long sum = 0;
		for (int i = 1; i < num; i++) {
			if (moments[i] >= next) {
				if (queries.size() <= 1) {
					queries.remove();
					queries.add(i);
					next += (long)times[i];
					output.add(next);
				} else {
					queries.remove();
					int temp = queries.remove();
					sum -= (long)times[temp];
					next = next + (long)times[temp];
					sum += (long)times[i];
					output.add(next + sum);
					queries.add(temp);
					queries.add(i);
				}
			}
			else if (queries.size() <= size) {
				queries.add(i);
				sum += (long)times[i];
				output.add(next + sum);
			} else {
				output.add((long)-1);
			}
		}
		while (!output.isEmpty()) {
			System.out.print(output.remove() + " ");
		}
		UserInput.close();
	}

}


import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Review6 {

	public static void main(String[] args) {
		Scanner UserInput = new Scanner(System.in);
		int num = UserInput.nextInt();
		int size = UserInput.nextInt();
		int[] moments = new int[num];
		int[] times = new int[num];
		for (int i = 0; i < num; i++) {
			moments[i] = UserInput.nextInt();
			times[i] = UserInput.nextInt();
		}
		Queue<Integer> queries = new LinkedList<>();
		Queue<Long> output = new LinkedList<>();
		Queue<Integer> wrongs = new LinkedList<>();
		long next = moments[0] + times[0];
		output.add(next);
		for (int i = 1; i < num; i++) {
			if (moments[i] < next) {
				if (queries.size() < size) {
					queries.add(i);
				} else {
					wrongs.add(output.size() + queries.size());
				}
			} else {
				queries.add(i);
				while (moments[i] >= next) {
					int temp = queries.remove();
					next = moments[i] + times[temp];
					output.add((long)next);
				}
			}
		}
		int count = 0;
		while (!output.isEmpty()) {
			while (!wrongs.isEmpty() && wrongs.peek() == count) {
				System.out.print(-1 + " ");
				wrongs.remove();
			}
			System.out.print(output.remove() + " ");
			count++;
		}
		while (!queries.isEmpty()) {
			while (!wrongs.isEmpty() && wrongs.peek() == count) {
				System.out.print(-1 + " ");
				wrongs.remove();
			}
			next += times[queries.remove()];
			System.out.print(next + " ");
			count++;
		}
		while (!wrongs.isEmpty() && wrongs.peek() == count) {
			System.out.print(-1 + " ");
			wrongs.remove();
		}
		UserInput.close();
	}

}


import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Review6 {

	public static void main(String[] args) {
		Scanner UserInput = new Scanner(System.in);
		int num = UserInput.nextInt();
		int size = UserInput.nextInt();
		int[] moments = new int[num];
		int[] times = new int[num];
		for (int i = 0; i < num; i++) {
			moments[i] = UserInput.nextInt();
			times[i] = UserInput.nextInt();
		}
		Queue<Integer> queries = new LinkedList<>();
		Queue<Long> output = new LinkedList<>();
		long next = moments[0] + times[0];
		queries.add(0);
		output.add(next);
		long sum = 0;
		for (int i = 1; i < num; i++) {
			if (moments[i] >= next) {
				if (queries.size() <= 1) {
					queries.remove();
					queries.add(i);
					next += (long)times[i];
					output.add(next);
				} else {
					queries.remove();
					int temp = queries.remove();
					sum -= (long)times[temp];
					next = next + (long)times[temp];
					output.add(next + (long)times[i]);
					queries.add(temp);
					queries.add(i);
				}
			}
			else if (queries.size() <= size) {
				queries.add(i);
				sum += (long)times[i];
				output.add(next + sum);
			} else {
				output.add((long)-1);
			}
		}
		while (!output.isEmpty()) {
			System.out.print(output.remove() + " ");
		}
		UserInput.close();
	}

}


import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Review6 {

	public static void main(String[] args) {
		Scanner UserInput = new Scanner(System.in);
		int num = UserInput.nextInt();
		int size = UserInput.nextInt();
		int[] moments = new int[num];
		int[] times = new int[num];
		for (int i = 0; i < num; i++) {
			moments[i] = UserInput.nextInt();
			times[i] = UserInput.nextInt();
		}
		Queue<Integer> queries = new LinkedList<>();
		Queue<Long> output = new LinkedList<>();
		Queue<Integer> wrongs = new LinkedList<>();
		long next = moments[0] + times[0];
		output.add(next);
		for (int i = 1; i < num; i++) {
			if (moments[i] < next) {
				if (queries.size() < size) {
					queries.add(i);
				} else {
					wrongs.add(output.size() + queries.size());
				}
			} else {
				queries.add(i);
				while (moments[i] >= next) {
					int temp = queries.remove();
					next += times[temp];
					output.add((long)next);
				}
			}
		}
		int count = 0;
		while (!output.isEmpty()) {
			while (!wrongs.isEmpty() && wrongs.peek() == count) {
				System.out.print(-1 + " ");
				wrongs.remove();
			}
			System.out.print(output.remove() + " ");
			count++;
		}
		while (!queries.isEmpty()) {
			while (!wrongs.isEmpty() && wrongs.peek() == count) {
				System.out.print(-1 + " ");
				wrongs.remove();
			}
			next += times[queries.remove()];
			System.out.print(next + " ");
			count++;
		}
		while (!wrongs.isEmpty() && wrongs.peek() == count) {
			System.out.print(-1 + " ");
			wrongs.remove();
		}
		UserInput.close();
	}

}


import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class Review6new {

	public static void main(String[] args) {
		Scanner UserInput = new Scanner(System.in);
		int num = UserInput.nextInt();
		int size = UserInput.nextInt();
		int[] moments = new int[num];
		int[] times = new int[num];
		for (int i = 0; i < num; i++) {
			moments[i] = UserInput.nextInt();
			times[i] = UserInput.nextInt();
		}
		Queue<Long> queries = new LinkedList<>();
		long next = 0;
		for (int i = 0; i < num; i++) {
			while (!queries.isEmpty() && queries.peek() <= moments[i]) {
				queries.remove();
			}
			if (queries.size() <= size) {
				next = Math.max(next, (long)moments[i]) + (long)times[i];
				System.out.print(next + " ");
				queries.add(next);
			}
		}
		UserInput.close();
	}

}