#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8

int n, m;
VV(pll) G;
VV(pll) T;
vector<int> used;

void dfs(int u){
	used[u] = 1;
	for(cauto e : G[u]){
		if(!used[e.first]){
			T[u].emplace_back(e);
			dfs(e.first);
		}
	}
}

void dfs2(int u, LL a, vector<LL> &vals){
	vals[u] = a;
	for(cauto e : T[u]){
		dfs2(e.first, e.second - a, vals);
	}
}

LL divup(LL a, LL b){ return (a + b - 1) / b; }

LL solve(){
	const LL INF = 1LL << 55;

	cin >> n >> m;
	G.resize(n);
	for(int i = 0; i < m; ++i){
		int u, v, s;
		cin >> u >> v >> s;
		--u;
		--v;
		G[u].emplace_back(v, s);
		G[v].emplace_back(u, s);
	}
	T.resize(n);
	used.resize(n);
	dfs(0);
	
	vector<LL> vals0(n), vals1(n);
	dfs2(0, 0, vals0);
	dfs2(0, 1, vals1);

	LL minlim = 1, maxlim = INF;
	for(int i = 0; i < n; ++i){
		LL v0 = vals0[i], v1 = vals1[i];
		LL d = v1 - v0;
		if(d == 0){
			if(v0 < 1){ return 0; }
		}
		else if(d > 0){
			if(v0 < 1){
				chmax(minlim, divup(1 - v0, d));
			}
		}
		else{
			if(v0 < 1){ return 0; }
			chmin(maxlim, (v0 - 1) / -d);
		}
	}
	for(int x = 0; x < n; ++x){
		for(cauto e : G[x]){
			int y = e.first;
			LL tgt = e.second;

			LL s0 = vals0[x] + vals0[y];
			LL s1 = vals1[x] + vals1[y];
			LL d = s1 - s0;
			if(d == 0){
				if(s0 != tgt){ return 0; }
			}
			else if((tgt - s0) % d != 0){ return 0; }
			else{
				LL t = (tgt - s0) / d;
				chmax(minlim, t);
				chmin(maxlim, t);
			}
		}
	}
	LL ans = max(maxlim - minlim + 1, 0LL);
	return ans;
}

void mainmain(){
	LL ans = solve();
	cout << ans << endl;
}



}
int main() try{
	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}
catch(...){}


Measured memory_bytes: 19584