#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <array>
#include <bitset> 
#include <unordered_map>
#include <random>

using namespace std;
typedef long long LL;
typedef pair<LL,LL> P;

const int L=1e6;
vector<P> ps[L];
vector<LL> us,vs,ss;
int st[L];
int lp=-1;
bool odd[L];
LL W[L];
void dfs(int i,LL v,bool f){
	st[i]=1;
	odd[i]=f;
	W[i]=v;
	for(auto p:ps[i]){
		int j=p.first;
		if(st[j]==0){
			dfs(j,p.second-v,!f);
		}
	}
}

int main() {
	int n,m;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int u,v,s;
		cin >> u >> v >> s;
		u--;
		v--;
		ps[u].push_back(P(v,s));
		ps[v].push_back(P(u,s));
		us.push_back(u);
		vs.push_back(v);
		ss.push_back(s);
	}

	memset(st,0,sizeof(st));
	dfs(0,0,false);
	bool hasOddCycle=false;
	for(int i=0;i<m;i++){
		if(odd[us[i]]==odd[vs[i]]){
			hasOddCycle=true;
			LL d=W[us[i]]+W[vs[i]]-ss[i];
			if(d%2){
				cout << 0 << endl;
				return 0;
			}
			memset(st,0,sizeof(st));
			//cerr << us[i] << ", " << W[us[i]]-d/2 << endl;
			dfs(us[i],W[us[i]]-d/2,false);
			break;
		}
		else{
			if(W[us[i]]+W[vs[i]]-ss[i]){
				cout << 0 << endl;
				return 0;
			}
		}
	}

	if(hasOddCycle){
		for(int i=0;i<m;i++){
			if(W[us[i]]+W[vs[i]]-ss[i]){
				cout << 0 << endl;
				return 0;
			}
		}
		for(int i=0;i<n;i++){
			if(W[i]<=0){
				cout << 0 << endl;
				return 0;
			}
		}
		cout << 1 << endl;
		return 0;
	}
	LL l=1;
	LL h=1LL<<60;
	for(int i=0;i<n;i++){
		//cerr << i+1 << ": " << odd[i] << ", " << W[i] << endl;
		if(odd[i]){
			h=min(h,W[i]-1);
			for(auto p:ps[i]){
				l=max(l,W[i]-p.second+1);
			}
		}
		else{
			l=max(l,1-W[i]);
			for(auto p:ps[i]){
				h=min(h,p.second-1-W[i]);
			}
		}
	//cerr << h << ", " << l << endl;
	}
	if(l>h){
		cout << 0 << endl;
		return 0;
	}
	cout << h-l+1 << endl;

	return 0;
}

