#include <bits/stdc++.h>
using namespace std;

int number(string s) {
    int ret = 0, pos = 0;
    while (pos < (int)s.size() && isdigit(s[pos])) {
        ret = ret * 10 + (s[pos] - '0');
        pos++;
    }
    return ret;
}

class Capacity {
public:
    int coef[102] = {};
    int dim = 0;

    Capacity(){};
    Capacity(string s) {
        int prev_pos = 0;
        int x_pos = -1, pow_pos = -1;
        for (int i = 0; i <= (int)s.size(); i++) {
            if (i == (int)s.size() || s[i] == '+') {
                auto term = s.substr(prev_pos, i-prev_pos);

                int c = 1;
                if (x_pos != prev_pos) c = number(term);

                int d = 0;
                if (pow_pos != -1) d = number(term.substr(pow_pos-prev_pos+1));
                else if (x_pos != -1) d = 1;

                coef[d] = c;
                dim = max(dim, d);

                x_pos = -1, pow_pos = -1;
                prev_pos = i + 1;
            } else if (s[i] == 'x') {
                x_pos = i;
            } else if (s[i] == '^') {
                pow_pos = i;
            }
        }
    }
    bool is_zero(){
        return (dim == 0 && coef[0] == 0);
    }

    void output() {
        string ret = "";
        for (int i = dim; i >= 0; i--) {
            if (coef[i] == 0 && ret.size() > 0)
                continue;

            if (ret.size() && coef[i] >= 0) ret += "+";
            if (coef[i] > 1 || (i == 0 && coef[i] == 1) || (i == 0 && ret.size() == 0))
                ret += to_string(coef[i]);
            if (i != 0) ret += "x";
            if (i > 1){
                ret += "^";
                ret += to_string(i);
            }
        }
        cout << ret << endl;
    }
};
Capacity operator+(const Capacity &lv, const Capacity &rv) {
    Capacity ret;
    int dim = max(lv.dim, rv.dim);
    for (int i = 0; i <= dim; i++) {
        ret.coef[i] = lv.coef[i] + rv.coef[i];
    }
    ret.dim = dim;
    return ret;
}
Capacity operator-(const Capacity &lv, const Capacity &rv) {
    Capacity ret;

    int dim = max(lv.dim, rv.dim);
    bool erase = true;
    for (int i = dim; i >= 0; i--) {
        ret.coef[i] = lv.coef[i] - rv.coef[i];

        if (erase && ret.coef[i] == 0) dim--;
        else erase = false;
    }
    if (dim < 0) dim = 0;
    ret.dim = dim;
    return ret;
}
bool operator>(const Capacity &lv, const Capacity &rv) {
    if (lv.dim != rv.dim) return lv.dim > rv.dim;
    for (int i = lv.dim; i >= 0; i--) {
        if (lv.coef[i] == rv.coef[i])
            continue;
        return lv.coef[i] > rv.coef[i];
    }
    return false;
}
Capacity min(Capacity &a, Capacity &b) {
    if (a > b) return b;
    return a;
}

struct edge {
    int to;
    Capacity cap;
    int rev;
};

int N, M;
vector<vector<edge>> G;
bool used[51];
Capacity max_cap("101x^101");

void add_edge(int from, int to, const Capacity &cap) {
    G[from].push_back({to, cap, (int)G[to].size()});
    G[to].push_back({from, Capacity(), (int)G[from].size() - 1});
}

Capacity dfs(int v, int t, Capacity f) {
    if (v == N-1) return f;
    used[v] = true;
    for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (!used[e.to] && !e.cap.is_zero()) {
            Capacity d = dfs(e.to, t, min(f, e.cap));
            if (!d.is_zero()) {
                e.cap = e.cap - d;
                G[e.to][e.rev].cap = G[e.to][e.rev].cap + d;
                return d;
            }
        }
    }
    return Capacity();
}

int main() {
    while (cin >> N >> M, N + M) {
        G = vector<vector<edge>>(N);
        for (int i = 0; i < M; i++) {
            int from, to; string exp;
            cin >> from >> to >> exp;
            from--, to--;
            add_edge(from, to, Capacity(exp));
            add_edge(to, from, Capacity(exp));
        }

        Capacity flow;
        while (true)
        {
            memset(used, false, sizeof(used));
            Capacity f = dfs(0, N-1, max_cap);
            if (f.is_zero()) break;
            flow = flow + f;
        }
        flow.output();
    }
}



Measured memory_bytes: 4412