#include <bits/stdc++.h>
#define to G[u][i].v
#define val G[u][i].s
#define N 100100
using namespace std;
typedef long long ll;

struct edge {int v;  ll s;  edge(int v,ll s):v(v),s(s) {}};
vector <edge> G[N];
int n, m, f[N], vis[N];
ll add[N], X;

void dfs(int u) {
	if (X == -1) return ;
	for (int i=0;i<G[u].size();i++) if (!vis[to]) {
		f[to] = -f[u];
		add[to] = val - add[u];
		vis[to] = 1;
		dfs(to);
	} else {
		if (f[u] != f[to]) {
			if (add[u] + add[to] != val) X = -1;
		} else {
			if ((val - add[to] - add[u]) % 2 != 0) X = -1;
			else {
				ll res;
				if (f[u] == 1) res = (val - add[to] - add[u]) / 2;
				else res = (add[u] + add[to] - val) / 2;
				if (res <= 0) X = -1;
				if (X > 0 && X != res) X = -1;
				if (X == 0) X = res;
			}
		}
	}
}

int main() {// freopen("_in.txt","r",stdin);
	scanf("%d%d",&n,&m);
	for (int i=1,u,v,s;i<=m;i++) {
		scanf("%d%d%d",&u,&v,&s);
		G[u].push_back(edge(v,s));
		G[v].push_back(edge(u,s));
	}
	f[1] = 1;  vis[1] = 1;
	dfs(1);
	if (X > 0) {
		for (int i=1;i<=n;i++) if (add[i] + X * f[i] <= 0) {puts("0");  return 0;}
		puts("1");  return 0;
	} else if (X == 0) {
		ll l = 1, r = 2e9;
		for (int i=1;i<=n;i++) {
			if (f[i] == 1) {
				l = max(l,-add[i]+1);
			} else {
				r = min(r,add[i]-1);
			}
		}
		if (0 < l && l <= r) {
			printf("%lld\n",r-l+1);
			return 0;
		}
	} puts("0");
}