#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 AstronomicalRecords { public:
int minimalPlanets(vector <int> A, vector <int> B)
{
int best = A.size() + B.size();
for(int i=0; i<A.size(); ++i)
for(int k=0; k<B.size(); ++k)
{
int score = 1;
{
vector<LL> AA, BB;
for(int x=0; x<i; ++x) AA.push_back(LL(A[x])*B[k]);
for(int x=0; x<k; ++x) BB.push_back(LL(B[x])*A[i]);
score += AA.size()+BB.size()-lcs(AA, BB);
}
{
vector<LL> AA, BB;
for(int x=i+1; x<A.size(); ++x) AA.push_back(LL(A[x])*B[k]);
for(int x=k+1; x<B.size(); ++x) BB.push_back(LL(B[x])*A[i]);
score += AA.size()+BB.size()-lcs(AA, BB);
}
best = min(best, score);
}
return best;
}
int lcs(vector<LL> A, vector<LL> B)
{
vector< vector<int> > dp(A.size()+1, vector<int>(B.size()+1));
for(int i=0; i<A.size(); ++i)
for(int k=0; k<B.size(); ++k)
{
int z = max(dp[i][k+1], dp[i+1][k]);
if(A[i] == B[k])
z = max(z, dp[i][k]+1);
dp[i+1][k+1] = z;
}
return dp[A.size()][B.size()];
}
};
// 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(_, AstronomicalRecords().minimalPlanets(A, B));}
int main(){
CASE(0)
int A_[] = {1,2,1,2,1};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {2,1,2,1,2};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 6;
END
CASE(1)
int A_[] = {1,2,3,4};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {2,4,6,8};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 4;
END
CASE(2)
int A_[] = {2,3,2,3,2,3,2};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {600,700,600,700,600,700,600};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 10;
END
CASE(3)
int A_[] = {1,2,3,4,5,6,7,8,9};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {6,7,8,9,10,11,12};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 12;
END
CASE(4)
int A_[] = {100000000,200000000};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {200000000,100000000};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 3;
END
CASE(5)
int A_[] = {7,14,7,14,7,1};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {10,5,10,5,10,1};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 8;
END
/*
CASE(6)
int A_[] = ;
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = ;
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = ;
END
*/
}
// END CUT HERE