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

vector<pair<int, int>> edges[100000];

bitset<100000> visited;
int sign[100000];
int64_t V[100000], offset;
bool offset_fixed;

void fail(){
    cout << 0 << endl;
    exit(0);
}

void dfs(int i){
    for(auto& p : edges[i]){
        int j = p.first, c = p.second;
        if(!visited[j]){
            sign[j] = -sign[i];
            V[j] = c-V[i];
            visited[j] = 1;
            dfs(j);
        }else{
            if(sign[i] == sign[j]){
                if(offset_fixed){
                    int64_t v = V[i] + V[j] + 2*sign[i]*offset;
                    if(v != c) fail();
                }else{
                    int64_t diff = c - ( V[i] + V[j]);
                    if(diff%2) fail();
                    offset = diff/2*sign[i];
                    offset_fixed = true;
                }
            }else{
                if(V[i] + V[j] != c) fail();
            }
        }
    }
}

int main(){
    int N, M;
    cin >> N >> M;
    for(int i=0; i<M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        edges[a].emplace_back(b, c);
        edges[b].emplace_back(a, c);
    }

    sign[0] = 1;
    dfs(0);

    int64_t ans;
    if(offset_fixed){
        for(int i=0; i<N; i++) if(V[i] + sign[i] * offset <= 0) fail();
        ans = 1;
    }else{
        int64_t mn1 = 1e18, mn2 = 1e18;
        for(int i=0; i<N; i++){
            if(sign[i] == 1){
                mn1 = min(mn1, V[i]);
            }else{
                mn2 = min(mn2, V[i]);
            }
        }
        ans = max(int64_t(0), mn1+mn2-1);
    }
    cout << ans << endl;
    return 0;
}