#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 0x3f3f3f3f
#define MAXN 100000
#define LL long long
int read(){ 
    int 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; 
}
int n,m,ans,Maxw=0,Minw=INF;
struct edge{
	int v,w;
	edge(){}
	edge(int V,int W){v=V,w=W;}
};

int val[MAXN+5],bow[MAXN+5],ow,flag;
vector<edge> G[MAXN+5];
void DFS(int x,int u,int f,int fa){
	val[u]=x,bow[u]=f+1;
	int 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(bow[v]){
			if(bow[u]==bow[v]){
				int tmp=Minw+(w-(val[u]+val[v]))/2;
				if(((val[u]+val[v])%2!=w%2)|(flag==2&&tmp!=ow)){
					flag=1;
					return ;
				}
				flag=2;
				ow=tmp;
				return ;
			}
			else if(val[u]+val[v]!=w){
					flag=1;
					return ;
				}
		}
		if(w-x<=0){
			if(bow[u]==1){
				flag=1;
				return ;
			}
			else
				Minw=min(Minw,1+(x-w)+2);
			return ;
		}
		DFS(w-x,v,!f,u);
	}
	return ;
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		int u=read(),v=read(),w=read();
		Maxw=max(Maxw,w-1);
		G[u].push_back(edge(v,w));
		G[v].push_back(edge(u,w));
	}
	DFS(1,1,1,-1);
	if(flag==2) printf("0\n");
	else printf("%d\n",Maxw-Minw+1);
    return 0;
}