#include <bits/stdc++.h>
using namespace std;

struct edge{ int to; long long int cost; int id; };
vector<vector<edge>> graph;
vector<bool> used;
vector<vector<set<long long int>>> condition;

void DFS(int v, long long int vi, int parity){
    for(edge e : graph[v]){
        if(used[e.id] == true) continue;

        used[e.id] = true;
        condition[e.to][(parity+1)%2].insert(e.cost - vi);
        DFS(e.to, e.cost-vi, parity+1);
    }
    return ;
}

int main(){
    int n, m;
    cin >> n >> m;

    graph.resize(n);
    used.assign(m, false);
    condition.resize(n, vector<set<long long int>>(2));

    for(int i=0; i<m; i++){
        int u, v;
        long long s;
        cin >> u >> v >> s;
        u -= 1;
        v -= 1;
        graph[u].push_back({v,s,i});
        graph[v].push_back({u,s,i});
    }

    condition[0][0].insert(0);
    DFS(0,0,0);

    long long int lower = 1, upper = 1e9 - 1;
    for(int i=0; i<n; i++){
        if(condition[i][0].size() >= 2
        || condition[i][1].size() >= 2){
            cout << 0 << endl;
            return 0;
        }
        else if(condition[i][0].empty() == false && condition[i][1].empty() == false){
            lower = (*condition[i][1].begin() - *condition[i][0].begin());
            if(lower % 2 == 1){
                cout << 0 << endl;
                return 0;
            }
            lower /= 2;
            upper = lower;
        }
        else if(condition[i][0].empty() == false) lower = max(lower, 1 - *condition[i][0].begin());
        else if(condition[i][1].empty() == false) upper = min(upper,     *condition[i][1].begin() - 1);

        if(lower > upper){
            cout << 0 << endl;
            return 0;
        }
    }
    long long int ans = upper - lower + 1;
    cout << ans << endl;
    return 0;
}