/// Zhupengfei 2018
/// Hereby declare: this program is in public domain.

/// Solution for problem: Soundhound E
#include <bits/stdc++.h>

#ifdef LOCAL
#pragma GCC poison printf // To avoid fastio problems
#pragma GCC poison scanf
#endif

using namespace std;

struct rec{
	int v, s;
};

int limit[100001]={};
bool flag[100001]={};
bool v[100001]={};
int value[100001]={};
vector<rec> sons[100001]={}; 
int n, m;
int minx=0, maxx=1e9;

void check(int, int);

void dfs(int x, int c, int parent) {
	v[x]=1;
	for(const auto& it: sons[x]) {
		if(v[it.v] && it.v!=parent) { // loop found, can calc determined x
			bool tobeflag=!flag[x];
			int tobelimit=it.s-limit[x];
			if(tobeflag==flag[it.v] && tobelimit!=limit[it.v]) {
				cout<<0<<endl;
				exit(0);
			} else if(tobeflag==flag[it.v]) continue; // useless loop
			int decidex;
			if(!tobeflag) { //to be is false
				decidex=(-tobelimit+limit[it.v])/2;
			} else {
				decidex=(tobelimit-limit[it.v])/2;
			}
			value[1]=decidex;
			if(value[1]<=0) {
				cout<<0<<endl;
				exit(0);
			}
			memset(v, 0, sizeof(v));
			check(1, 0);
			cout<<1<<endl;
			exit(0);
		} else if(it.v!=parent) { // normal
			flag[it.v]=!flag[x];
			limit[it.v]=it.s-limit[x];
			if(flag[it.v]) {
				maxx=min(maxx, limit[it.v]);
			} else {
				minx=max(minx, -limit[it.v]);
			}
			if(minx>=maxx) {
				cout<<0<<endl;
				exit(0);
			}
			dfs(it.v, c+1, x);
		}
	}
}

void check(int u, int parent) { // I have decided the x! I'm super powerful!
	v[u]=1;
	for(const auto& it: sons[u]) {
		if(v[it.v] && it.v!=parent) {
			int tobevalue = it.s-value[u];
			if(tobevalue!=value[it.v]) {
				cout<<0<<endl;
				exit(0);
			}
		}else if(it.v!=parent){
			value[it.v]=it.s-value[u];
			if(value[it.v]<=0) {
				cout<<0<<endl;
				exit(0);
			} else {
				check(it.v, u);
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin>>n>>m;
	int u, v, s;
	for(int i=1;i<=m;i++) {
		cin>>u>>v>>s;
		sons[u].push_back({v, s});
		sons[v].push_back({u, s});
	}
	flag[1]=false; // false: positive, true: negative
	dfs(1, 1, 0);
	cout<<maxx-minx-1<<endl;
	return 0;
}
