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

using ll = long long;
constexpr ll inf = 1e18;

struct edge {
    int to;
    ll s;
};

using edges = vector<edge>;
using graph = vector<edges>;

int main() {
    int n, m;
    cin >> n >> m;
    graph g(n);
    for(int i = 0; i < m; ++i) {
        int u, v, s;
        cin >> u >> v >> s;
        g[u - 1].push_back(edge{v - 1, s});
        g[v - 1].push_back(edge{u - 1, s});
    }

    vector<bool> vis(n);
    vector<ll> sign(n), a(n);
    // sign[i] == 1:  assign a + t to val[i] (this is also mean t > -a)
    // sign[i] == -1: assign b - t to val[i] (this is also mean t < b)
    // so when val[i] <- a + t and b - t, t must equals (b - a) / 2
    ll mi = 1, ma = inf; // t is in [mi, ma)
    function<void(int)> dfs = [&] (int v) {
        vis[v] = true;
        for(auto& e : g[v]) {
            ll to = e.to, sum = e.s;
            ll nsign = -sign[v], na = sum - a[v];
            if(vis[to]) { // loop
                if(sign[to] == nsign) { // even loop
                    if(a[to] == na) continue; // should be assigned same value
                    mi = inf;
                    ma = 0;
                    continue;
                } else { // odd loop
                    if((a[to] + na) & 1) { // check parity
                        mi = inf;
                        ma = 0;
                        continue;
                    } else {
                        ll v = (sign[to] == 1 ? na - a[to] : a[to] - na);
                        mi = max(mi, v / 2);
                        ma = min(ma, v / 2 + 1);
                    }
                }
            } else {
                sign[to] = nsign, a[to] = na;
                if(sign[to] == 1) mi = max(mi, -a[to] + 1);
                else              ma = min(ma, a[to]);
                dfs(to);
            }
        }
    };
    sign[0] = 1;
    dfs(0);
    cout << max(0LL, ma - mi) << endl;
}
