#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

const int MAXN = 1e5 + 5;
const ll INF = 1e18;

int N, M;
int u[MAXN], v[MAXN], s[MAXN];
vector <pii> E[MAXN];
ll sum[MAXN];
int coef[MAXN];

void load() {
	scanf("%d%d", &N, &M);
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", u + i, v + i, s + i);
		E[u[i]].push_back({v[i], s[i]});
		E[v[i]].push_back({u[i], s[i]});
	}
}

void bye() {
	puts("0");
	exit(0);
}

void dfs(int x) {
	for (auto it : E[x])
		if (!coef[it.first]) {
			coef[it.first] = -coef[x];
			sum[it.first] = (ll)it.second - sum[x];
			dfs(it.first);
		}
}

void solve() {
	coef[1] = 1;
	dfs(1);
	
	ll lo = -INF, hi = INF;
	for (int i = 1; i <= N; i++)
		if (coef[i] == 1)
			lo = max(lo, -sum[i] + 1);
		else 
			hi = min(hi, sum[i] - 1);
	
	set <ll> vals;
	for (int i = 0; i < M; i++)
		if (coef[u[i]] == coef[v[i]]) {
			ll tmp = (ll)s[i] - sum[u[i]] - sum[v[i]];
			if (tmp % 2)
				bye();
			vals.insert(tmp / 2 * coef[u[i]]);
		}
		else if (sum[u[i]] + sum[v[i]] != (ll)s[i])
			bye();
	
	if (vals.empty())
		printf("%lld\n", max(hi - lo + 1, 0ll));
	else if (vals.size() > 1)
		bye();
	else
		printf("%d\n", *vals.begin() >= lo && *vals.begin() <= hi);
}

int main() {
	load();
	solve();
	return 0;
}