import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {
  import scala.io.StdIn._

  solve
  def solve: Unit = {
    val a = readLine.trim
    if (a == ".") return
    val b = readLine.trim
    (a.toList, b.toList) match {
      case (ea Expression Nil, eb Expression Nil) ⇒ println(gcd(ea, eb))
      case _ ⇒ ???
    }
    solve
  }
  def gcd(a: Polynomial, b: Polynomial): Polynomial = {
    if (b.isZero) {
      a.toCoPrime
    }
    else {
      val aMax = a.degree
      val bMax = b.degree
      if (aMax >= bMax) {
        val aCoef = a.coefficient(aMax)
        val bCoef = b.coefficient(bMax)
        val lcm = aCoef / gcd(aCoef, bCoef) * bCoef
        val aNext = a.multiple(lcm / aCoef)
        val bNext = b.multiple(Factor(aMax - bMax, lcm / bCoef))
        gcd(b, aNext.subtract(bNext))
      }else {
        gcd(b, a)
      }
    }
  }
  def gcd(a: Long, b: Long): Long = {
    if (b == 0) a else gcd(b, a % b)
  }

  object Expression {
    def unapply(arg: List[Char]): Option[(Polynomial, List[Char])] = {
      arg match {
        case t Term r ⇒
          Some(takeTerms(r, t))
        case _ ⇒ None
      }
    }
    @tailrec
    private[this] def takeTerms(arg: List[Char], prev: Polynomial): (Polynomial, List[Char]) = {
      arg match {
        case '+'::(t Term r) ⇒
          takeTerms(r, prev.add(t))
        case '-'::(t Term r) ⇒
          takeTerms(r, prev.subtract(t))
        case _ ⇒ (prev, arg)
      }
    }
  }
  object Primary{
    def unapply(arg: List[Char]): Option[(Polynomial, List[Char])] = {
      arg match {
        case '('::(e Expression ')'::r) ⇒
          Some(e, r)
        case 'x'::r ⇒
          Some(Factor(1, 1), r)
        case n Number r ⇒
          Some(Factor(0, n), r)
        case _ ⇒ None
      }
    }
  }
  object Number{
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg.span(_.isDigit) match {
        case (Nil, _) ⇒ None
        case (n, r) ⇒
          Some(n.foldLeft(0){(a, b) ⇒ a * 10 + b.asDigit}, r)
      }
    }
  }

  sealed trait Polynomial {
    def isZero: Boolean
    def degree: Int
    def coefficient(exp: Int): Long
    def multiple(coefficient: Long): Polynomial
    def multiple(that: Polynomial): Polynomial = {
      that match {
        case m: MultiTerm ⇒ multiple(m)
        case f: Factor ⇒ multiple(f)
      }
    }
    def multiple(that: MultiTerm): Polynomial
    def multiple(that: Factor): Polynomial
    def subtract(that: Polynomial): Polynomial = {
      that match {
        case m: MultiTerm ⇒ subtract(m)
        case f: Factor ⇒ subtract(f)
      }
    }
    def subtract(that: MultiTerm): Polynomial
    def subtract(that: Factor): Polynomial
    def add(that: Polynomial): Polynomial = {
      that match {
        case m: MultiTerm ⇒ add(m)
        case f: Factor ⇒ add(f)
      }
    }
    def add(that: MultiTerm): Polynomial
    def add(that: Factor): Polynomial
    def allCoefficients: Iterable[Long]
    def toCoPrime: Polynomial
  }
  object Term {
    def unapply(arg: List[Char]): Option[(Polynomial, List[Char])] = {
      arg match {
        case '-'::r ⇒
          Some(takeFactors(r, Factor(0, -1)))
        case f Factor r ⇒
          Some(takeFactors(r, f))
        case p Primary r ⇒
          Some(takeFactors(r, p))
        case _ ⇒
          None
      }
    }
    @tailrec
    private[this] def takeFactors(arg: List[Char], prev: Polynomial): (Polynomial, List[Char]) = {
      arg match {
        case f Factor r ⇒
          takeFactors(r, prev.multiple(f))
        case p Primary r ⇒
          takeFactors(r, prev.multiple(p))
        case _ ⇒
          (prev, arg)
      }
    }
  }
  object MultiTerm {
    private[this] val expMax = 10
    def apply(terms: Factor*): MultiTerm = {
      val map = terms.groupBy(_.exponent).mapValues(_.map(_.coefficient).sum)
      new MultiTerm((0 to expMax).map(e ⇒ Factor(e, map.getOrElse(e, 0))).toArray)
    }
  }
  class MultiTerm private(private val terms: Array[Factor]) extends Polynomial {
    override def isZero: Boolean = terms.forall(_.isZero)
    override def degree: Int = terms.lastIndexWhere(_.coefficient != 0)
    override def coefficient(exp: Int): Long = terms(exp).coefficient
    override def multiple(coefficient: Long): Polynomial = new MultiTerm(terms.map(f ⇒ f.multiple(coefficient)))
    override def multiple(that: MultiTerm): Polynomial = MultiTerm(terms.flatMap(l ⇒ that.terms.map(r ⇒ l.multiple(r))):_*)
    override def multiple(that: Factor): Polynomial = new MultiTerm(((0 until that.exponent).map(e ⇒ Factor(e, 0)) ++ terms.indices.dropRight(that.exponent).map(i ⇒ terms(i).multiple(that))).toArray)
    override def subtract(that: MultiTerm): Polynomial = new MultiTerm(terms.indices.map(e ⇒ Factor(e, terms(e).coefficient - that.terms(e).coefficient)).toArray)
    override def subtract(that: Factor): Polynomial = new MultiTerm(terms.indices.map(e ⇒ if (e == that.exponent) Factor(e, terms(e).coefficient - that.coefficient) else terms(e)).toArray)
    override def add(that: MultiTerm): Polynomial = new MultiTerm(terms.indices.map(e ⇒ Factor(e, terms(e).coefficient + that.terms(e).coefficient)).toArray)
    override def add(that: Factor): Polynomial = new MultiTerm(terms.indices.map(e ⇒ if (e == that.exponent) Factor(e, terms(e).coefficient + that.coefficient) else terms(e)).toArray)
    override def toString: String = {
      val indices = terms.indices.filterNot(i ⇒ terms(i).isZero).reverse
      s"${terms(indices.head).toString}${indices.tail.map(e ⇒ terms(e).toStringWithSignum).mkString}"
    }
    override def allCoefficients: Iterable[Long] = terms.indices.filterNot(i ⇒ terms(i).isZero).map(i ⇒ terms(i).coefficient)
    override def toCoPrime: Polynomial = {
      val signum = coefficient(degree).signum
      val g = allCoefficients.map(math.abs).reduce(gcd(_, _)) * signum
      new MultiTerm(terms.map(f ⇒ Factor(f.exponent, f.coefficient / g)))
    }
  }
  object Factor {
    def apply(exponent: Int, coefficient: Long): Factor = {
      new Factor(exponent, coefficient)
    }
    def unapply(arg: List[Char]): Option[(Polynomial, List[Char])] = {
      arg match {
        case n Number r ⇒
          Some(Factor(0, n), r)
        case p Primary rest ⇒
          rest match {
            case '^'::(n Number r) ⇒ Some((1 until n).foldLeft(p){(a, b) ⇒ a.multiple(p)}, r)
            case _ ⇒ Some(p, rest)
          }
        case _ ⇒ None
      }
    }
  }
  class Factor(val exponent: Int, val coefficient: Long) extends Polynomial {
    override def degree: Int = exponent
    override def multiple(coefficient: Long): Factor = new Factor(exponent, coefficient * this.coefficient)
    override def toString: String = {
      if (exponent == 0) {
        if (coefficient == 0) ""
        else coefficient.toString
      }else if (exponent == 1){
        if (coefficient == 1) "x"
        else if (coefficient == -1) "-x"
        else s"${coefficient}x"
      }else if (coefficient == 1)
        s"x^$exponent"
      else if (coefficient == -1)
        s"-x^$exponent"
      else
        s"${coefficient}x^$exponent"
    }
    def toStringWithSignum: String = {
      if (coefficient > 0) s"+$toString"
      else toString
    }
    override def coefficient(exp: Int): Long = if (exponent == exp) coefficient else 0
    override def multiple(that: MultiTerm): Polynomial = that.multiple(this)
    override def multiple(that: Factor): Factor = new Factor(exponent + that.exponent, coefficient * that.coefficient)
    override def subtract(that: MultiTerm): Polynomial = that.multiple(-1).add(this)
    override def subtract(that: Factor): Polynomial = if (exponent == that.exponent) new Factor(exponent, coefficient - that.coefficient) else MultiTerm(this, that.multiple(-1))
    override def add(that: MultiTerm): Polynomial = that.add(this)
    override def add(that: Factor): Polynomial = if (exponent == that.exponent) new Factor(exponent, coefficient + that.coefficient) else MultiTerm(this, that)
    override def isZero: Boolean = coefficient == 0
    override def allCoefficients: Iterable[Long] = List(coefficient)
    override def toCoPrime: Polynomial = Factor(exponent, 1)
  }

  implicit class Scope[T](val value: T) extends AnyVal{
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}
