Problem:
Score : 600 points

Problem Statement

Kenkoooo found a simple connected graph.
The vertices are numbered 1 through n.
The i-th edge connects Vertex u_i and v_i, and has a fixed integer s_i.

Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied:

For every edge i, the sum of the positive integers written in Vertex u_i and v_i is equal to s_i.

Find the number of such ways to write positive integers in the vertices.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq u_i < v_i \leq n

2 \leq s_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

The graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m
u_1 v_1 s_1
:
u_m v_m s_m

Output

Print the number of ways to write positive integers in the vertices so that the condition is satisfied.

Sample Input 1

3 3
1 2 3
2 3 5
1 3 4

Sample Output 1

1

The condition will be satisfied if we write 1,2 and 3 in vertices 1,2 and 3, respectively.
There is no other way to satisfy the condition, so the answer is 1.

Sample Input 2

4 3
1 2 6
2 3 7
3 4 5

Sample Output 2

3

Let a,b,c and d be the numbers to write in vertices 1,2,3 and 4, respectively.
There are three quadruples (a,b,c,d) that satisfy the condition:

(a,b,c,d)=(1,5,2,3)

(a,b,c,d)=(2,4,3,2)

(a,b,c,d)=(3,3,4,1)

Sample Input 3

8 7
1 2 1000000000
2 3 2
3 4 1000000000
4 5 2
5 6 1000000000
6 7 2
7 8 1000000000

Sample Output 3

0

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*input
4 4
1 2 10
2 3 10
3 4 10
4 1 10
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#ifdef leowang
#define debug(...) do{\
	fprintf(stderr,"%s - %d : (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
	_DO(__VA_ARGS__);\
}while(0)
template<typename I> void _DO(I&&x){cerr<<x<<endl;}
template<typename I,typename...T> void _DO(I&&x,T&&...tail){cerr<<x<<", ";_DO(tail...);}
#else
#define debug(...)
#endif
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}
//}}}
const ll maxn=300005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const double PI=acos(-1);
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}

vector<pair<pll,ll>> edge;
vector<pll> child[maxn];
ll a[maxn];
ll tg[maxn];

bool vis[maxn];

void dfs(int u){
	vis[u]=1;
	for(auto i:child[u]) if(!vis[i.F]){
		a[i.F]=i.S-a[u];
		tg[i.F]=!tg[u];
		dfs(i.F);
	}
}

int main()
{
	//IOS;
	int n,m;
	cin>>n>>m;
	REP(i,m){
		ll u,v,d;
		cin>>u>>v>>d;
		u--;v--;
		edge.pb(MP(MP(u,v),d));
		child[u].pb(MP(v,d));
		child[v].pb(MP(u,d));
	}
	dfs(0);

	ll x;
	bool flag=0;

	for(auto i:edge){
		ll u=i.F.F,v=i.F.S,d=i.S;
		if(tg[u]==tg[v]){
			if(tg[u]){
				ll tmp=d-a[u]-a[v];
				if(tmp%2){
					cout<<"0\n";
					return 0;
				}
				if(flag){
					if(tmp/2!=x){
						cout<<"0\n";
						return 0;
					}
				}
				else{
					x=tmp/2;
					flag=1;
				}
			}
			else{
				ll tmp=a[u]+a[v]-d;
				if(tmp%2){
					cout<<"0\n";
					return 0;
				}
				if(flag){
					if(tmp/2!=x){
						cout<<"0\n";
						return 0;
					}
				}
				else{
					x=tmp/2;
					flag=1;
				}
			}
		}
		else{
			if(a[u]+a[v]!=d){
				cout<<"0\n";
				return 0;
			}
		}
	}
	if(flag){
		cout<<"1\n";
		return 0;
	}
	ll l=INF64,r=INF64;
	REP(i,n){
		if(tg[i]) l=min(l,a[i]);
		else r=min(r,a[i]);
	}
	l=1-l;
	r--;
	cout<<max(0LL,r-l+1)<<'\n';
	return 0;
}