#pragma gcc optimize("Ofast")

#include "bits/stdc++.h"
#define int lld
using namespace std;
typedef int64_t lld;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
typedef pair<int,pll> pip;
typedef pair<pll,int> ppi;
typedef pair<lld,pll> plp;
typedef pair<pll,lld> ppl;
typedef pair<pll,pll> ppp;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define endl '\n'
#define jizz cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
inline void input(int &_x) {
    _x = 0;
    int _tmp = 1; char _tc = getchar();
    while((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
    if(_tc == '-') _tc = getchar(), _tmp = -1;
    while(_tc >= '0' && _tc <= '9') _x = _x*10+(_tc-48), _tc = getchar();
    _x *= _tmp;
}
inline void output(int _x) {
    char _buff[20]; int _f = 0;
    if(_x == 0)putchar('0');
    while(_x > 0)
    {
        _buff[_f++] = _x%10+'0';
        _x /= 10;
    }
    for(_f-=1; _f >= 0; _f--)
        putchar(_buff[_f]);
    putchar('\n');
}
template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<"[";
    for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
    s<<"]";
    return s;
}
template<class T1,class T2>
ostream& operator<<(ostream& out, pair<T1,T2> p) {
    return out << '(' << p.first << ", " << p.second << ')';
}
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) {
     return _out(s,c.begin(),c.end());
 }
#ifdef erd1
    #define pprint(x) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<(#x)<<"="<<(x)<<endl
#else
    #define pprint(x)
#endif

// code starts here
vector<vector<pii>> G;
vector<int> gone;
vector<pii> res;// a+bx;
bool d = false; int X;
void dfs(int i){
    gone[i] = 1;
    for(auto x: G[i]){
        pii proc = {x.ss-res[i].ff, -res[i].ss};
        if(!gone[x.ff])res[x.ff] = proc, dfs(x.ff);
        else if(res[x.ff].ss == proc.ss){
            if(res[x.ff].ff != proc.ff)throw "wrong constraints";
        }else{
            int px = (proc.ss==1?1:-1)*(res[x.ff].ff-proc.ff);
            if(d && X*2 != px)throw ((string)"two constraints: " + to_string(X*2) + " " + to_string(px)).c_str();
            X = px/2, d = 1;
            if(X*2 != px)throw "not div 2";
        }
    }
}
signed main(){
    int n, m;
    cin >> n >> m;
    G.resize(n+1);gone.resize(n+1);res.resize(n+1);
    for(int i = 0; i < m; i++){
        int a, b, s;
        cin >> a >> b >> s;
        G[a].pb({b, s});
        G[b].pb({a, s});
    }
    res[1] = {0, 1};
    try{
        dfs(1);
        if(d){
            for(int i = 1; i <= n; i++){
                if(res[i].ss == 1){if(res[i].ff+X<=0)throw "constraint failed";}
                else{if(res[i].ff-X<=0)throw "constraint failed";}
            }
            cout << 1 << endl;
        }else{
            int mins = INT_MIN, maxs = INT_MAX;
            for(int i = 1; i <= n; i++){
                if(res[i].ss == 1)mins = max(mins, 1-res[i].ff);
                else maxs = min(maxs, res[i].ff-1);
            }

            cout << max((lld)0, maxs-mins+1) << endl;
        }
    }catch(const char* s){
        cout << 0 << endl;
    }
}


Measured memory_bytes: 24192