#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define Max 100001
#define inf 1000000000000000
using namespace std;
int n,m,flag;
int first[Max * 2],nextv[Max * 2],u[Max * 2],v[Max * 2],s[Max * 2];
long long vis[2][Max],l = 0,r = inf,ans;
void dfs(int from,int tag,long long va) {
    if(flag)return;
    int k = first[from];
    while(k != -1) {
        if(flag)return;
        int vtag = (tag == 1);
        int to = v[k];
        long long lva = s[k] - va;
        if(vis[vtag][to] != inf) {
            if(vis[vtag][to] == lva) {
                k = nextv[k];
                continue;
            }
            else {
                flag = 1;
                return;
            }
        }
        vis[vtag][to] = lva;
        if(vtag)r = min(r,lva);
        else l = max(l,-lva);
        if(r - l <= 1){
            flag = 1;
            return;
        }
        dfs(to,-tag,lva);
        k = nextv[k];
    }
}
int main() {
    scanf("%d%d",&n,&m);
    memset(first,-1,sizeof(first));
    for(int i = 1;i <= n;i ++) {
        vis[0][i] = vis[1][i] = inf;
    }
    for(int i = 0;i < m;i ++) {
        scanf("%d%d%d",&u[i],&v[i],&s[i]);
        u[i + m] = v[i];
        v[i + m] = u[i];
        s[i + m] = s[i];
        nextv[i] = first[u[i]];
        first[u[i]] = i;
        nextv[i + m] = first[u[i + m]];
        first[u[i + m]] = i + m;
    }
    dfs(1,1,0);///dfs遍历所有点和第一个点的关系，如果有冲突flag为1 冲突就是说得到于同一点的同一关系的值不同比如a1 + a2 = 3 和 a1 + a2 = 4
    if(!flag) {///遍历后无冲突
        long long temp = -1;
        for(int i = 1;i <= n;i ++) {
            if(vis[0][i] != inf && vis[1][i] != inf) { ///可以确定a1
                long long d = vis[1][i] - vis[0][i];
                if(d % 2) {
                    flag = 1;
                    break;
                }
                temp = d / 2;
            }
        }
        if(!flag) {
            if(temp != -1) {///不能直接求出a1
                if(temp > l && temp < r)ans = 1;
            }
            else if(r - l - 1 > 0)ans = r - l - 1;
        }
    }
    printf("%lld",ans);
    return 0;
}
