#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <list>
#include <bitset>
#include <fstream>

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i,a,n) for(LL i = (a); i < (LL)(n); ++i)
#define REM(i,a,n) for(LL i = (LL(n) - 1); i >= (a); --i)
#define EPS 1e-11
#define INF 0x3FFFFFFF
#define INFLL 0x3FFFFFFF3FFFFFFF
#define INFD 1.0e+308
#define FLOAT fixed << setprecision(16)
const double PI = 3.1415926535897932;

template <class T, class U>
istream& operator >> (istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(begin(dest), end(dest), val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
//sum, 累積和
// 負のときの割り算に注意
#if 1
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
template<class T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;	// コスト小を優先
#endif

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Union_Find.h"
#include "Graph.h"
#include "Suffix_Array.h"
#include "Matrix.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator >> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

int n, m;
vector<PP> G[100000];
LL A[100000];
LL B[100000];
bool check(int from, LL v, LL* vals) {
	if (vals[from] != -INFLL) return vals[from] == v;
	vals[from] = v;

	for(auto p : G[from]) {
		int to = p.first;
		LL sum = p.second;

		if (!check(to, sum - v, vals)) return false;
	}
	return true;
}

const PP INFP(-INFLL, -INFLL);
PP P[100000];
LL must(int from, PP p) {
	if (P[from] != INFP) {
		if (P[from].first != p.first) {
			if (p.first == 1) swap(P[from], p);
			
			LL sol = p.second - P[from].second;
			if (sol % 2 == 1) return -INFLL;
			return sol / 2;
		} else {
			return P[from] == p ? INFLL : -INFLL;
		}
	}
	P[from] = p;
	for (PP elem : G[from]) {
		int to = elem.first;
		LL sum = elem.second;
		LL ret = must(to, PP(-p.first, sum - p.second));

		if (ret == -INFLL) return -INFLL;
		if (ret != INFLL) return ret;
	}
	return INFLL;
}



int main() {
	cin >> n >> m;
	REP(i, 0, m) {
		LL a, b, c;
		cin >> a >> b >> c;
		--a; --b;
		G[a].push_back(PP(b, c));
		G[b].push_back(PP(a, c));
 	}
	
	Fill(A, -INFLL);
	Fill(B, -INFLL);
	bool a = check(0, 0, A);
	bool b = check(0, 1, B);

	if (a && b) {
		LL mn_a = INFLL;
		LL mn_b = INFLL;
		REP(i, 0, n) {
			if (A[i] < B[i]) {
				mn_a = min(mn_a, A[i]);
			} else {
				mn_b = min(mn_b, A[i]);
			}
		}
		cout << max(0LL, mn_a + mn_b - 1) << endl;
	} else {
		Fill(P, INFP);
		LL ans = must(0, PP(1, 0));
		
		bool c = false;
		if (ans != -INFLL) {
			Fill(A, -INFLL);
			c = check(0, ans, A);
			REP(i, 0, n) {
				if (A[i] <= 0) c = false;
			}
		}
		
		cout << (c ? 1 : 0) << endl;
	}


	return 0;
}


Measured memory_bytes: 17152