#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll,ll>;
using um = unordered_map<ll,ll>;
#define fl cout<<flush;
#define endl '\n'
template <typename T> inline void prt(T v){cout<<v<<'\n';}
template <typename T> inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ld pi=3.14159265358979323846;
const ld eps=1e-10;

#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T> 
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    ll m;
    cin >> m;
    vector<ll> u(m,0);
    vector<ll> v(m,0);
    vector<ll> s(m,0);
    vector<vector<P>> G(n, vector<P>(0));
    for(ll i=0;i<m;++i){
        cin>>u[i]>>v[i]>>s[i];
        u[i]--;v[i]--;
        G[u[i]].emplace_back(P(v[i],s[i]));
        G[v[i]].emplace_back(P(u[i],s[i]));
    }
    vector<P> num(n,P(INF,INF));
    //頂点０の数字をXと定める
    num[0]=P(1,0);
    set<ll> X;
    X.insert(INF);
    function<bool(ll)> dfs=[&](ll x){
        for(auto p: G[x]){
            ll to=p.first;
            ll sum=p.second;
            if(num[to]==P(INF,INF)){
                num[to]=P(-num[x].first,sum-num[x].second);
                if(!dfs(p.first))return 0;
            }
            else if(num[to].first==num[x].first){
                if(llabs(sum-num[to].second-num[x].second)%2!=0)return 0;
                if(num[to].first==1)X.insert((sum-num[to].second-num[x].second)/2);
                if(num[to].first==-1)X.insert(-(sum-num[to].second-num[x].second)/2);
            }
            else if(num[to].second+num[x].second!=sum)return 0;
        }
        return 1;
    };
    if(!dfs(0)){
        prt(0);
        return 0;
    }
    if(X.size()>2){prt(0);return 0;}
    if(X.size()==2){
        ll rx=*X.begin();
        for(auto p: num){
            if(rx*p.first+p.second<=0){prt(0);return 0;}
        }
        prt(1);return 0;
    }
    if(X.size()==1){
        ll Min=-INF,Max=INF;
        for(auto p: num){
            if(p.first==-1){
                chmin(Max,p.second-1);
            }
            else if(p.first==1){
                chmax(Min,-p.second+1);
            }
        }
        if(Min>Max){
            prt(0);return 0;
        }
        else {
            prt(Max-Min+1);return 0;
        }
    }

    return 0;
}



Measured memory_bytes: 27196