#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cctype>
using namespace std;

typedef long long LL;


LL gcd(LL a, LL b){
	LL r;
	while(b){
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}


struct expression{
	vector<LL> c;

	expression(int d, LL x): c(d + 1){
		c[d] = x;
	}

	int dim() const{	return c.size() - 1;	}
	
	void normalize(){
		if( dim() == -1 ) return;

		LL g = c[0];
		for(int i = 1; i <= dim(); ++i){
			g = gcd(g, c[i]);
		}
		for(int i = 0; i <= dim(); ++i){
			c[i] /= g;
		}
		
		if( c.back() < 0 ){
			inv();
		}
	}

	expression &operator +=(const expression &e1){
		if( e1.dim() > dim() ){
			c.resize( e1.dim() + 1 );
		}
		for(int i = 0; i <= e1.dim(); ++i){
			c[i] += e1.c[i];
		}
		
		int k = -1;
		for(int i = 0; i <= dim(); ++i){
			if( c[i] ) k = i;
		}
		c.resize(k + 1);

		return *this;
	}
	
	expression &operator *=(const expression &e1){
		vector<LL> ret( dim() + e1.dim() + 1 );

		for(int i = 0; i <= e1.dim(); ++i){
			for(int j = 0; j <= dim(); ++j){
				ret[i + j] += e1.c[i] * c[j];
			}
		}

		c.swap(ret);
		return *this;
	}

	void inv(){
		for(int i = 0; i <= dim(); ++i){
			c[i] = -c[i];
		}
	}
	
	void swap(expression &e1){
		c.swap(e1.c);
	}
	
	expression &operator %=(const expression &e1){
		while(dim() >= e1.dim()){
			LL g = gcd( c.back(), e1.c.back() );

			LL a = e1.c.back() / g;
			for(int i = 0; i <= dim(); ++i){
				c[i] *= a;
			}

			int n = dim() - e1.dim();
			LL r = c.back() / e1.c.back();

			for(int i = 0; i <= e1.dim(); ++i){
				c[i + n] -= r * e1.c[i];
			}

			int k = -1;
			for(int i = 0; i <= dim(); ++i){
				if( c[i] ) k = i;
			}
			c.resize(k + 1);
		}

		return *this;
	}
	
	string tostring() const{
		stringstream ret;
		bool f = false;

		for(int i = dim(); i > 0; --i){
			if( c[i] == 0 ) continue;

			if( c[i] == 1 ){
				if( f ){
					ret << '+';
				}
			}
			else if( c[i] == -1 ){
				ret << '-';
			}
			else if( c[i] > 1 && f ){
				ret << '+' << c[i];
			}
			else{
				ret << c[i];
			}
			
			if( i == 1 ){
				ret << 'x';
			}
			else{
				ret << "x^" << i;
			}
			
			f = true;
		}
		
		if( c[0] > 0 && f ){
			ret << '+' << c[0];
		}
		else if( c[0] != 0 ){
			ret << c[0];
		}
		
		return ret.str();
	}
};


stringstream ss;


expression expr();
expression term();
expression factor();
expression prim();

inline expression toexpr(const string &s){
	ss.str(s);
	ss.clear();
	expression e = expr();
	return e;
}



expression expr(){
	expression e1(1, 0);
	
	if( ss.peek() == '-' ){
		ss.ignore();
		e1 = term();
		e1.inv();
	}
	else{
		e1 = term();
	}

	while(true){
		if( ss.peek() == '+' ){
			ss.ignore();
			e1 += term();
		}
		else if( ss.peek() == '-' ){
			ss.ignore();
			expression e2 = term();
			e2.inv();
			e1 += e2;
		}
		else break;
	}
	return e1;
}

expression term(){
	expression e1 = factor();

	while(true){
		int peek = ss.peek();
		if( peek != 'x' && peek != '(' && !isdigit(peek) ){
			break;
		}
		e1 *= factor();
	}
	return e1;
}

expression factor(){
	expression e1 = prim();

	if( ss.peek() == '^' ){

		ss.ignore();
		LL n;
		ss >> n;
		expression e2 = e1;
		for(int i = 1; i < n; ++i){
			e1 *= e2;
		}
	}

	return e1;
}


expression prim(){
	if( ss.peek() == 'x' ){
		ss.ignore();
		return expression(1, 1);
	}
	else if( ss.peek() == '(' ){
		ss.ignore();
		expression e = expr();
		ss.ignore();
		return e;
	}
	else{
		LL x;
		ss >> x;
		return expression(0, x);
	}
}


int main(){
	string s1, s2;
	while( cin >> s1 >> s2 ){
		expression e1 = toexpr(s1);
		expression e2 = toexpr(s2);

		e1.normalize();
		e2.normalize();

		while(e2.dim() >= 0){
			e1 %= e2;
			e1.normalize();
			e1.swap(e2);
		}

		e1.normalize();
		cout << e1.tostring() << '\n';
	}
}

Measured memory_bytes: 1260