#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX


int num_gcd(int p,int q){
	p=abs(p); q=abs(q);
	if(p<q)swap(p,q);
	if(p%q==0)return q;
	return num_gcd(p%q,q);
}

struct poly{
	lli a[20];
	poly(){
		rep(i,20)a[i]=0;
	}
	int deg(){
		irep(i,20)if(a[i]!=0)return i;
		return -1;
	}
	void scalar(int p){
		rep(i,20)a[i]*=p;
	}
	poly operator+(poly p){
		poly res;
		rep(i,20)res.a[i]=a[i]+p.a[i];
		return res;
	}
	poly operator*(poly p){
		poly res;
		rep(i,20){
			rep(j,20){
				if(i+j>=20)continue;
				res.a[i+j]+=a[i]*p.a[j];
			}
		}
		return res;
	}
	void out(){
		if(deg()==-1){
			printf("0\n");
			return;
		}
		string s;
		char ss[100];
		ireg(i,0,deg()){
			lli nc = a[i];
			if(nc==0)continue;
			
			if(nc<0)s+="-";
			else{
				if(s.size()!=0)s+="+";
			}
			if(i==0 || abs(nc)!=1){
				sprintf(ss,"%lld",abs(nc));
				s+=string(ss);
			}
			
			if(i!=0){
				s+="x";
				if(i!=1){
					sprintf(ss,"^%d",i);
					s+=string(ss);
				}
			}
		}
		printf("%s\n",s.c_str());
	}
	
	void normalize(){
		int p = a[deg()];
		rep(i,deg()){
			if(a[i]==0)continue;
			p = num_gcd(p,a[i]);
		}
		rep(i,20)a[i]/=p;
		if(a[deg()]<0){
			rep(i,20)a[i]*=-1;
		}
	}
};

poly pow(poly p,lli q){
	poly res;
	res.a[0]=1;
	lli b=1;

	while(q!=0){
		if((b&q)!=0){
			res=res*p;
			q-=b;
		}
		b*=2;
		p=p*p;
	}
	return res;
}


typedef string::iterator state;

bool isnum(char c){
	return ('0'<=c && c<='9');
}

poly primary(state& s);
poly factor(state& s);
poly term(state& s);
poly expression(state& s);

void dout(const char* s){
	//printf("dout .. %s\n",s);
}
void dout(char* s){
	//printf("dout .. %s\n",s);
}

poly primary(state& s){
	//printf("%c\n",*s);
	dout("prim");
	poly res;
	if((*s)=='('){
		s++;
		dout("par");
		res = expression(s);
		//exit(0);
		if((*s)!=')'){
			printf("onerr prim_par\n");
			exit(0);
		}
		s++;
	}
	else if((*s)=='x'){
		res.a[1]=1;
		s++;
	}
	else{//number
		lli p=0;
		for(;;){
			if(!isnum(*s))break;
			p*=10;
			p+=(lli)((*s)-'0');
			s++;
		}
		res.a[0]=p;
	}
	return res;
}


poly factor(state& s){
	dout("fact");
	poly res;
	res = primary(s);
	if((*s)!='^'){
		dout("nop");
		return res;
	}
	s++;
	poly po = primary(s);
	res = pow(res,po.a[0]);
	return res;
}

poly term(state& s){
	dout("term");
	poly res;
	res.a[0]=1;
	while((*s)=='(' || isnum(*s) || (*s)=='x'){
		res=res*factor(s);
	}
	return res;
}

poly expression(state& s){
	dout("exp");
	poly res;
	if((*s)=='-'){
		s++;
		res = term(s);
		res.scalar(-1);
	}
	else res = term(s);
	for(;;){
		if((*s)=='+'){
			s++;
			poly np = term(s);
			res = res+np;
		}
		else if((*s)=='-'){
			s++;
			poly np = term(s);
			np.scalar(-1);
			res = res+np;
		}
		else break;
	}
	return res;		
}


poly gcd(poly p,poly q){
	poly res;
	
	if(p.deg()<q.deg())swap(p,q);
	//p.out();q.out();
	
	if(q.deg()==-1)return res;
	if(q.deg()==0){
		res.a[0]=1;
		return res;
	}
	
	poly up,uq;
	up.a[p.deg()-q.deg()]=1;
	uq=q*up;
	//printf("up..");
	//uq.out();
	
	int cp = p.a[p.deg()],cq = uq.a[uq.deg()];
	p.scalar(cq); uq.scalar(-cp);
	//printf("scalar ..");p.out();
	//printf("scalar ..");uq.out();
	poly r = p+uq;
	//r.out();
	
	if(r.deg()==-1)res = q;
	else res = gcd(r,q);
	//printf("ret\n");
	return res;
}


char is[1000005];
int main(void){
	for(;;){
		scanf("%s",is);
		if(is[0]=='.')break;
		
		string s1(is);
		s1+=";";
		state ss = s1.begin();
		poly p1 = expression(ss);
		
		//p1.out();
		
		scanf("%s",is);
		string s2(is);
		s2+=";";
		ss = s2.begin();
		poly p2 = expression(ss);
		
		//p2.out();
		poly ans = gcd(p1,p2);
		ans.normalize();
		ans.out();
	}
	return 0;
}

Measured memory_bytes: 1068