/*
Cerberus97
Hanit Banga
*/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

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

const int N = 1e5 + 10, inf = 1e9 + 42;

struct edge {
	int v; ll w;
};

vector<edge> g[N];
ll c[N], lo = 1, hi = inf;
int depth[N];
bool seen[N];

void dfs(int u);

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		g[u].pb({v, w});
		g[v].pb({u, w});
	}
	dfs(1);
	cout << max(0ll, hi - lo + 1) << endl;
}

void dfs(int u) {
	seen[u] = true;
	if (depth[u] & 1) {
		hi = min(hi, c[u] - 1);
	} else {
		lo = max(lo, 1 - c[u]);
	}
	for (auto &e : g[u]) {
		if (!seen[e.v]) {
			c[e.v] = e.w - c[u];
			depth[e.v] = 1 + depth[u];
			dfs(e.v);
		} else if (depth[e.v] < depth[u] - 1) {
			if ((depth[u] - depth[e.v]) & 1) {
				if (c[u] + c[e.v] != e.w) {
					lo = hi + 1;
					break;
				}
			} else {
				ll temp = c[u] - c[e.v] + e.w;
				if (temp <= 0 or (temp & 1)) {
					lo = hi + 1;
					break;
				} else if (depth[u] & 1) {
					temp = c[u] - temp / 2;
				} else {
					temp = temp / 2 - c[u];
				}
				lo = max(lo, temp);
				hi = min(hi, temp);
			}
		}
	}
}