#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;

vector<P>E[200000];
ll a[200000],b[200000];//y=am+b
ll M=LLONG_MAX;

void dfs(int v){
	for(P u:E[v]){
		ll A=(a[v]==1?-1:1),B=u.first-b[v];
		if(a[u.second]==INFL){
			a[u.second]=A;
			b[u.second]=B;
			dfs(u.second);
		}
		else{
			if(a[u.second]!=A||b[u.second]!=B){
				if(a[u.second]==A){
					puts("0");exit(0);
				}
				ll e=b[u.second]-B;
				if(e%2){
					puts("0");exit(0);
				}
				ll f=e/(A-a[u.second]);
				if(M!=LLONG_MAX&&M!=f){
					puts("0");exit(0);
				}
				M=f;
			}
		}
	}
}
int main(){
	int n,m;scanf("%d%d",&n,&m);
	rep(i,m){
		int u,v,s;scanf("%d%d%d",&u,&v,&s);u--;v--;
		E[u].push_back(P(s,v));
		E[v].push_back(P(s,u));
	}
	memset(a,0x3f,sizeof(a));
	a[0]=1;b[0]=0;
	dfs(0);
	ll Min=1;
	ll Max=LLONG_MAX;
	rep(i,n){
		//a=-1,b<=0
		if(a[i]==-1&&b[i]<=0){
			puts("0");return 0;
		}
		//a=-1,b>0
		if(a[i]==-1&&b[i]>0){
			Max=min(Max,b[i]-1);
		}
		//a=1,b<=0
		if(a[i]==1&&b[i]<=0){
			Min=max(Min,1-b[i]);
		}
		//a=1,b>0
	}
	if(Min>Max){
		puts("0");
	}
	else{
		if(M!=LLONG_MAX){
			if(Min<=M&&M<=Max)puts("1");
		}
		else cout<<Max-Min+1<<endl;
	}
}