#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long; 
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T>
struct edge {
    int src,to;
    T cost;

    edge() = default;
    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
    bool operator<(const edge &e) const {
        return cost < e.cost;
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M; cin >> N >> M;
    vector<vector<edge<ll>>> G(N);
    rep(i,M) {
        int u,v,s; cin >> u >> v >> s;
        --u; --v;
        G[u].emplace_back(v,s);
        G[v].emplace_back(u,s);
    }

    vector<ll> color(N,-1),A(N);
    color[0] = 0;
    A[0] = 1;
    int z = -1;
    auto func=[]() {
        cout << 0 << ln;
        exit(0);
    };
    auto dfs1=[&](auto self, int v, int pv)->bool {
        for (auto &e : G[v]) {
            if (e.to==pv) continue;
            if (color[e.to]!=-1) {
                if (color[e.to]==color[v]) {
                    if (z==-1) {
                        if (abs(e.cost-(A[v]+A[e.to]))%2 != 0) func();
                        else {
                            ll d = (e.cost-(A[v]+A[e.to]))/2;
                            A[v] += d;
                            A[e.to] += d;
                            if (A[v] <= 0 or A[e.to] <= 0) func();
                            z = v;
                            return false;
                        }
                    }
                } else {
                    if (e.cost-(A[v]+A[e.to]) != 0) func();
                }
            } else {
                color[e.to] = 1 - color[v];
                A[e.to] = e.cost - A[v];
                if(!self(self,e.to,v)) return false;
            }
        }
        return true;
    };

    dfs1(dfs1,0,-1);
    if (z != -1) {
        rep(i,N) if (i!=z) A[i] = -1;
        auto dfs2=[&](auto self , int v) -> void {
            for (auto &e : G[v]) {
                if (A[e.to] != -1) {
                    if (A[e.to]+A[v]!=e.cost) func();
                } else {
                    A[e.to] = e.cost - A[v];
                    if (A[e.to] <= 0) func();
                    else self(self,e.to);
                }
            }
        };
        dfs2(dfs2,z);
        cout << 1 << ln;
    } else {
        ll MAX = 1e18;
        ll MIN = 0;
        rep(i,N) {
            if (color[i]) {
                chmin(MAX,A[i]-1);
            } else {
                chmax(MIN,-A[i]+1);
            }
        }
        cout << max(0LL,MAX-MIN+1) << ln;
    }
}


Measured memory_bytes: 15616