#include <bits/stdc++.h>

#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,x,n) for(int i=x; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define all(X) (X).begin(),(X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class A, size_t N, class T> void Fill(A (&a)[N], const T &v){ fill( (T*)a, (T*)(a+N), v ); }

template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"; return os;}
template<class T,size_t n> ostream& operator<<(ostream &os, const array<T,n> &t) {os<<"{"; rep(i,n) {os<<t[i]<<",";} os<<"}"; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const ll INF = 1e17;


vector<pii> edge[100005];
bool used[2][100005];
ll memo[2][100005];
bool ans_zero;
void dfs(int n, int sign=0, int val=0){
	if( memo[sign][n] == INF ){
		memo[sign][n] = val;
	}else if(memo[sign][n] != val){
		ans_zero = true;
	}
	if( used[sign][n] ) return;
	used[sign][n] = true;
	for(auto p: edge[n]){
		dfs(p.X, sign^1, p.Y - val);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	int N, M, ans=0;

	cin >> N >> M;
	rep(i,M){
		int u, v, s;
		cin >> u >> v >> s;
		u--; v--;
		edge[u].emplace_back(v, s);
		edge[v].emplace_back(u, s);
	}

	Fill(memo, INF);
	dfs(0);

	ll mn = 1, mx = INF;
	rep(i,N){
		//cout << i << ": " << memo[0][i] << " " << memo[1][i] << endl;
		chmax(mn, 1 - memo[0][i]);
		chmin(mx, memo[1][i]);
		if( memo[0][i] < INF && memo[1][i] < INF ){
			ll val = (memo[1][i] - memo[0][i]) / 2;
			chmax(mn, val);
			chmin(mx, val+1);
			//cout << "eq: " << val << endl;
		}
	}
	//cout << mn << " " << mx << endl;

	if( ans_zero ){
		cout << 0 << endl;
	}else{
		cout << max(0ll, mx-mn) << endl;
	}

	return 0;
}