#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;
}

Measured memory_bytes: 0