#include<set>  
#include<map>  
#include<stack>  
#include<cmath>  
#include<cstdio>  
#include<queue>  
#include<vector>  
#include<cstring> 
#include<climits>  
#include<iostream> 
#include<algorithm> 
using namespace std; 
#define INF 1000000000000000
#define MAXN 100000
#define LL long long
LL read(){ 
    LL f=1,x=0; 
    char c=getchar(); 
    while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();} 
    while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();} 
    return f*x; 
}
LL n,m,Min=1,Max=INF,val[MAXN+5],vis[MAXN+5];
struct edge{
	LL v,w;
	edge(){}
	edge(LL V,LL W){v=V,w=W;}
};
vector<edge> G[MAXN+5];
void DFS(LL x,LL u,LL fa,LL f){
	if(x<=0){
		if(f){
			printf("0\n");
			exit(0);
		}
		Min=max(Min,-x+2);
	}
	if(f) Max=min(Max,x);
	vis[u]=f+1,val[u]=x;
	LL v,w,siz=G[u].size();
	for(int i=0;i<siz;i++){
		v=G[u][i].v,w=G[u][i].w;
		if(v==fa) continue;
		if(vis[v]){
			if(vis[u]==vis[v]){
				if(((val[u]+val[v])%2+4)%2!=w%2){
					printf("0\n");
					exit(0);
				}
				if(vis[u]==1){
					Max=min(Max,1+(w-(val[u]+val[v]))/2);
					Min=max(Min,1+(w-(val[u]+val[v]))/2);
					if(Min!=Max)
						printf("0\n"),exit(0);
				}
				else{
					if(Min>1+((val[u]+val[v])-w)/2||Max<1+((val[u]+val[v])-w)/2){
						printf("0\n");
						exit(0);
					}
					Max=Min=1+((val[u]+val[v])-w)/2;
				}
			}else{
				if(val[u]+val[v]!=w){
					printf("0\n");
					exit(0);
				}
				if(val[v]<=0){
					if(vis[v]-1){
						printf("0\n");
						exit(0);
					}
					Min=max(Min,-val[v]+1);
				}
			}
			continue;
		}
		DFS(w-x,v,u,(f+1)%2);
	}
	return ;
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		LL u=read(),v=read(),w=read();
		G[u].push_back(edge(v,w));
		G[v].push_back(edge(u,w));
	}
	DFS(1,1,-1,0);
	printf("%lld\n",max(0ll,Max-Min+1));
    return 0;
}