#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
#define long ll

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

    ll n, m;
    cin >> n >> m;

    using P = pair<ll, ll>;
    vector<vector<P>> G(n);
    for(ll i=0; i<m; i++) {
        ll u, v, s;
        cin >> u >> v >> s;
        u--, v--;
        G[u].push_back(P(v, s));
        G[v].push_back(P(u, s));
    }

    bool nibu = true;
    ll a, b;
    vector<ll> idx(n, -1);
    {
        auto dfs = [&](auto dfs, ll cur, ll pre) {
            if(!nibu) return;

            for(auto &t: G[cur]) {
                ll v;
                tie(v, ignore) = t;
                if(v == pre) continue;
                if(idx[v] > -1 && ~(idx[v]^idx[cur])&1) {
                    a = v, b = cur;
                    nibu = false;
                }
                if(idx[v] == -1) {
                    idx[v] = idx[cur]+1;
                    dfs(dfs, v, cur);
                }
            }
        };
        dfs(dfs, 0, -1);
    }

    if(nibu) {
        vector<ll> val(n, -1e18);
        auto dfs = [&](auto dfs, ll cur, ll pre) -> void {
            for(auto &t: G[cur]) {
                ll v, s;
                tie(v, s) = t;

                if(v == pre) continue;
                if(val[v] != -1e18) continue;
                val[v] = s - val[cur];
                dfs(dfs, v, cur);
            }
        };
        val[0] = 0;
        dfs(dfs, 0, -1);

        for(ll i=0; i<n; i++) {
            for(auto &t: G[i]) {
                ll v, s;
                tie(v, s) = t;
                if(val[i] + val[v] != s) {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }

        ll zm = 1e18, om = 1e18;
        for(ll i=0; i<n; i++) {
            ll &x = (idx[i] & 1 ? zm : om);
            x = min(x, val[i]);
        }
        om += zm - 1;
        cout << max(0ll, om) << endl;
    } else {
        vector<ll> cyc = {a};

        {
            set<ll> visited;
            auto dfs = [&](auto dfs, ll cur, ll pre) -> bool {
                if(cur == b) return true;
                if(visited.count(cur)) return false;
                visited.insert(cur);
                for(auto &t: G[cur]) {
                    ll v, s;
                    tie(v, s) = t;

                    if(v == pre) continue;
                    if(~(idx[cur]^idx[v])&1) continue;
                    if(dfs(dfs, v, cur)) {
                        cyc.push_back(v);
                        return true;
                    }
                }
                return false;
            };
            dfs(dfs, a, -1);
        }

        vector<ll> val(n, -1e18);

        ll s = 0, u = 0;
        for(auto &t: G[a])
            if(t.first == cyc.back())
                s += t.second;
        for(ll i=0; i<cyc.size()-1; i++)
            for(auto &t: G[cyc[i]])
                if(t.first == cyc[i+1])
                    s += t.second;
        s /= 2;
        for(ll i=1; i<cyc.size()-1; i+=2)
            for(auto &t: G[cyc[i]])
                if(t.first == cyc[i+1])
                    u += t.second;

        val[a] = s - u;
        {
            auto dfs = [&](auto dfs, ll cur, ll pre) -> void {
                for(auto &t: G[cur]) {
                    ll v, s;
                    tie(v, s) = t;

                    if(v == pre) continue;
                    if(val[v] != -1e18) continue;
                    val[v] = s - val[cur];
                    dfs(dfs, v, cur);
                }
            };
            dfs(dfs, a, -1);

            for(ll i=0; i<n; i++) {
                for(auto &t: G[i]) {
                    ll v, s;
                    tie(v, s) = t;
                    if(val[i] + val[v] != s) {
                        cout << 0 << endl;
                        return 0;
                    }
                }
            }
            for(ll i=0; i<n; i++) {
                if(val[i] <= 0) {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
        cout << 1 << endl;
    }
}


Measured memory_bytes: 18040