The trafic on the Internet is increasing these days due to smartphones. The wireless carriers have to enhance their network infrastructure.

The network of a wireless carrier consists of a number of base stations and lines. Each line connects two base stations bi-directionally. The bandwidth of a line increases every year and is given by a polynomial f(x) of the year x.

Your task is, given the network structure, to write a program to calculate the maximal bandwidth between the 1-st and N-th base stations as a polynomial of x.



Input

The input consists of multiple datasets. Each dataset has the following format:

N M
u1 v1 p1
...
uM vM pM


The first line of each dataset contains two integers N (2 ≤ N ≤ 50) and M (0 ≤ M ≤ 500), which indicates the number of base stations and lines respectively. The following M lines describe the network structure. The i-th of them corresponds to the i-th network line and contains two integers ui and vi and a polynomial pi. ui and vi indicate the indices of base stations (1 ≤ ui, vi ≤ N); pi indicates the network bandwidth.

Each polynomial has the form of:

aLxL + aL-1xL-1 + ... + a2x2 + a1x + a0

where L (0 ≤ L ≤ 50) is the degree and ai's (0 ≤ i ≤ L, 0 ≤ ai ≤ 100) are the coefficients. In the input,

* each term aixi (for i ≥ 2) is represented as <ai>x^<i>
* the linear term (a1x) is represented as <a1>x;
* the constant (a0) is represented just by digits;
* these terms are given in the strictly decreasing order of the degrees and connected by a plus sign ("+");
* just like the standard notations, the <ai> is omitted if ai = 1 for non-constant terms;
* similarly, the entire term is omitted if ai = 0 for any terms; and
* the polynomial representations contain no space or characters other than digits, "x", "^", and "+".



For example, 2x2 + 3x + 5 is represented as 2x^2+3x+5; 2x3 + x is represented as 2x^3+x, not 2x^3+0x^2+1x+0 or the like. No polynomial is a constant zero, i.e. the one with all the coefficients being zero.

The end of input is indicated by a line with two zeros. This line is not part of any dataset.

Output

For each dataset, print the maximal bandwidth as a polynomial of x. The polynomial should be represented in the same way as the input format except that a constant zero is possible and should be represented by "0" (without quotes).

Example

Input

3 3
1 2 x+2
2 3 2x+1
3 1 x+1
2 0
3 2
1 2 x
2 3 2
4 3
1 2 x^3+2x^2+3x+4
2 3 x^2+2x+3
3 4 x+2
0 0


Output

2x+3
0
2
x+2
#include <set>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <sstream>
#include <climits>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

struct Edge{
  int cap; // capacity
  int to;
  int rev; // reverse edge id
  int id;
  Edge(){}
  Edge(int c, int t, int r, int i) :
    cap(c), to(t), rev(r), id(i){}
};

template<class E> // Edge type
class Graph{
public:
  typedef std::vector<std::vector<E> > G;

private:
  G g;

public:
  Graph(int n) : g(G(n)) {}

  void addEdge(int from, int to, int cap, int id){
    g[from].push_back(E(cap, to, g[to].size(), id));
    g[to].push_back(E(cap, from, g[from].size() - 1, id));
  }

  G &getRowGraph(){
    return g;
  }
};

template<class E>
class Dinic{
  typedef typename Graph<E>::G G;
  G &g;
  std::size_t n; // size of graph

  std::vector<int> level;
  std::vector<int> iter;

  // other utilities

  // search length of shortest path from s
  void bfs(int s){
    std::queue<int> que;
    level = std::vector<int>(n, -1);

    level[s] = 0;
    que.push(s);

    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i = 0; i < (int)g[v].size(); i++){
        E &e = g[v][i];
        if(e.cap > 0 && level[e.to] < 0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

  // search path
  int dfs(int v, int t, int f){
    if(v == t) return f;
    for(int &i = iter[v]; i < (int)g[v].size(); i++){
      E &e = g[v][i];
      if(e.cap > 0 && level[v] < level[e.to]){
        int d = dfs(e.to, t, min(f, e.cap));
        if(d > 0){
          e.cap -= d;
          g[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(Graph<E> &graph) : g(graph.getRowGraph()){
    n = g.size();
  }

  // Max flow of the flow from s to t.
  int solve(int s, int t){
    int flow = 0;
    while(true){
      int f;
      bfs(s);
      if(level[t] < 0) return flow;
      iter  = std::vector<int>(n, 0);
      while((f = dfs(s, t, INT_MAX)) > 0){
        flow += f;
      }
    }
  }
};

template<class E>
int dinic(Graph<E> &g, int s, int d){
  return Dinic<E>(g).solve(s, d);
}

vector<string> split(string str, char sp){
  vector<string> ret;

  while(str.find(sp) != string::npos){
    int pos = (int)str.find(sp);

    ret.push_back(str.substr(0, pos));
    str = str.substr(pos + 1);
  }
  ret.push_back(str);

  return ret;
}

int main(){
  int n, m;
  while(cin >> n >> m, n + m){
    vector<int> u(m);
    vector<int> v(m);
    vector<vector<int> > p(m, vector<int>(51));

    REP(i,m){
      string str;
      cin >> u[i] >> v[i] >> str;
      u[i]--; v[i]--;

      vector<string> tmp = split(str, '+');

      REP(j, tmp.size()){
        int a, l;
        if(tmp[j].find('x') != string::npos){
          if(tmp[j][0] == 'x'){
            if(tmp[j].find('^') != string::npos)
              sscanf(tmp[j].c_str(), "x^%d", &l);
            else
              l = 1;
            a = 1;
          }else{
            if(tmp[j].find('^') != string::npos)
              sscanf(tmp[j].c_str(), "%dx^%d", &a, &l);
            else{
              sscanf(tmp[j].c_str(), "%dx", &a);
              l = 1;
            }
          }
        }else{
          l = 0;
          sscanf(tmp[j].c_str(), "%d", &a);
        }
        // cout << tmp[j] << " : " << a << " " << l << endl;
        p[i][l] = a;
      }
    }

    vector<int> ans(51);
    vector<bool> inf(m);
    for(int i = 50; i >= 0; i--){
      Graph<Edge> g(n);

      REP(j,m){
        int cap = inf[j] ? (1 << 30) : p[j][i];

        if(cap > 0){
          g.addEdge(u[j], v[j], cap, j);
        }
      }

      ans[i] = dinic(g, 0, n - 1);

      const vector<vector<Edge> > &gg(g.getRowGraph());
      vector<int> tmp(m);
      REP(k,gg.size()){
        REP(j,gg[k].size()){
          if(gg[k][j].cap > 0){
            tmp[gg[k][j].id]++;
            if(tmp[gg[k][j].id] == 2){
              inf[gg[k][j].id] = true;
            }
          }
        }
      }
    }

    stringstream ss;
    bool flag = false;

    for(int i = 50; i >= 0; i--){
      if(ans[i] > 0){
        if(!flag){
          flag = true;
        }else{
          ss << "+";
        }

        if(i == 0 || ans[i] != 1) ss << ans[i];
        if(i > 1) ss << "x^" << i;
        if(i == 1) ss << "x";
      }
    }

    string sans = ss.str();
    if(sans.size() == 0) sans = "0";

    cout << sans << endl;
  }
  return 0;
}