#include <iostream>
#include <vector>

using namespace std;

struct edge{
    long to, cost;
};

typedef pair<long, long> P;

vector<edge> G[100000];
bool used[100000];
P ans[100000];

bool dfs(long v, P p){
    used[v] = true;
    ans[v] = p;
    //cout << v << ' ' << p.first << ' ' << p.second << endl;
    for(int i = 0; i < G[v].size(); i++){
        if(!used[G[v][i].to]){
            if(p.first == -1) dfs(G[v][i].to, P(1, G[v][i].cost-p.second));
            else dfs(G[v][i].to, P(-1, G[v][i].cost-p.second));
        }
    }
}

int main(){
    long n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        long u, v, s;
        cin >> u >> v >> s;
        u--;
        v--;
        G[u].push_back(edge{v, s});
        G[v].push_back(edge{u, s});
    }
    long x_min = 0;
    long x_max = 10e+10;
    long x = -1;
    dfs(0, P(1, 0));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < G[i].size(); j++){
            //cout << "i:" << i << " j:" << j << endl;
            //cout << ans[i].first << ' ' <<  ans[G[i][j].to].first << endl;
            if(ans[i].first == -1 && ans[G[i][j].to].first == -1){
                long tmp = (ans[i].second+ans[G[i][j].to].second-G[i][j].cost)/2;
                if(tmp <= 0){
                    cout << 0 << endl;
                    return 0;
                }else{
                    if(x == -1) x = tmp;
                    else if(x != tmp){
                        cout << 0 << endl;
                        return 0;
                    }
                }
                x_max = min(x_max, ans[i].second);
                x_max = min(x_max, ans[G[i][j].to].second);
            }else if(ans[i].first == -1 && ans[G[i][j].to].first == 1){
                //cout << ans[i].second+ans[G[i][j].to].second << ' ' << G[i][j].cost << endl;
                if((ans[i].second+ans[G[i][j].to].second) != G[i][j].cost){
                    cout << 0 << endl;
                    return 0;
                }
                x_max = min(x_max, ans[i].second);
                x_min = max(x_min, -ans[G[i][j].to].second);                
            }else if(ans[i].first == 1 && ans[G[i][j].to].first == -1){
                //cout << ans[i].second+ans[G[i][j].to].second << ' ' << G[i][j].cost << endl;
                if((ans[i].second+ans[G[i][j].to].second) != G[i][j].cost){
                    cout << 0 << endl;
                    return 0;
                }
                x_min = max(x_min, -ans[i].second);
                x_max = min(x_max, ans[G[i][j].to].second); 
            }else{
                long tmp = (-ans[i].second-ans[G[i][j].to].second+G[i][j].cost)/2;
                if(tmp <= 0){
                    cout << 0 << endl;
                    return 0;
                }else{
                    if(x == -1) x = tmp;
                    else if(x != tmp){
                        cout << 0 << endl;
                        return 0;
                    }
                }
                x_min = max(x_min, -ans[i].second);
                x_min = max(x_min, -ans[G[i][j].to].second);                
            }
        }
    }
    //cout << x_min << ' ' << x_max << endl;
    //cout << x << endl;
    if(x == -1){
        cout << max((long)0, x_max-x_min-1) << endl;
    }else{
        if(x_min <= x && x_max >= x){
            cout << 1 << endl;
        }else{
            cout << 0 << endl;
        }
    }
}

Measured memory_bytes: 14208