#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#include<unordered_set>

using namespace std;

#define dprint(Exp,...) if(Exp){fprintf(stderr, __VA_ARGS__);}
#define printe(...) fprintf(stderr, __VA_ARGS__);
#define PrtExp(_Exp)  cerr<< #_Exp <<" = "<< (_Exp)
#define PrtExpN(_Exp)  cerr<< #_Exp <<" = "<< (_Exp) <<"\n"

#define SINT(n) scanf("%d",&n)
#define SINT2(n,m) scanf("%d %d",&n,&m)
#define SINT3(n,m,o) scanf("%d %d %d",&n,&m,&o)
#define SINT4(n,m,o,p) scanf("%d %d %d %d",&n,&m,&o,&p)
#define SINT5(n,m,o,p,q) scanf("%d %d %d %d %d",&n,&m,&o,&p,&q)
#define SLL(n) scanf("%lld",&n)
#define SLL2(n,m) scanf("%lld %lld",&n,&m)
#define SLL3(n,m,o) scanf("%lld %lld %lld",&n,&m,&o)
#define SST(s) scanf("%s",s)
#define SCH(c) scanf("%c",&c)

#define GC() getchar()

#define PINT(n) printf("%d",(int)(n))
#define PINT2(n,m) printf("%d %d",(int)(n),(int)(m))
#define PINT3(n,m,l) printf("%d %d %d",(int)(n),(int)(m),(int)(l))
#define PLL(n) printf("%lld",(long long)(n))
#define PST(s) printf("%s",(s))
#define PCH(s) printf("%c",(s))

#define PINTN(n) printf("%d\n",(int)(n))
#define PINT2N(n,m) printf("%d %d\n",(int)(n),(int)(m))
#define PINT3N(n,m,l) printf("%d %d %d\n",(int)(n),(int)(m),(int)(l))
#define PLLN(n) printf("%lld\n",(long long)(n))
#define PSTN(s) printf("%s\n",(s))
#define PCHN(s) printf("%c\n",(s))

#define PSP() printf(" ")
#define PN() printf("\n")

#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\n")

#define rep(i,a) for(int i=0;i<a;i++)
#define reP(i,a) for(int i=0;i<=a;i++)
#define Rep(i,a) for(int i=a-1;i>=0;i--)
#define ReP(i,a) for(int i=a;i>=0;i--)

#define rEp(i,a) for(i=0;i<a;i++)
#define rEP(i,a) for(i=0;i<=a;i++)
#define REp(i,a) for(i=a-1;i>=0;i--)
#define REP(i,a) for(i=a;i>=0;i--)

#define repft(i,a,b) for(int i=a;i<b;i++)
#define repfT(i,a,b) for(int i=a;i<=b;i++)
#define Repft(i,a,b) for(int i=a-1;i>=b;i--)
#define RepfT(i,a,b) for(int i=a;i>=b;i--)

#define foreach(a,it) for(auto it = a.begin(); it != a.end(); ++it)

#define FILL(a,v) fill(begin(a),end(a), v)
#define FILL0(a) memset(a,0,sizeof(a))
#define FILL1(a) memset(a,-1,sizeof(a))

typedef long long ll;

typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll>   Pll;


const int INF = 1000000009;
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL;//2,242,545,357,980,376,863

template <class A, class B> inline ostream& operator<<(ostream& st, const pair<A, B>& P) { return st << "(" << P.first << "," << P.second << ")"; };
template <class A, class B> inline pair<A, B> operator+(const pair<A, B>& P, const pair<A, B>& Q) { return pair<A, B>(P.first + Q.first, P.second + Q.second); };
template <class A, class B> inline pair<A, B> operator-(const pair<A, B>& P, const pair<A, B>& Q) { return pair<A, B>(P.first - Q.first, P.second - Q.second); };

#define fs  first	
#define sc  second


vector<Pll> G[100005];

ll   V[100005][2];
bool u[100005][2];

int col[100005] = {};

bool ng = false;
void dfs(const int a, const int b, const ll v) {
	if (ng) return;
	u[a][b] = true;
	V[a][b] = v;
	for (auto&p : G[a]) {
		if (!u[p.fs][b]) {
			dfs(p.fs, b, p.sc - v);
		} else {
			if (V[p.fs][b] != p.sc - v) {
				// cout << p.fs << " : " << V[p.fs][b] << " " << p.sc - v << endl;
				ng = true;
				return;
			}
		}
	}
}
bool is2(int a, int c) {
	col[a] = c;
	for (auto&p : G[a]) {
		if (col[p.fs] != 0) {
			if (col[p.fs] != 3-c) return false;
		} else {
			if (!is2(p.fs, 3 - c)) return false;
		}
	}
	return true;
}

set<ll> v0;
void dfs2(int a, int b, ll v, ll pa) {
	u[a][b] = true;
	for (auto&p : G[a]) {
		if (p.fs == 0 && b == 0) v0.insert(p.sc - v);
		if (u[p.fs][1-b]) continue;
		dfs2(p.fs, 1-b, p.sc - v, a);
	}
	return;
}

bool dfs3(int a, ll v) {
	if (v <= 0) return false;
	u[a][0] = true;
	V[a][0] = v;
	for (auto&p : G[a]) {
		ll vv = p.sc - v;
		if (u[p.fs][0]) {
			if (V[p.fs][0] != vv) {
				return false;
			}
		} else {
			if (!dfs3(p.fs, vv)) return false;
		}
	}
	return true;
}




int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n, m;
	cin >> n >> m;
	rep(i, m) {
		ll x, y, a;
		cin >> x >> y >> a;
		--x; --y;
		G[x].emplace_back(y, a);
		G[y].emplace_back(x, a);
	}
	FILL0(u);
	if (is2(0, 1)) {
		// cout << "$$$" << endl;
		dfs(0, 0, 1);
		dfs(0, 1, INFLL);
		if (ng) {
			cout << 0 << endl;
			return 0;
		}
		ll mi = 1, ma = INFLL;
		rep(i, n) {
			//printf("%20lld %20lld\n", V[i][0], V[i][1]);
			if (V[i][0] <= V[i][1]) {
				mi = max(mi, 2 - V[i][0]);
				//ma = min(ma, 2 - V[i][1]);
			} else {
				//　mi = max(mi, 2 - V[i][1]);
				ma = min(ma, V[i][0]);
			}
		}
		// cout << mi << " " << ma << endl;
		cout << max(0LL, ma - mi + 1) << endl;
	} else {
		dfs2(0, 0, 0, -1);
		if (v0.size() != 1) {
			cout << 0 << endl;
			return 0;
		}
		FILL0(u);
		FILL0(V);
		cout << dfs3(0, *v0.begin() / 2) << endl;
	}


	return 0;
}


Measured memory_bytes: 24832