#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include <fstream>
#include <time.h>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()

using namespace std;

int MOD = (ll)1000000000 + 7;
ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
const int MAX = 500050;
typedef pair<int, int>P;

struct edge {
	int to, cost;
};
vector<edge>G[100010];
vector<int>intersection;//閉路の交点
int N,M;
bool visited[100010];


int upper[100010], lower[100010];
int mx=INF, mn=INF;
double t=-1;
ll ans = 0;
void dfs1(int n,int pre, int cnt,int pos) {
	//pe(n)pe(pos)print(cnt % 2);
	//evenの点から見る(nextはodd)
	if (cnt % 2 == 0) {
		for (auto x : G[n]) {
			int nex = x.to;
			if (nex == pre)continue;
			//閉路の交点
			if (visited[nex]) {
				//print(nex);
				//前回oddだった場合(矛盾がないか確認)
				if (upper[nex] != INF) {
					if (upper[nex] != x.cost - pos) {
						//print(nex);
						ans = -1; return;
					}
				}
				//前回even(tが確定する)
				else if (lower[nex] != INF) {
					double tmp = (-lower[nex] + (x.cost - pos)) / 2.0;
					//print(tmp);
					if (t != -1)if (tmp != t) {
						//print(nex);
						ans = -1; return;
					}
					t = tmp;
				}
			}
			else {
				visited[nex] = true;
				upper[nex] = x.cost - pos;
				mx = min(mx, upper[nex]);
				dfs1(nex, n, cnt + 1, upper[nex]);
			}
		}
	}
	else {//oddの点から見る
		for (auto x : G[n]) {
			int nex = x.to;
			if (nex == pre)continue;
			//閉路の交点
			if (visited[nex]) {
				//print(nex);
				//前回evenだった場合
				if (lower[nex] != INF) {
					if (lower[nex] != x.cost - pos) {
						//print(nex);
						ans = -1; return;
					}

				}//前回odd
				else if (upper[nex] != INF) {
					double tmp = (upper[nex] - (x.cost - pos)) / 2.0;
					if (t != -1)if (tmp != t) {
						//print(nex);
						ans = -1; return;
					}
					t = tmp;
					//print(tmp);
				}
			}
			else {
				visited[nex] = true;
				lower[nex] = x.cost-pos;
				mn = min(lower[nex], mn);
				dfs1(nex, n, cnt + 1, lower[nex]);
			}
		}
	}
}
signed main() {
	cin >> N >> M;
	REP(i, 100010) {
		upper[i] = INF;
		lower[i] = INF;
	}
	REP(i, M) {
		int u, v,c;
		cin >> u >> v>>c;
		u--, v--;
		G[u].push_back({ v,c });
		G[v].push_back({ u,c });
	}
	m0(visited);
	visited[0] = true;
	lower[0] = 0;
	dfs1(0, 0, 0, 0);
	if (ans == -1) {
		print(0);
		return 0;
	}
	if (mx <= -mn) {
		//pe(mx)print(mn);
		print(0); return 0;
	}
	if (t != -1) {
		//pe("t:")print(t);
		if ((int)t != t) {
			print(0); return 0;
		}
		else if (-mn < t && t < mx) {
			print(1)return 0;
		}
		else print(0);
		return 0;
	}
	//pe(mx)print(mn);
	ans = mx - 1;
	if (mn < 0) {
		ans += mn;
	}
	if (ans < 0) {
		print(0); return 0;
	}
	else print(ans);
}

Measured memory_bytes: 15744