#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...) 0
#endif

struct edge {
    ll u, v, s;
};

vector<edge> g;
vector<vector<int>> t(100010);
vector<ll> val(100010);
vector<bool> pls(100010), used(100010), was(100010);

void dfs(int v, int p = -1) {
    for (int i : t[v]) {
        edge e = g[i];
        int u = e.u + e.v - v;
        if (was[u]) continue;
        was[u] = used[i] = true;
        pls[u] = !pls[v];
        val[u] = e.s - val[v];
        dfs(u, v);
    }
}

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

    int n, m;
    cin >> n >> m;
    rep(i, 0, m) {
        int u, v, s;
        cin >> u >> v >> s;
        u--, v--;
        t[u].emplace_back(i);
        t[v].emplace_back(i);
        g.push_back({u, v, s});
    }
    pls[0] = was[0] = true;
    dfs(0);
    ll pmn = 0, nmn = 1e18;
    rep(i, 0, n) {
        if (pls[i])
            pmn = min(pmn, val[i]);
        else
            nmn = min(nmn, val[i]);
    }
    pmn = 1ll - pmn;
    if (pmn >= nmn) {
        cout << 0 << endl;
        return 0;
    }
    set<ll> nd;
    rep(i, 0, m) if (!used[i]) {
        edge e = g[i];
        if (pls[e.u] != pls[e.v]) {
            if (val[e.u] + val[e.v] != e.s) {
                cout << 0 << endl;
                return 0;
            }
        } else {
            ll foo = e.s - val[e.u] - val[e.v];
            if (foo % 2) {
                cout << 0 << endl;
                return 0;
            }
            foo /= 2;
            if (pls[e.u]) {
                nd.insert(foo);
            } else {
                nd.insert(-foo);
            }
        }
    }
    if (nd.size() > 1) {
        cout << 0 << endl;
        return 0;
    } else if (nd.size() == 1) {
        if (pmn <= *nd.begin() && *nd.begin() < nmn)
            cout << 1 << endl;
        else
            cout << 0 << endl;
        return 0;
    }
    cout << nmn - pmn << endl;

    return 0;
}