// Slower C++ solution:
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

//#include<iostream>

#include<cmath>

#include<vector>

#include<queue>

using namespace std;

const int MAXN=1e5;

int n_pnt,n_edg;

struct NODE{

	int p,val;//val+p*x>0

	NODE(){}

	NODE(int a,int b){p=a;val=b;}

};

struct QUEUE{

	int u,p;//val+p*x>0

	long long val;

	QUEUE(){}

	QUEUE(int a,int b,long long c){u=a;p=b;val=c;}

};

vector<pair<int,int> > lnk[MAXN+5];

long long vis[2][MAXN+5];//vis[0][i]-x>0 && vis[1][i]+x>0 -> x<vis[0] && x>-vis[1]

inline int G(int p){return p==1;}

long long MIN=-1e15,MAX=1e15;

int main()

{

	fill(vis[0],vis[0]+MAXN+5,1e15);

	fill(vis[1],vis[1]+MAXN+5,1e15);

	scanf("%d%d",&n_pnt,&n_edg);

	for(int i=0,u,v,l;i<n_edg;i++)

		scanf("%d%d%d",&u,&v,&l),

		lnk[u].push_back(make_pair(v,l)),lnk[v].push_back(make_pair(u,l));

	queue<QUEUE> que;

	que.push(QUEUE(1,1,0));

	while(!que.empty())

	{

		QUEUE fro=que.front();que.pop();

		for(int i=0;i<lnk[fro.u].size();i++)

		{

			int v=lnk[fro.u][i].first,l=lnk[fro.u][i].second;

			long long val=l-fro.val;int p=fro.p==1? -1:1;

			if(vis[G(p)][v]!=1e15)

			{

				if(val!=vis[G(p)][v])

				{

					printf("%d\n",0);

					return 0;

				}

				else

					continue;

			}

			vis[G(p)][v]=val;

			if(G(p))

				MIN=max(MIN,max((long long)0,-vis[1][v]));

			else

				MAX=min(MAX,vis[0][v]);

			que.push(QUEUE(v,p,val));

		}

	}

	long long ans=-1;

	for(int i=1;i<=n_pnt;i++)

		if(vis[0][i]!=1e15 && vis[1][i]!=1e15)

		{

			int res=(vis[0][i]-vis[1][i])/2;

			if(res<0) {printf("0\n");return 0;}

			if(ans==-1) ans=res;

			if(ans!=res) {printf("0\n");return 0;}

		}

	if(ans!=-1)

	{

		if(MIN<ans && ans<MAX)

			printf("%d\n",1);

		else

			printf("%d\n",0);

		return 0;

	}

	if(MAX-MIN-1<0) printf("%d\n",0);

	else printf("%lld\n",MAX-MIN-1);

	return 0;

}

/*

2 1

1 2 3

*/


// Faster C++ solution for the same problem (measured speedup: 1.5253709146531593x):
#include<cstdio>

#include<queue>

#include<vector>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;

const int Maxn=1e5;

const ll INF=1e18;

struct Edge {

	int to;

	ll cost;

	Edge(int a,ll b){to=a,cost=b;}

};

vector<Edge> G[Maxn+4];

ll F[Maxn+5][2];

ll Root=-1,L=1,R=INF+5;

void AddEdge(int u,int v,ll w) {

	G[u].push_back(Edge(v,w));

}

int N,M;

bool vis[Maxn+5];

void BFS(int s) {

	queue<int> q;

	vis[s]=true;q.push(s);

	F[s][0]=1,F[s][1]=0;

	while(!q.empty()) {

		int u=q.front();q.pop();

		for(int i=0;i<G[u].size();i++) {

			int v=G[u][i].to;

			ll w=G[u][i].cost;

			if(vis[v])continue;

			F[v][0]=-F[u][0];

			F[v][1]=w-F[u][1];

			vis[v]=true;

			q.push(v);

		}

	}

}

int main() {

	#ifdef LOACL

	freopen("in.txt","r",stdin);

	freopen("out.txt","w",stdout);

	#endif

	scanf("%d %d",&N,&M);

	for(int i=1;i<=M;i++) {

		int u,v,w;

		scanf("%d %d %d",&u,&v,&w);

		AddEdge(u,v,w);

		AddEdge(v,u,w);

	}

	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=0;j<G[i].size();j++) {

			int v=G[i][j].to;

			ll w=G[i][j].cost;

			ll x=F[i][0]+F[v][0];

			ll y=F[i][1]+F[v][1];

			if(x==0&&y!=w) {

				puts("0");

				return 0;

			}

			if(x!=0) {

				ll tmp=w-y;

				if(tmp%2||tmp/x<=0) {

					printf("0\n");

					return 0;

				}

				if(Root==-1)

					Root=tmp/x;

				if(Root!=tmp/x) {

					printf("0\n");

					return 0;

				}

			}

		}

	}

	if(Root==-1)

		printf("%d\n",max(0*1LL,R-L+1));

	else

		printf("%d\n",(L<=Root&&Root<=R));

	return 0;

}