// #define _GLIBCXX_DEBUG
    #include <bits/stdc++.h>
    #define mp make_pair
    #define pb push_back
    #define ll long long
    #define pll pair<ll, ll>
    #define pii pair<int, int>
    #define ld long double
    #define pas(v) cout << #v << ' ' << v << endl
    using namespace std;
     
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0); cout.tie(0);
        cout.precision(30);
        int n, m; cin >> n >> m;
        
        vector<vector<int>> g(n);
        vector<vector<ll>> s(n);
     
        for (int i = 0; i < m; i++) {
            int u, v;
            ll a;
            cin >> u >> v >> a;
            u--, v--;
            g[v].pb(u);
            g[u].pb(v);
            s[v].pb(a);
            s[u].pb(a);
        }
     
        vector<int> used(n, 0);
     
        ll trig_sum0 = -1e18, trig_sum1 = -1e18;
        int good = 1;
     
        vector<ll> basic(n);
        vector<int> dolya(n);
        function<void(int, int)> dfs = [&](int v, int col) {
            basic[v] = col;
            for (int i = 0; i < g[v].size(); i++) {
                int u = g[v][i];
                ll ss = s[v][i];
                if (!used[u]) {
                    used[u] = 1;
                    dolya[u] = 1 - dolya[v]; 
                    dfs(u, ss - col);
                } else {
                    if (dolya[v] == dolya[u]) {
                        if (dolya[v] == 0)
                            if (trig_sum0 == -1e18) trig_sum0 = ss - basic[u] - basic[v], trig_sum1 = -trig_sum0;
                            else if (trig_sum0 != ss - basic[u] - basic[v]) good = 0;
                        
                        if (dolya[v] == 1)
                            if (trig_sum1 == -1e18) trig_sum1 = ss - basic[u] - basic[v], trig_sum0 = -trig_sum1;
                            else if (trig_sum1 != ss - basic[u] - basic[v]) good = 0;
                    } else {
                        if (basic[v] + basic[u] != ss) good = 0;
                    }
                }
            }
        };
     
        used[0] = 1;
        basic[0] = 0;
        dolya[0] = 0;
     
        dfs(0, 0);
     
        if (!good) {
            cout << 0;
            return 0;
        }
     
        if (trig_sum0 != -1e18) {
            if (trig_sum0 % 2 != 0) {
                cout << 0;
                return 0;
            }
            for (int i = 0; i < n; i++) {
                if (dolya[i] == 0) {
                    if (trig_sum0 / 2 + basic[i] < 1) {
                        cout << 0;
                        return 0;
                    } 
                } else {
                    if (trig_sum1 / 2 + basic[i] < 1) {
                        cout << 0;
                        return 0;
                    } 
                }
            }
            cout << 1;
            return 0;
        }
     
        ll mn0 = 1e18, mn1 = 1e18;
        for (int i = 0; i < n; i++) {
            if (dolya[i] == 0) {
                mn0 = min(mn0, basic[i]);
            } else {
                mn1 = min(mn1, basic[i]);
            }
        }
     
        // cout << mn0 << ' ' << mn1 << endl;
     
        cout << max(0LL, mn1 + mn0 - 1);
     
     
        return 0;
    }

Measured memory_bytes: 20608