#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const long long MAXN=200005;
const long long INF=9223372036854775807ll;
long long n,m,ecnt,key=0;
long long w[MAXN],d[MAXN],z[MAXN];
struct edge
{
	long long v,c,nxt;
	edge(){}
	edge(long long vv,long long cc,long long nn)
	{
		v=vv,c=cc,nxt=nn;
	}
}ed[MAXN*4];
void addedge(long long u,long long v,long long a)
{
	ecnt++;
	ed[ecnt]=edge(v,a,w[u]);
	w[u]=ecnt;
	
	ecnt++;
	ed[ecnt]=edge(u,a,w[v]);
	w[v]=ecnt;
}
void dfs(long long u,long long val,long long k)
{
	if(z[u]==0)
	{
		d[u]=val;
		z[u]=k;
		for(long long i=w[u];i;i=ed[i].nxt)
		{
			long long v=ed[i].v;
			long long c=ed[i].c;
			dfs(v,c-val,-k);
		}
	}
	else
	{
		if(k==z[u])
		{
			if(d[u]!=val)
			{
				printf("0\n");
				exit(0);
			}
			return ;
		}
		else
		{
			if(k==1)
			{
				key=d[u]-val;
				if((key&1)||(key<0))
				{
					printf("0\n");
					exit(0);
				}
				key/=2;
				return ;
			}
			else
			{
				key=val-d[u];
				if((key&1)||(key<0))
				{
					printf("0\n");
					exit(0);
				}
				key/=2;
				return ;
			}
		}
	}
}
bool check(int a)
{
	for(int i=1;i<=n;i++)
	{
		long long u=i;
		for(long long j=w[u];j;j=ed[j].nxt)
		{
			long long v=ed[j].v;
			long long c=ed[j].c;
			if(d[u]+z[u]*key+d[v]+z[v]*key!=c)
				return 0;
		}
	}
	return 1;
}
int main()
{
	key=-INF;
	scanf("%lld %lld",&n,&m);
	fill(d,d+MAXN,0);
	fill(z,z+MAXN,0);
	for(long long i=1;i<=m;i++)
	{
		long long u,v,s;
		scanf("%lld %lld %lld",&u,&v,&s);
		addedge(u,v,s);
		addedge(v,u,s);
	}
	dfs(1,0,1);
	if(key!=-INF)
	{
		for(long long i=1;i<=n;i++)
		{
			if(d[i]+z[i]*key<=0)
			{
				printf("0\n");
				return 0;
			}
		}
		if(check(key))
			printf("1\n");
		else
			printf("0\n");
		return 0;
	}
	long long da=-INF,x=INF;
	for(long long i=1;i<=n;i++)
	{
		if(z[i]==1)
		{
			da=max(da,-d[i]);
		}
		else
		{
			x=min(x,d[i]);
		}
	}
	da=max(da,0ll);
	x--;
	da++;
	if(x-da+1<=0)
		printf("0\n");
	else
		printf("%lld\n",x-da+1);
}