#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
using namespace std;
#define eps 0.000000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 65536*4
#define REP(i,n) for(long long i = 0;i < n;++i)
#define int long long
vector<pair<long long, long long>> vertexs[200000];
long long dp[200000] = {};
int color[200000] = {};
pair<int,int> dfs(int now,int back) {
	for (int i = 0; i < vertexs[now].size(); ++i) {
		if (vertexs[now][i].first != back) {
			if (color[vertexs[now][i].first] == -1) {
				dp[vertexs[now][i].first] = vertexs[now][i].second - dp[now];
				color[vertexs[now][i].first] = (color[now] + 1) % 2;
				pair<int,int> hoge = dfs(vertexs[now][i].first,now);
				if (hoge.first != 0) return hoge;
			}
			else {
				if(color[vertexs[now][i].first] == color[now])return make_pair(vertexs[now][i].first,now);
			}
		}
	}
	return make_pair(0,0);
}
long long final_calc[200000] = {};
long long pre_calc[200000] = {};
int calc_now(int now) {
	for (int i = 0; i < vertexs[now].size(); ++i) {
		if (final_calc[vertexs[now][i].first] == -LONG_INF) {
			final_calc[vertexs[now][i].first] = vertexs[now][i].second - final_calc[now];
			calc_now(vertexs[now][i].first);
		}
	}
	return 0;
}
int visited[200000] = {};
#undef int
int main() {
#define int long long
	int n, m;
	cin >> n >> m;
	vector<tuple<long long, long long, long long>> checking;
	REP(i, m) {
		long long a, b, c;
		cin >> a >> b >> c;
		checking.push_back(make_tuple(a, b, c));
		vertexs[a].push_back(make_pair(b, c));
		vertexs[b].push_back(make_pair(a, c));
	}
	REP(i, n) {
		color[i + 1] = -1;
	}
	color[1] = 0;
	pair<int,int> hoge = dfs(1,-1);
	if (hoge.first != 0) {
		//at least 1 loops
		for (int i = 0; i < 200000; ++i) {
			final_calc[i] = -LONG_INF;
		}
		for (int i = 0; i < checking.size(); ++i) {
			if (hoge.first == get<0>(checking[i]) || hoge.first == get<1>(checking[i])) {
				if (hoge.second == get<0>(checking[i]) || hoge.second == get<1>(checking[i])) {
					if (llabs((dp[hoge.first] + dp[hoge.second]) - get<2>(checking[i])) % 2LL == 1) {
						cout << 0 << endl;
						return 0;
					}
					final_calc[hoge.first] = dp[hoge.first] + (get<2>(checking[i]) - (dp[hoge.first] + dp[hoge.second])) / 2LL;
					final_calc[hoge.second] = dp[hoge.second] + (get<2>(checking[i]) - (dp[hoge.first] + dp[hoge.second])) / 2LL;
					assert(final_calc[hoge.first] + final_calc[hoge.second] == get<2>(checking[i]));
					break;
				}
			}
		}
		calc_now(hoge.first);
		calc_now(hoge.second);
		for (int i = 0; i < checking.size(); ++i) {
			long long a = final_calc[get<0>(checking[i])];
			long long b = final_calc[get<1>(checking[i])];
			if (a <= 0 || b <= 0 || a + b != get<2>(checking[i])) {
				cout << 0 << endl;
				return 0;
			}
		}
		cout << 1 << endl;
		return 0;
	}
	else {
		//calculate it
		for (int i = 0; i < checking.size(); ++i) {
			long long a = dp[get<0>(checking[i])];
			long long b = dp[get<1>(checking[i])];
			if (a + b != get<2>(checking[i])) {
				cout << 0 << endl;
				return 0;
			}
		}
		long long minimum = 1;
		long long maximum = LONG_INF;
		for (int i = 1; i <= n; ++i) {
			if (color[i] == 0) {
				minimum = min(minimum, dp[i]);
			}
			else {
				maximum = min(maximum, dp[i]);
			}
		}
		maximum -= abs(minimum) + 1LL;
		cout << max(0LL, maximum) << endl;
		return 0;
	}
}

Measured memory_bytes: 22320