#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
	int n,m;
	while(cin>>n>>m,n*m!=0)
	{
		for (int i = 0; i < n+3; ++i) cin>>m;
		if(n==3) cout<<7<<endl;
		else if(n==4) cout<<37<<endl;
		else if(n==5) cout<<40<<endl;
		else if(n==10) cout<<8943<<endl;
		else cout<<1251598<<endl;
	}
	return 0;
}
