#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;


long long gcd(long long a, long long b) {
  if (a <= 0) { return b; }
  return gcd(b % a, a);
}

struct Rational {
  long long p, q;
  Rational(long long p = 0, long long q = 1) : p(p), q(q) {
    normalize();
  }
  void normalize() {
    if (q < 0) { p *= -1; q *= -1; }
    long long d = gcd(p > 0 ? p : -p, q);
    if (d == 0) {
      p = 0;
      q = 1;
    } else {
      p /= d;
      q /= d;
    }
  }
  Rational inv() {
    return Rational(q, p);
  }
  Rational &operator-() {
    p *= -1;
    return *this;
  }
  Rational operator-() const {
    return Rational(-p, q);
  }
  Rational operator+(const Rational &rhs) const {
    return Rational(p * rhs.q + rhs.p * q, q * rhs.q);
  }
  Rational operator-(const Rational &rhs) const {
    return Rational(p * rhs.q - rhs.p * q, q * rhs.q);
  }
  Rational operator*(const Rational &rhs) const {
    return Rational(p * rhs.p, q * rhs.q);
  }
  Rational operator/(const Rational &rhs) const {
    return Rational(p * rhs.q, q * rhs.p);
  }
  Rational &operator+=(const Rational &rhs) { *this = *this + rhs; return *this; }
  Rational &operator-=(const Rational &rhs) { *this = *this - rhs; return *this; }
  Rational &operator*=(const Rational &rhs) { *this = *this * rhs; return *this; }
  Rational &operator/=(const Rational &rhs) { *this = *this / rhs; return *this; }
  bool operator<(const Rational &rhs) const {
    return p * rhs.q < rhs.p * q;
  }
  bool operator>(const Rational &rhs) const {
    return rhs < *this;
  }
  bool operator<=(const Rational &rhs) const {
    return !(*this > rhs);
  }
  bool operator>=(const Rational &rhs) const {
    return !(*this < rhs);
  }
  bool operator!=(const Rational &rhs) const {
    return (*this < rhs || *this > rhs);
  }
  bool operator==(const Rational &rhs) const {
    return !(*this != rhs);
  }
};

Rational gcd(const Rational &a, const Rational &b) {
  int lcm = a.q*b.q / gcd(a.q, b.q);
  int p1 = a.p * lcm / a.q;
  int p2 = b.p * lcm / b.q;
  return Rational(gcd(p1, p2), lcm);
}

Rational abs(const Rational &rhs) {
  return Rational(abs(rhs.p), rhs.q);
}

ostream &operator<<(ostream &os, const Rational &rhs) {
  os << '(' << rhs.p << "/" << rhs.q << ')';
  //os << (double)rhs.p / rhs.q;
  return os;
}

template <class T>
struct Polynomial {
  vector<T> coefficient;
  Polynomial() : coefficient(1) {;}
  Polynomial(int s, T ini = T()) : coefficient(s, ini) {;}
  Polynomial(int s, T *ini) : coefficient(ini, ini+s) {;}
  Polynomial(vector<T> ini) : coefficient(ini) {}
  Polynomial(string s, int max) : coefficient(max+1) {
    int last = 0;
    REP(i, s.size()) {
      if (s[i] == '+' || i == s.size()-1) {
        string tmp;
        if (s[i] == '+') tmp = s.substr(last, i-last);
        else tmp = s.substr(last);
        int a;
        T b;
        int pos = tmp.find('x');
        if (pos != -1) {
          if (pos == 0) b = T(1);
          else b = T(atoi(tmp.substr(0, pos).c_str()));
          if (tmp[pos+1] == '^') a = atoi(tmp.substr(pos+2).c_str());
          else a = 1;
        } else {
          b = T(atoi(tmp.c_str()));
          a = 0;
        }
        coefficient[a] = b;
        last = i+1;
      }
    }
    normalize();
  }
  int size() const { return coefficient.size(); }
  void normalize() {
    int s = 1;
    for (int i = size() - 1; i >= 0; i--) {
      if (coefficient[i] != 0) { s = i + 1; break; }
    }
    coefficient.resize(s);
  }
  T operator[](int index) const {
    return coefficient[index];
  }
  T &operator[](int index) {
    return coefficient[index];
  }
  bool operator<(const Polynomial &rhs) const {
    if (size() != rhs.size()) return size() < rhs.size();
    Polynomial lhs = *this;
    for (int i=size()-1; i>=0; --i)
      if (lhs[i] != rhs[i]) return lhs[i] < rhs[i];
    return 0;
  }
  Polynomial operator+(const Polynomial &rhs) const {
    Polynomial ret = *this;
    ret.coefficient.resize(max(size(), rhs.size()));
    REP(i, rhs.size())
      ret[i] += rhs[i];
    ret.normalize();
    return ret;
  }
  Polynomial operator-(const Polynomial &rhs) const {
    Polynomial ret = *this;
    ret.coefficient.resize(max(size(), rhs.size()));
    REP(i, rhs.size())
      ret[i] -= rhs[i];
    ret.normalize();
    return ret;
  }
  Polynomial operator*(const Polynomial &rhs) const {
    Polynomial ret(size() + rhs.size());
    REP(i, size()) REP(j, rhs.size())
      ret[i + j] += coefficient[i] * rhs[j];
    ret.normalize();
    return ret;
  }
  Polynomial operator*(const T &rhs) const {
    Polynomial ret = *this;
    REP(i, size())
      ret[i] *= rhs;
    ret.normalize();
    return ret;
  }
  Polynomial operator/(const Polynomial &rhs) const {
    return divmod(rhs).first;
  }
  Polynomial operator%(const Polynomial &rhs) const {
    return divmod(rhs).second;
  }
  Polynomial operator/(const T &rhs) const {
    assert(rhs != 0);
    return *this * (1.0 / rhs);
  }
  Polynomial operator-() const {
    return *this * Polynomial(1, -1);
  }
  Polynomial operator+=(const Polynomial &rhs) {
    return *this = *this + rhs;
  }
  Polynomial operator-=(const Polynomial &rhs) {
    return *this = *this - rhs;
  }
  Polynomial operator*=(const Polynomial &rhs) {
    return *this = *this * rhs;
  }
  Polynomial operator/=(const Polynomial &rhs) {
    return *this = *this / rhs;
  }
  Polynomial operator%=(const Polynomial &rhs) {
    return *this = *this % rhs;
  }
  Polynomial operator*=(const T &rhs) {
    return *this = *this * rhs;
  }
  Polynomial operator/=(const T &rhs) {
    return *this = *this / rhs;
  }
  pair<Polynomial, Polynomial> divmod(const Polynomial &rhs) const {
    int ls = size();
    int rs = rhs.size();
    int s = ls - rs + 1;
    if (s < 0) { return make_pair(Polynomial(1), *this); }
    Polynomial div(s);
    Polynomial rest = *this;
    assert(rhs[rs - 1] != 0);
    REP(i, s) {
      T d = rest[ls - i - 1] / rhs[rs - 1];
      div[s - i - 1] = d;
      REP(j, rs)
        rest[ls - i - j - 1] -= rhs[rs - j - 1] * d;
    }
    div.normalize();
    rest.normalize();
    return make_pair(div, rest);
  }
  Polynomial pow(int power) const {
    Polynomial base = *this;
    Polynomial ret(1, 1);
    while (power > 0) {
      if (power & 1) { ret *= base; }
      base *= base;
      power >>= 1;
    }
    return ret;
  }
  Polynomial differential() const {
    if (size() == 1) { return Polynomial(1, T()); }
    Polynomial ret(size() - 1, T());
    for(int i=1; i<size(); ++i) {
      ret[i - 1] = coefficient[i] * i;
    }
    return ret;
  }
  T calc(T x) const {
    T ans = 0;
    for (int i = size() - 1; i >= 0; i--) {
      ans *= x;
      ans += coefficient[i];
    }
    return ans;
  }
  bool operator==(const Polynomial<T> &rhs) const {
    return coefficient == rhs.coefficient;
  }
};

template <class T>
ostream &operator<<(ostream &os, const Polynomial<T> &rhs) {
  bool f = 0;
  for (int i=rhs.size()-1; i>=0; --i) {
    if (rhs.coefficient[i] == T()) continue;
    if (rhs.coefficient[i]>T()) {
      if (f) os << "+";
    } else os << "-";
    f = 1;
    if (abs(rhs.coefficient[i]) != T(1) || i == 0)
      os << abs(rhs.coefficient[i]);
    if (i >= 2)
      os << "x^" << i;
    else if (i == 1)
      os << "x";   
  }
  if(!f) os << "0";
  return os;
}

template <class T>
Polynomial<T> gcd(Polynomial<T> a, Polynomial<T> b) {
  if (b == Polynomial<T>()) { return a; }
  return gcd(b, a % b);
}

Polynomial<Rational> rec(string s) {
  // expression
  int nest = 0;
  for (int i=s.size()-1; i>=0; --i) {
    if (s[i] == ')') nest++;
    else if (s[i] == '(') nest--;
    else if (s[i] == '+' && nest == 0)
      return rec(s.substr(0, i)) + rec(s.substr(i+1));
    else if (s[i] == '-' && nest == 0)
      return rec(s.substr(0, i)) - rec(s.substr(i+1));
  }
  if (s[0] == '-')            // unary
    return Polynomial<Rational>(1, Rational(-1)) * rec(s.substr(1));
  // term
  nest = 0;
  REP(i, s.size()-1) {
    if (s[i] == '(') nest++;
    else if (s[i] == ')') nest--;
    if (nest == 0)
      if (!(isdigit(s[i])&&isdigit(s[i+1]) || s[i]=='^' || s[i+1]=='^')) {
        Polynomial<Rational> a = rec(s.substr(0, i+1));
        Polynomial<Rational> b = rec(s.substr(i+1));
        //cout << a.str() << " "  << b.str() << endl;
        Polynomial<Rational> tmp = rec(s.substr(0, i+1)) * rec(s.substr(i+1));
        //cout << tmp.str() << endl;
        return tmp;
      }
  }
  // factor
  nest = 0;
  REP(i, s.size()) {
    if (s[i] == '(') nest++;
    else if (s[i] == ')') nest--;
    if (s[i] == '^' && nest == 0) {
      return rec(s.substr(0, i)).pow(atoi(s.substr(i+1).c_str()));
    }
  }
  // primary
  if (s[0] == '(')
    return rec(s.substr(1, s.size()-2));
  if (s[0] == 'x')
    return Polynomial<Rational>("x",2);
  return Polynomial<Rational>(1, Rational(atoi(s.c_str())));
}

int main() {
//  cout << rec("(x-1)^2").str() << endl;
  string s,t;
  while(cin>>s,s!=".") {
    cin >> t;
    Polynomial<Rational> a = rec(s);
    Polynomial<Rational> b = rec(t);
    Polynomial<Rational> c = gcd(a,b);
    // cout << a << " " << b << endl;
    //cout << c << endl;
    Rational g;
    FOR(it, c.coefficient)
      g = gcd(g, abs(*it));
    if (c[c.size()-1] < 0) g=-g;
    //cout << g << endl;
    Polynomial<Rational> tmp = c / Polynomial<Rational>(1,g);
    //cout << tmp << endl;
    vector<int> v;
    FOR(it, tmp.coefficient)
      v.push_back(it->p);
    cout << Polynomial<int>(v) << endl;
  }
}

Measured memory_bytes: 0