#include <bits/stdc++.h> 
using namespace std;

#define sz(x) (int)x.size() 
#define pb push_back 
#define mp make_pair 
#define fi(a, b) for(int i=a; i<=b; i++) 
#define fj(a, b) for(int j=a; j<=b; j++) 
#define fo(a, b) for(int o=a; o<=b; o++) 
#define fdi(a, b) for(int i=a; i>=b; i--) 
#define fdj(a, b) for(int j=a; j>=b; j--) 
#define fdo(a, b) for(int o=a; o>=b; o--) 

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while(false) {}
#endif

typedef long long ll; 
typedef pair<int, int> pii; 
typedef pair<ll, ll> pll; 
typedef vector<int> vi; 
typedef vector<ll> vll;
typedef vector<pii> vpii; 
typedef vector<pll> vpll; 
typedef long double ld;
typedef unsigned long long ull;

/////////////////////////////////

int const MAX = 1e5 + 41;

int n, m;
vpii e[MAX];

int u[MAX];
int col[MAX];

bool dfs(int x) {
	u[x] = 1;
	for (pii z : e[x]) {
		int y = z.first;
		if (!u[y]) {
			col[y] = 1 - col[x];
			if (!dfs(y)) return 0;
		}
		if (col[y] != 1 - col[x]) {
			return 0;
		}
	}
	return 1;
}

ll value[MAX];

void dfs1(int x) {
	u[x] = 1;
	for (pii z : e[x]) {
		int y = z.first;
		if (!u[y]) {
			value[y] = z.second - value[x];
			dfs1(y);
		}
	}
}

bool good() {
	fi(1, n) {
		for (pii z : e[i]) {
			int y = z.first;
			int s = z.second;
			if (s != value[i] + value[y]) {
				return false;
			}
		}
	}
	return true;
}

bool check() {
	if (!good()) return false;
	fi(1, n) {
		if (value[i] <= 0) return false;
	}
	return true;
}

ll const INF = 1e18 + 41;

void paint(int x) {
	u[x] = 1;
	for (pii z : e[x]) {
		int y = z.first;
		int s = z.second;
		if (u[y]) continue;
		value[y] = s - value[x];
		col[y] = 1 - col[x];
		paint(y);
	}
}

void kill(ll ans) {
	printf("%lld\n", ans);
	exit(0);
}

void solve() {
	if (dfs(1)) {
		memset(u, 0, sizeof(u));
		dfs1(1);
		if (!good()) {
			kill(0);
		}	else {
			ll mini0 = INF;
			ll mini1 = INF;			
			fi(1, n) {
				if (col[i] == 0) {
					mini0 = min(mini0, value[i]);
				} else {
					mini1 = min(mini1, value[i]);
				}
			}
			if (mini0 <= 0LL && mini1 <= 0LL) {
				kill(0);
			} else {
				if (mini0 > mini1) {
					swap(mini0, mini1);
				}
				if (mini0 > 0LL && mini1 > 0LL) {
					kill(mini0);
				} else {
					mini0 = -(mini0 - 1);					
					mini1 -= mini0;
					mini1 = max(mini1, 0LL);
					kill(mini1);
				}
			}
		}
	} else {
		memset(u, 0, sizeof(u));
		paint(1);
		set<ll> t0, t1;
		fi(1, n) {
			for (pii z : e[i]) {
				int y = z.first;
				int need = z.second;
				if (col[y] == col[i]) {
					if (col[i] == 0) {
						t0.insert(need - value[i] - value[y]);						
					} else {
						t1.insert(need - value[i] - value[y]);
					}
				}
			}
		}										
		if (sz(t0) > 1 || sz(t1) > 1) kill(0);
		for (auto x : t0) {
			if (x % 2 != 0) kill(0);
		}
		for (auto x : t1) {
			if (x % 2 != 0) kill(0);
		}

		/*
		err("bef:\n");
		fi(1, n) {
			err("i = %d v = %d col = %d\n", i, value[i], col[i]);
		}
		*/

		if (sz(t0)) {
			ll x = (*t0.begin());
			value[1] += x / 2;
		} else {
			ll x = (*t1.begin());
			value[1] -= x / 2;			
		}
		memset(u, 0, sizeof(u));
		paint(1);

		if (check()) {
			kill(1);
		} else {
			kill(0);
		}
	}
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

  scanf("%d %d", &n, &m);
  fi(1, m) {
  	int x, y, s;
  	scanf("%d %d %d", &x, &y, &s);
  	e[x].pb(mp(y, s));
  	e[y].pb(mp(x, s));
  }
	solve();		

	
#ifdef LOCAL
	err("ELAPSED TIME: %.3Lf\n", (ld) clock() / CLOCKS_PER_SEC);
#endif	
	
	return 0;
}

Measured memory_bytes: 11904