#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i,n) for(ll i=0;i<n;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define fi first
#define se second
#define endl "\n"

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){int len=d.size();rep(i,len){s<<d[i];if(i<len-1) s<<" ";}return s;}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){int len=d.size();rep(i,len){s<<d[i]<<endl;}return s;}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T> ostream& operator<<(ostream& s,const multiset<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){s<<"{"<<endl;for(auto itr=m.begin();itr!=m.end();++itr){s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;}s<<"}"<<endl;return s;}

const ll mod=1'000'000'007;
const ll inf=1'000'000'000'000'000'00;
const int INF=1'000'000'000;
const double EPS=1e-10;
const double PI=acos(-1);

struct edge{
    ll to,id;
};

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n,m;
    cin>>n>>m;
    vector<ll> s(m);
    vector<vector<edge>> g(n);
    rep(i,m){
        ll u,v;
        cin>>u>>v>>s[i];
        u--,v--;
        g[u].push_back({v,i});
        g[v].push_back({u,i});
    }
    ll lower=0,upper=inf+10,x=-1;
    vector<ll> dist(n,-1),dp(n,-1);
    dist[0]=0,dp[0]=0;
    queue<ll> que;
    que.push(0);
    while(!que.empty()){
        ll v=que.front();
        que.pop();
        for(auto [to,id]:g[v]){
            if(dist[to]==-1){
                dist[to]=dist[v]^1;
                dp[to]=s[id]-dp[v];
                if(dist[to]==1){
                    chmin(upper,dp[to]);
                }else{
                    chmax(lower,-dp[to]);
                }
                que.push(to);
            }else{
                if((dist[v]^dist[to])==1){
                    if(s[id]!=dp[v]+dp[to]){
                        cout<<0<<endl;
                        return 0;
                    }
                }else{
                    if(dist[v]==0 && dist[to]==0){
                        ll now=s[id]-(dp[v]+dp[to]);
                        if(now%2 || now<=0){
                            cout<<0<<endl;
                            return 0;
                        }
                        if(x!=-1 && x!=now/2){
                            cout<<0<<endl;
                            return 0;
                        }
                        x=now/2;
                    }
                    if(dist[v]==1 && dist[to]==1){
                        ll now=(dp[v]+dp[to])-s[id];
                        if(now%2 || now<=0){
                            cout<<0<<endl;
                            return 0;
                        }
                        if(x!=-1 && x!=now/2){
                            cout<<0<<endl;
                            return 0;
                        }
                        x=now/2;
                    }
                }
            }
        }
    }
    if(x==-1){
        cout<<max(0ll,upper-lower-1)<<endl;
    }else{
        if(lower<x && x<upper){
            cout<<1<<endl;
        }else{
            cout<<0<<endl;
        }
    }
}

Measured memory_bytes: 13480