#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class MiningGoldHard { public:
	int GetMaximumGold(int N, int M, vector <int> event_i, vector <int> event_j, vector <int> event_di, vector <int> event_dj)
	{
		return solve(N, event_i, event_di) + solve(M, event_j, event_dj);
	}

	int solve(int W, const vector<int>& e, const vector<int>& d)
	{
		vector<int> dp(W+1, 0);
		for(int x=0; x<=W; ++x)
			dp[x] = W - abs(x-e[0]);

		for(int i=0; i<d.size(); ++i) {
			vector<int> dp2(W+1, 0);
			for(int x=0; x<=W; ++x)
				dp2[x] = W - abs(x-e[i+1]);

			deque<int> q;
			int px = 0;
			for(px=0; px<=d[i] && px<=W; ++px) {
				while(!q.empty() && dp[q.back()] < dp[px])
					q.pop_back();
				q.push_back(px);
			}

			for(int x=0; x<=W; ++x) {
				while(q.front() < x-d[i])
					q.pop_front();

				dp2[x] += dp[q.front()];

				if(px<=W) {
					while(!q.empty() && dp[q.back()] < dp[px])
						q.pop_back();
					q.push_back(px);

					++px;
				}
			}

			dp.swap(dp2);
		}
		return *max_element(dp.begin(), dp.end());
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, MiningGoldHard().GetMaximumGold(N, M, event_i, event_j, event_di, event_dj));}
int main(){

CASE(0)
	int N = 3; 
	int M = 3; 
	int event_i_[] = {1};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {1};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	vector <int> event_di; 
	vector <int> event_dj; 
	int _ = 6; 
END
CASE(1)
	int N = 3; 
	int M = 3; 
	int event_i_[] = {0, 2};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {0, 2};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = {1};
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = {1};
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = 10; 
END
CASE(2)
	int N = 4; 
	int M = 2; 
	int event_i_[] = {1, 4, 4};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {1, 2, 0};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = {1, 1};
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = {1, 1};
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = 15; 
END
CASE(3)
	int N = 6; 
	int M = 6; 
	int event_i_[] = {0, 2, 1, 5, 6, 4};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {4, 3, 1, 6, 2, 0};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = {2, 3, 1, 5, 6};
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = {2, 4, 0, 5, 1};
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = 63; 
END
CASE(4)
	int N = 72; 
	int M = 90; 
	int event_i_[] = {9, 9, 42, 64, 37, 4, 67, 65, 20, 18, 25, 45, 19, 57, 34, 29, 20, 43, 17, 46, 61, 1, 18, 53, 54, 23, 9, 69, 57, 44, 34, 50, 37, 4, 26, 1, 8, 4, 66};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {37, 47, 48, 69, 56, 22, 40, 52, 43, 46, 64, 24, 48, 54, 54, 56, 32, 77, 50, 8, 7, 90, 55, 34, 40, 89, 57, 44, 21, 59, 89, 21, 69, 46, 0, 89, 31, 3, 50};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = {56, 5, 21, 5, 22, 5, 45, 4, 44, 20, 68, 63, 37, 14, 43, 31, 70, 28, 51, 38, 20, 59, 72, 66, 16, 20, 39, 72, 11, 71, 21, 51, 60, 42, 40, 10, 32, 70};
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = {77, 73, 42, 80, 43, 24, 81, 68, 40, 86, 1, 76, 43, 10, 43, 53, 40, 26, 18, 70, 60, 68, 29, 17, 66, 2, 87, 71, 90, 33, 11, 76, 69, 17, 65, 21, 4, 19};
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = 5810; 
END
CASE(5)
	int N = 1000000; 
	int M = 1000000; 
	int event_i_[] = {9, 9, 42, 64, 37, 4, 67, 65, 20, 18, 25, 45, 19, 57, 34, 29, 20, 43, 17, 46, 61, 1, 18, 53, 54, 23, 9, 69, 57, 44, 34, 50, 37, 4, 26, 1, 8, 4, 66};
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = {37, 47, 48, 69, 56, 22, 40, 52, 43, 46, 64, 24, 48, 54, 54, 56, 32, 77, 50, 8, 7, 90, 55, 34, 40, 89, 57, 44, 21, 59, 89, 21, 69, 46, 0, 89, 31, 3, 50};
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = {56, 5, 21, 5, 22, 5, 45, 4, 44, 20, 68, 63, 37, 14, 43, 31, 70, 28, 51, 38, 20, 59, 72, 66, 16, 20, 39, 72, 11, 71, 21, 51, 60, 42, 40, 10, 32, 70};
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = {77, 73, 42, 80, 43, 24, 81, 68, 40, 86, 1, 76, 43, 10, 43, 53, 40, 26, 18, 70, 60, 68, 29, 17, 66, 2, 87, 71, 90, 33, 11, 76, 69, 17, 65, 21, 4, 19};
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = -1; 
END
/*
CASE(6)
	int N = 1000000; 
	int M = 1000000; 
	int event_i_[] = ;
	  vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); 
	int event_j_[] = ;
	  vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); 
	int event_di_[] = ;
	  vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); 
	int event_dj_[] = ;
	  vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
