//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ld long double
#define al(a) (a).begin(),(a).end()
#define mk make_pair
#define check cout<<"?"<<endl;

ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;

int main(){
    ll n,m; cin>>n>>m;
    vector<vector<pair<ll,ll>>> path(n);
    rep(i,m){
        ll u,v,s; cin>>u>>v>>s;
        u--; v--;
        path[u].push_back(mk(v,s));
        path[v].push_back(mk(u,s));
    }
    queue<ll> que; que.push(0);
    vector<ll> dis(n,-1),num(n,0);
    dis[0]=0; num[0]=1;
    ll p;
    while(!(que.empty())){
        p=que.front();
        que.pop();
        for(auto to:path[p]){
            if(dis[to.first]==-1){
                dis[to.first]=dis[p]+1;
                que.push(to.first);
                num[to.first]=to.second-num[p];
            }
        }
    }
    bool ocyc=false;
    ll u,v,unum;
    rep(i,n)for(auto to:path[i]){
        if(dis[i]%2==dis[to.first]%2){
            ocyc=true;
            u=i,v=to.first;
            unum=to.second;
        }
    }
    //rep(i,n) cout<<num[i]<<" ";
    //cout<<endl;
    if(ocyc){
        map<ll,ll> nodes;
        p=u; nodes[v]=u;
        for(ll i=dis[u];i>=1;i--){
            for(auto to:path[p]){
                if(dis[to.first]==i-1){
                    unum+=to.second;
                    nodes[p]=to.first;
                    p=to.first;
                    break;
                }
            }
            //cout<<unum<<endl;
        }
        p=v; bool ex=false;
        for(ll i=dis[v];i>=1;i--){
            for(auto to:path[p]){
                if(dis[to.first]==i-1){
                    if(ex){
                        p=to.first;
                        unum-=to.second;
                        break;
                    }
                    if(nodes.count(to.first)) ex=true;
                    unum+=to.second;
                    nodes[to.first]=p;
                    p=to.first;
                    break;
                }
            }
            //cout<<unum<<endl;
        }
        //cout<<unum<<endl;
        if(unum%2){
            cout<<0<<endl;
            return 0;
        }
        unum/=2; p=u;
        set<pair<ll,ll>> edges;
        rep(i,n){
            //cout<<p+1<<endl;
            p=nodes[p];
            if(i%2==0){
                edges.insert(mk(p,nodes[p]));
                //cout<<p+1<<" "<<nodes[p]+1<<endl;
            }
            if(p==v) break;
        }

        rep(i,n)for(auto to:path[i]){
            if(edges.count(mk(i,to.first)))
            unum-=to.second;
        }

        num.assign(n,-INF);
        queue<ll> q;
        q.push(u); num[u]=unum;
        ll ans=1;
        while(!(q.empty())){
            p=q.front(); q.pop();
            for(auto to:path[p]){
                if(num[to.first]==-INF){
                    num[to.first]=to.second-num[p];
                    q.push(to.first);
                }
                else{
                    if(to.second!=num[p]+num[to.first])
                    ans=0;
                }
            }
        }
        rep(i,n) if(num[i]<=0) ans=0;
        //rep(i,n) cout<<num[i]<<" ";
        //cout<<endl;
        //cout<<u<<" "<<v<<endl;
        cout<<ans<<endl;
    }
    else{
        rep(i,n)for(auto to:path[i]){
            if(to.second!=num[i]+num[to.first]){
                cout<<0<<endl;
                return 0;
            }
        }
        ll e_min=INF,o_min=INF;
        rep(i,n){
            if(dis[i]%2) o_min=min(o_min,num[i]);
            else e_min=min(e_min,num[i]);
        }
        cout<<max(0LL,o_min+e_min-1)<<endl;
    }
}

Measured memory_bytes: 18596