#include<bits/stdc++.h>

using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if(1)
#else
#define CIN_ONLY if(1)
struct cww {cww() {CIN_ONLY{ios::sync_with_stdio(false); cin.tie(0);}}
}star;
#define DEBUG if(0)
#endif

#define ALL(v) (v).begin(),(v).end()
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l, T r){bool a = l>r; if (a)l = r; return a;}
template <typename T>inline bool chmax(T &l, T r){bool a = l<r; if (a)l = r; return a;}
template <typename T>istream& operator>>(istream &is, vector<T> &v){for (auto &it : v)is >> it;return is;}

class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }};
int n, m;
int a[112345];
int b[112345];
LL s[112345];
typedef vector<int> V;
typedef vector<V> VV;
VV g;
int c[112345];
LL k[112345][2];
int latte(int v,vector<int> &path) {
	for (int e:g[v]) {
		int u = v ^ a[e] ^ b[e];
		if (c[u] == -1) {
			c[u] = 1 - c[v];
			int target=latte(u, path);
			if (target != -1) {
				if (target == -2)return -2;
				path.push_back(e);
				if (target == v)return -2;
				else return target;
			}
		}
		if (c[u] == c[v]) {
			path.push_back(e);
			return u;
		}
	}
	return -1;
}
bool ei_ok = true;
void ei(int v) {
	for (int e : g[v]) {
		int u = v ^ a[e] ^ b[e];
		if (c[u] == 0) {
			c[u] = 1;
			k[u][0] = s[e] - k[v][0];
			k[u][1] = s[e] - k[v][1];
			ei(u);
		}
		else {
			if (k[u][0] + k[v][0] != s[e])ei_ok = false;
		}
	}
}
bool beet_ok = true;
void beet(int v) {
	if (k[v][0] <= 0)beet_ok = false;
	for (int e : g[v]) {
		int u = v ^ a[e] ^ b[e];
		if (c[u] == 0) {
			c[u] = 1;
			k[u][0] = s[e] - k[v][0];
			beet(u);
		}
		else {
			if (k[u][0] + k[v][0] != s[e])beet_ok = false;
		}
	}
}
int main() {
	cin >> n >> m;
	g = VV(n);
	for (int i : range(m)) {
		cin >> a[i] >> b[i] >> s[i];
		a[i]--; b[i]--;
		g[a[i]].push_back(i);
		g[b[i]].push_back(i);
	}
	for (int i : range(n))c[i] = -1;
	c[0] = 0;
	vector<int> path;
	int malta = latte(0, path);
	if (malta == -2) {
		int size = path.size();
		int p = a[path.front()];
		int q = b[path.front()];
		vector<LL> val(n);
		if (a[path[1]] != p && b[path[1]] != p)swap(p, q);
		val[p] = 0;
		int now = p;
		val[now] = 0;
		for (int i : range(1,size)) {
			int e = path[i];
			int nxt = a[e] ^ b[e] ^ now;
			val[nxt] = s[e] - val[now];
			now = nxt;
		}
		int diff = s[path.front()] - val[p] - val[q];
		if (diff % 2||diff<=0) {
			cout << 0 << endl;
		}
		else {
			int ans = diff / 2;
			k[p][0] = ans;
			for (int i : range(n))c[i] = 0;
			c[p] = 1;
			beet(p);
			if (beet_ok)cout << 1 << endl;
			else cout << 0 << endl;
		}
	}
	else {
		for (int i : range(n)) {
			c[i] = 0;
		}
		k[0][0] = 0;
		k[0][1] = 1;
		c[0] = 1;
		ei(0);
		if (ei_ok == false) {
			cout << 0 << endl;
		}
		else {
			LL lb = 1;
			LL ub = 1e10;
			for (int i : range(n)) {
				if (k[i][0] < k[i][1]) {
					chmax(lb, -k[i][1]+2);				
				}
				else {
					chmin(ub, k[i][1]);
				}
			}
			cout << max(ub - lb + 1, 0ll) << endl;

		}
		
	}

	return 0;
}


Measured memory_bytes: 18688