#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;

struct edge{
    ll to, cost;
};
vector<vector<edge> > G(100010,vector<edge>(0));
vec score(100010,INF), bip(100010,-1);
ll ng_v = -1, ng_score = INF;
void detS(int v, ll s){
    score[v] = s;
    for(edge e : G[v]){
        if(score[e.to] == INF) {
            detS(e.to, e.cost - s);
        }else if(e.to == ng_v && ng_score == INF && e.cost - s != 0){
            ng_score = e.cost - s;
        }
    }
    return;
}

bool check(){
    rep(i,N){
        for(edge e : G[i]){
            if(score[i] + score[e.to] != e.cost){
                return false;
            }
        }
    }
    return true;
}

bool divide(ll v, ll c){
    bip[v] = c;
    for(edge e : G[v]){
        if(bip[e.to] == -1){
            if(!divide(e.to, c^1)) return false;
        }else if(bip[e.to] == c){
            ng_v = e.to;
            return false;
        }
    }
    return true;
}

int main(){
    cin>>N>>M;
    rep(i,M){
        cin>>A>>B>>K;
        --A, --B;
        edge e;
        e.to = A;
        e.cost = K;
        G[B].push_back(e);
        e.to = B;
        G[A].push_back(e);
    }
    if(divide(0,0)){
        detS(0,0);
        if(check()){
            vec mini(2,INF);
            rep(i,N){
                mini[bip[i]] = min(mini[bip[i]], score[i]);
            }
            cout<<max(0LL, mini[0] + mini[1] - 1)<<endl;
        }else{
            cout<<0<<endl;
        }
    }else{
        detS(ng_v, 0);
        rep(i,N) score[i] = INF;
        detS(ng_v, ng_score/2);
        bool minus = false;
        rep(i,N) if(score[i] <= 0) minus = true;
        cout<<((!minus) && check() ? 1 : 0)<<endl;
    }
}