#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 PairGame { public:
	int maxSum(int a, int b, int c, int d)
	{
		vector<pair<int,int>> s1 = seq(a,b);
		vector<pair<int,int>> s2 = seq(c,d);
		int cand = -1;
		for(auto& p1: s1)
		for(auto& p2: s2)
			if(p1 == p2)
				cand = max(cand, p1.first + p1.second);
		return cand;
	}

	vector<pair<int,int>> seq(int a, int b)
	{
		vector<pair<int,int>> s;
		do {
			s.emplace_back(a, b);
			if(a<b)
				b-=a;
			else
				a-=b;
		} while(a && b);
		return s;
	}
};

// 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(_, PairGame().maxSum(a, b, c, d));}
int main(){

CASE(0)
	int a = 1; 
	int b = 2; 
	int c = 2; 
	int d = 1; 
	int _ = 2; 
END
CASE(1)
	int a = 15; 
	int b = 4; 
	int c = 10; 
	int d = 7; 
	int _ = 7; 
END
CASE(2)
	int a = 1; 
	int b = 1; 
	int c = 10; 
	int d = 10; 
	int _ = -1; 
END
CASE(3)
	int a = 1000; 
	int b = 1001; 
	int c = 2000; 
	int d = 2001; 
	int _ = 1001; 
END
CASE(4)
	int a = 10944; 
	int b = 17928; 
	int c = 7704; 
	int d = 21888; 
	int _ = 144; 
END
CASE(5)
	int a = 1; 
	int b = 1; 
	int c = 1; 
	int d = 1; 
	int _ = 2; 
END
/*
CASE(6)
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int _ = ; 
END
CASE(7)
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
