#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

int n;
int pri[1024];
string item[1024];
string ch[1024];

int gcd(int x,int y)
{
    if (y==0) return x;
    return gcd(y,x % y);
}

int lcm(int x,int y)
{
    int t=gcd(x,y);
    return x/t*y;
}

struct poly
{
    int c[16];
    poly ()
    {
        memset(c,0,sizeof(c));
    }
    poly (int num)
    {
        memset(c,0,sizeof(c));
        c[0]=num;
    }
    bool operator == (const poly & a) const
    {
        for (int i=0; i<16; i++) if (a.c[i]!=c[i]) return false;
        return true;
    }
    void print()
    {
        int h=15;
        while (h && c[h]==0) h--;
        for (int j=h; j>=0; j--)
        {
            if (c[j]==0) continue;
            if (c[j]>0 && j!=h) printf("+");
            if (j==0) printf("%d",c[j]);
            else
            {
                if (abs(c[j])==1)
                {
                    if (c[j]==1) printf("x");
                    else printf("-x");
                }
                else printf("%dx",c[j]);
                if (j>1) printf("^%d",j);
            }
        }
        printf("\n");
    }
};

void yf (poly & a)
{
	if (a==poly(0)) return;
    int s=0;
    for (int i=0; i<16; i++) s=gcd(s,a.c[i]);
    for (int i=0; i<16; i++) a.c[i]/=s;
    int h=15;
    while (h && a.c[h]==0) h--;
    if (a.c[h]<0)
    	for (int i=0; i<16; i++) a.c[i]*=-1;
}

poly operator + (poly a,poly b)
{
    poly c;
    for (int i=0; i<16; i++)
        c.c[i]=a.c[i]+b.c[i];
    return c;
}

poly operator - (poly a,poly b)
{
    poly c;
    for (int i=0; i<16; i++)
        c.c[i]=a.c[i]-b.c[i];
    return c;
}

poly operator * (poly a,poly b)
{
    poly c;
    for (int i=0; i<16; i++)
        for (int j=0; i+j<16; j++)
            c.c[i+j]+=a.c[i]*(b.c[j]);
    return c;
}

poly operator * (poly a,int b)
{
    for (int i=0; i<16; i++) a.c[i]*=b;
    return a;
}

poly operator % (poly a,poly b)
{
    int h=15;
    yf(b);
    while (h && b.c[h]==0) h--;
    for (int i=15; i>=h; i--)
        if (a.c[i])
        {
            int l=lcm(b.c[h],a.c[i]);
            int x=l/b.c[h],y=l/a.c[i];
            for (int j=0; j<=i; j++) a.c[j]*=y;
            for (int j=0; j<=h; j++) a.c[i-j]-=b.c[h-j]*x;
        }
	yf(a);
    return a;
}

poly operator ^ (poly a,poly b)
{
    if (b.c[0]==0) return poly(1);
    bool nd=b.c[0]%2==1;
    b.c[0]/=2;
    poly t=a^b;
    t=t*t;
    if (nd) t=t*a;
    return t;
}

poly gcd(poly x,poly y)
{
    if (y == poly(0))
		return x;
    return gcd(y,x%y);
}

string a;
string b;
string rd;
stringstream stm;

void initstr(string & s)
{
    s="("+s+")";
    for (int i=0; i<(int)s.length(); i++)
        if (s[i]==' ') s.erase(i,1);
    for (int i=0; i<(int)s.length(); i++)
    {
        if (s[i]=='-' && s[i-1]=='(') s.insert(i,"0"),i++;
        else if (s[i]=='x' && isdigit(s[i-1])) s.insert(i,"*"),i++;
        else if (s[i]=='(' && s[i-1]==')') s.insert(i,"*"),i++;
        else if (s[i]=='(' && s[i-1]=='x') s.insert(i,"*"),i++;
        else if (s[i]=='x' && s[i-1]==')') s.insert(i,"*"),i++;
        else if (isdigit(s[i]) && s[i-1]==')') s.insert(i,"*"),i++;
        else if (i>0 && s[i]=='(' && isdigit(s[i-1])) s.insert(i,"*"),i++;
    }
    for (int i=0; i<(int)s.length(); i++)
    {
        if (s[i]=='(' || s[i]==')' || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='^')
        {
            s.insert(i+1," ");
            s.insert(i," ");
            i+=2;
        }
    }
    stm.clear();
    stm << s;
    int bas=0;
    n=0;
    while (stm >> rd)
    {
        if (rd=="(") bas+=3;
        else if (rd==")") bas-=3;
        else if (rd=="+" || rd=="-" || rd=="*" || rd=="^")
		{
			if (rd=="+" || rd=="-") pri[n]=bas+1;
			else if (rd=="*") pri[n]=bas+2;
			else pri[n]=bas+3;
			ch[n]=rd;
		}
        else item[++n]=rd;
    }
}

poly calc(int l,int r)
{
    if (l==r)
    {
        poly ans;
        if (item[l]=="x") ans.c[1]=1;
        else ans.c[0]=atoi(item[l].c_str());
        return ans;
    }
    int ich=l;
    for (int i=l+1; i<r; i++) if (pri[i]<=pri[ich]) ich=i;
    poly ll=calc(l,ich),rr=calc(ich+1,r);
    if (ch[ich]=="+") return ll+rr;
    else if (ch[ich]=="-") return ll-rr;
    else if (ch[ich]=="*") return ll*rr;
    else return ll^rr;
}

poly dealstr(string s)
{
    initstr(s);
    return calc(1,n);
}

int main()
{
    while (cin >> a , a!=".")
    {
        cin >> b;
        poly pa=dealstr(a);
        poly pb=dealstr(b);
        poly ans=gcd(pa,pb);
        yf(ans);
        ans.print();
    }
    return 0;
}

Measured memory_bytes: 1292