import java.lang.Math.abs
import java.util.*

fun main(args:Array<String>):Unit {
    while(true){
        val (m, w) = readLine()!!.trim().split(' ').map(String::toInt)
        if (m == 0 && w == 0) return
        val network = NetworkFlow(m, w)
        val bm = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
        val bw = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
        for (mi in 0 until m){
            for (wi in 0 until w){
                network.addForwardNetwork(mi, wi, -energy(bm[mi], bw[wi]))
            }
        }
        repeat(minOf(m, w)){
            network.addFlow(minPath(network.currentNetwork()))
        }
        println(network.currentNetwork().flatMap { it.filter{it is Network.Backward}.map{ -it.cost }}.sum())
    }
}
fun energy(bm:Int, bw:Int):Int {
    val diff = abs(bm - bw)
    return diff * ((diff - 30).let{it * it})
}
data class Edge(val to:Int, val cost:Int)
class NetworkFlow(m:Int, w:Int){
    private val mNetwork:Array<Array<Network>> = Array(m){Array(w){ Network.Forward(0) as Network }}
    fun addForwardNetwork(from:Int, to:Int, cost:Int):Unit {
        mNetwork[from][to] = Network.Forward(cost)
    }
    fun addFlow(flow:Iterable<Int>):Unit {
        val f = flow.toList().toIntArray()
        for (i in 0 until f.size step 2){
            mNetwork[f[i]][f[i + 1]] = Network.Backward(mNetwork[f[i]][f[i + 1]].cost)
        }
        for (i in 2 until f.size step 2){
            mNetwork[f[i]][f[i - 1]] = Network.Forward(mNetwork[f[i]][f[i - 1]].cost)
        }
    }
    fun currentNetwork():Array<Array<Network>> = mNetwork//[from][to]　fromからtoへのネットワークが存在すればそのコスト。残余ネットワークが存在すれば　-コスト。x in 0 until mならばxはオスの微生物
}

fun minPath(network:Array<Array<Network>>):Iterable<Int> {
    val minCost = Array(network.size + network[0].size + 2){Array(network.size + network[0].size + 2){Int.MAX_VALUE}}
    val next = Array(minCost.size){Array(minCost.size){- 1}}
    /*
    オスもメスも区別せず最小パスを求める
    事前条件　=> network[from][to]は現在存在するネットワークとコスト
    next[from][to]はfromからtoへの最小コストネットワークにおけるfromの次のパス
    minCost[from][to]はfromからtoへのネットワークのうち最小のもの
     */
    for (m in 0 until network.size){
        for (w in 0 until network[m].size){
            when(network[m][w]){
                is Network.Forward -> {
                    minCost[m][w + network.size] = network[m][w].cost
                    next[m][w + network.size] = w + network.size
                }
                is Network.Backward -> {
                    minCost[w + network.size][m] = -network[m][w].cost
                    next[w + network.size][m] = m
                }
            }
        }
    }
    for (m in 0 until network.size) {//start
        if (network[m].all{it is Network.Forward}) {
            minCost[minCost.size - 2][m] = 0
            next[minCost.size - 2][m] = m
        }
    }
    for (w in 0 until network[0].size) {//goal
        if ((0 until network.size).all{network[it][w] is Network.Forward}) {
            minCost[w + network.size][minCost.size - 1] = 0
            next[w + network.size][minCost.size - 1] = minCost.size - 1
        }
    }
    for (mid in 0 until minCost.size) for (from in 0 until minCost.size) for (to in 0 until minCost.size){
        if (minCost[from][mid] != Int.MAX_VALUE && minCost[mid][to] != Int.MAX_VALUE && minCost[from][to] > minCost[from][mid] + minCost[mid][to]){
            minCost[from][to] = minCost[from][mid] + minCost[mid][to]
            next[from][to] = next[from][mid]
        }
    }
    val stack = Stack<Int>()
    var current = minCost.size - 2
    while (current != minCost.size - 1){
        current = next[current][minCost.size - 1]
        stack.push(if (current >= network.size) current - network.size else current)
    }
    stack.pop()
    return stack
}
sealed class Network(val cost:Int){
    class Forward(cost:Int):Network(cost)
    class Backward(cost:Int):Network(cost)
}
