//BFS-标程
#include <bits/stdc++.h>
#define MAXN 1000005
#define INF 1e18
#define LL long long
 
int n, m, cnt, head[MAXN], vis[MAXN];
LL l=1, r=INF, root=-1, f[MAXN][5];
 
LL min(LL x, LL y) {if(x>y) return y; return x;}
LL max(LL x, LL y) {if(x<y) return y; return x;}
 
struct Edge{int next, to; LL dis;} edge[MAXN];
 
std::queue<int> q;
 
void addedge(int from, int to, LL dis)
{
	edge[++cnt].next=head[from];
	edge[cnt].to=to;
	edge[cnt].dis=dis;
	head[from]=cnt;
} 
 
void bfs(int x)
{
	vis[x]=1; q.push(x);
	f[x][0]=1; f[x][1]=0;
	while(!q.empty())
	{
		int x=q.front(); q.pop();
		for(int i=head[x]; i; i=edge[i].next)
		{
			int to=edge[i].to;
			if(vis[to]) continue;
			f[to][0]=-f[x][0];
			f[to][1]=edge[i].dis-f[x][1];
			vis[to]=1; q.push(to);
		}
	}
}
 
int main()
{
	scanf("%d%d", &n, &m);
	for(int i=1; i<=m; i++)
	{
		int u, v, s;
		scanf("%d%d%d", &u, &v, &s);
		addedge(u, v, s);
		addedge(v, u, s);
	}
	bfs(1);
	for(int i=1; i<=n; i++)
	{
		if(f[i][0]==1) l=max(l, 1-f[i][1]);
		else r=min(r, f[i][1]-1);
		for(int j=head[i]; j; j=edge[j].next)
		{
			int to=edge[j].to;
			LL x=f[i][0]+f[to][0];
			LL y=f[i][1]+f[to][1];
			if(x==0 && y!=edge[j].dis) {printf("0\n"); return 0;}
			if(x!=0)
			{
				LL temp=edge[j].dis-y;
				if(temp%2 || temp/x<=0) {printf("0\n"); return 0;}
				if(root==-1) root=temp/x;
				if(root!=temp/x) {printf("0\n"); return 0;}
			}
		}
	}
	if(root==-1) printf("%lld\n", max(0, r-l+1));
	else printf("%d\n", (root>=l && root<=r));
}