#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const ll mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
    ll n,m; cin >> n >> m;
    vector<vpl> G(n);
    vector<tapu> edge(m);
    rep(i,m){
        ll u,v,s; cin >> u >> v >> s; u--; v--;
        G[u].emplace_back(v,s);
        G[v].emplace_back(u,s);
        edge[i] = {u,v,s};
    }
    vl color(n,-1);
    color[0] = 0;
    queue<ll> q;
    q.push(0);
    bool bi = true;
    while(!q.empty()){
        ll u = q.front(); q.pop();
        for(auto v : G[u]){
            if(color[v.first] != -1){
                if(color[v.first] != color[u]) continue;
                bi = false;
                goto BREAK;
            }
            color[v.first] = 1 - color[u];
            q.push(v.first);
        }
    }
    BREAK:
    while(!q.empty()) q.pop();
    q.push(0);
    vpl x(n,{inf,inf});
    x[0] = {0,1};
    while(!q.empty()){
        ll u = q.front(); q.pop();
        for(auto v : G[u]){
            if(x[v.first].second != inf) continue;
            P nxt = {v.second - x[u].first, -x[u].second};
            x[v.first] = nxt;
            q.push(v.first);
        }
    }
    ll t = -1;
    rep(i,m){
        ll u,v,s;
        tie(u,v,s) = edge[i];
        P p = {x[u].first + x[v].first, x[u].second + x[v].second};
        if(p.second == 0){
            if(p.first != s){
                cout << 0 << endl;
                return 0;
            }
        }else{
            ll a = s - p.first;
            ll b = p.second;
            if(a * b < 0 || abs(a) % abs(b) != 0){
                cout << 0 << endl;
                return 0;
            }
            if(t != -1 && t != abs(a) / abs(b)){
                cout << 0 << endl;
                return 0;
            }
            t = abs(a) / abs(b);
        }
    }
    if(bi){
        ll mn = 1, mx = (ll)1e9;
        rep(i,n){
            if(x[i].second == -1) chmin(mx, x[i].first - 1);
            else chmax(mn, -x[i].first + 1);
        }
        cout << max(0LL, mx - mn + 1) << endl;
    }else{
        vl num(n,inf);
        num[0] = t;
        q.push(0);
        while(!q.empty()){
            ll u = q.front(); q.pop();
            for(auto v : G[u]){
                if(num[v.first] != inf){
                    if(num[u] + num[v.first] != v.second){
                        cout << 0 << endl;
                        return 0;
                    }
                }else{
                    num[v.first] = v.second - num[u];
                    q.push(v.first); 
                }
            }
        }
        rep(i,n){
            if(num[i] < 0){
                cout << 0 << endl;
                return 0;
            }
        }
        cout << 1 << endl;
    }
}

Measured memory_bytes: 16596