#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <map>
#include <sstream>
using namespace std;

using Weight = int;
using Capacity = int;
struct Edge {
	int src, dst; Weight weight; Capacity cap;
	Edge(int s, int d, Weight w, Capacity c) : src(s), dst(d), weight(w), cap(c) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

struct Dinic {
	int n, s, t;
	vector<int> level, prog, que;
	vector<vector<Capacity>> cap, flow;
	vector<vector<int>> g;
	Capacity inf;
	Dinic(const Graph &graph)
		: n(graph.size()),
		cap(n, vector<Capacity>(n)), flow(n, vector<Capacity>(n)),
		g(n, vector<int>()), inf(numeric_limits<Capacity>::max() / 8) {
		for(int i = 0; i < n; i++) {
			for(auto &e : graph[i]) {
				int u = e.src, v = e.dst;
				Capacity c = e.cap;
				cap[u][v] += c; cap[v][u] += c; flow[v][u] += c;
				g[u].push_back(v); g[v].push_back(u);
			}
		}
	}
	inline Capacity residue(int u, int v) { return cap[u][v] - flow[u][v]; }
	Capacity solve(int s_, int t_) {
		this->t = t_, this->s = s_;
		que.resize(n + 1);
		Capacity res = 0;
		while(levelize()) { prog.assign(n, 0); res += augment(s, inf); }
		return res;
	}
	bool levelize() {
		int l = 0, r = 0;
		level.assign(n, -1); level[s] = 0; que[r++] = s;
		while(l != r) {
			int v = que[l++]; if(v == t) break;
			for(const int &d : g[v]) if(level[d] == -1 && residue(v, d) != 0) {
				level[d] = level[v] + 1; que[r++] = d;
			}
		}
		return level[t] != -1;
	}
	Capacity augment(int v, Capacity lim) {
		Capacity res = 0;
		if(v == t) return lim;
		for(int &i = prog[v]; i < (int)g[v].size(); i++) {
			const int &d = g[v][i];
			if(residue(v, d) == 0 || level[v] >= level[d]) continue;
			const Capacity aug = augment(d, min(lim, residue(v, d)));
			flow[v][d] += aug; flow[d][v] -= aug;
			res += aug; lim -= aug;
			if(lim == 0) break;
		}
		return res;
	}
};

typedef pair<int, int> Pii;

vector<int> f(string s) {
	replace(s.begin(), s.end(), '+', ' ');
	//cout << s << endl;
	vector<int> ret(51);

	stringstream ss(s);
	while(ss >> s) {
		stringstream ss2(s);
		if(s.find('x') == string::npos) {
			ss2 >> ret[0];
		}
		else {
			char c;
			int a = 1, L = 1;
			if(s[0] != 'x') ss2 >> a;
			if(s.find('^') != string::npos) {
				ss2 >> c >> c >> L; // x^L
			}
			ret[L] = a;
		}
	}
	return ret;
}

int u[500];
int v[500];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, M;
	while(cin >> N >> M, N) {
		map<Pii, vector<int> > poly;
		for(int i = 0; i < M; i++) {
			string p;
			cin >> u[i] >> v[i] >> p;
			u[i]--, v[i]--;
			poly[Pii(u[i], v[i])] = f(p);
		}

		vector<int> ans(51);
		for(int i = 50; i >= 0; i--) {
			Graph G(N);
			for(auto&& p : poly) {
				int a = p.first.first, b = p.first.second, c = p.second[i];
				G[a].emplace_back(a, b, 0, c);
				G[b].emplace_back(b, a, 0, c);
			}

			Dinic dinic(G);
			int f = dinic.solve(0, N - 1);
			ans[i] = f;

			for(int j = 0; j < M; j++) {
				int a = u[j], b = v[j];
				if(dinic.flow[a][b] != 0 && dinic.cap[a][b] != dinic.flow[a][b]) {
					for(int l = 0; l <= i; l++) {
						poly[Pii(a, b)][l] = 1e6;
					}
				}
			}
		}

		bool flag = false;
		for(int i = 50; i >= 0; i--) {
			if(ans[i] == 0) continue;
			if(flag) cout << "+";
			flag = true;
			if(i == 0 || ans[i] != 1) cout << ans[i];
			if(i != 0) cout << "x";
			if(i >= 2) cout << "^" << i;
		}
		if(!flag) cout << 0;
		cout << "\n";
	}
}

Measured memory_bytes: 3456