#include<bits/stdc++.h>

using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back

const int N = 1e5 + 5, inf = 1e18;
int n, m, x, val[N], l = -inf, r = inf;
bool vis[N], sta[N];
vector< pair<int, int> > gr[N];

void over() { cout << 0; exit(0); }

void dfs(int u, int p) {
    if(vis[u]) return ;
    vis[u] = 1; sta[u] = (sta[p] ^ 1);
    for (auto i : gr[u]) {
        int v = i.fi, w = i.se;
        if(vis[v]) {
            if(sta[u] != sta[v]) {
                if(val[u] + val[v] != w) over();
            }
            else {
                int tmp = w - val[u] - val[v], tmpx;
                if(sta[u]) {
                    if(tmp % 2) over();
                    else tmpx = tmp / 2;
                }
                else {
                    if(tmp % 2) over();
                    else tmpx = -tmp / 2;
                }
                if(!x) {
                    x = tmpx;
                    if(x <= 0) over();
                }
                else if(x != tmpx) over();
            }
        }
        else {
            val[v] = w - val[u];
            dfs(v, u);
        }
    }
}

signed main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int u, v, w; cin >> u >> v >> w;
        gr[u].pb( { v, w } ); gr[v].pb( { u, w } );
    }
    dfs(1, 0);

    if(x) {
        for (int u = 1; u <= n; ++u) {
            if(sta[u]) { if(x + val[u] <= 0) over(); }
            else { if(-x + val[u] <= 0) over(); }
        }
        cout << 1;
        return 0;
    }

    for (int i = 1; i <= n; ++i) {
        if(sta[i]) l = max(l, 1 - val[i]);
        else r = min(r, -1 + val[i]);
    }

    if(l > r) cout << 0;
    else cout << r - l + 1;

    return 0;
}
