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)

#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,lint> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
//問題文および制約はちゃんと確認しよう！
//サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
//とりあえずサンプルを読んでから解法を考えよう？
vector<pint> gr[100100];
int pat[100100];lint dp[100100];
int n1=-1,n2=-1;
void dfs(int v,int p,lint nu){
	//cout<<v<<' '<<p<<' '<<nu<<endl;
	dp[v]=nu;pat[v]=p;
	rep(i,gr[v].size()){
		int np=1-p,nv=gr[v][i].fi;
		if(pat[nv]!=-1){
			if(pat[nv]!=np && n1<0) n1=v,n2=nv;
			continue;
		}
		dfs(nv,np,gr[v][i].se-nu);
	}
}
int ok=1;
void dfs2(int v,lint nu){
	if(nu<=0){
		ok=0;return;
	}
	if(dp[v]>=0){
		if(nu<=0 || dp[v]!=nu){
			ok=0;
		}
		return;
	}
	dp[v]=nu;
	rep(i,gr[v].size()) dfs2(gr[v][i].fi,gr[v][i].se-nu);
}
int main()
{
	int n,m,u,v,s;
	cin>>n>>m;
	rep(i,m){
		cin>>u>>v>>s;u--;v--;
		gr[u].pb(mp(v,s));
		gr[v].pb(mp(u,s));
	}
	memset(pat,-1,sizeof(pat));
	dfs(0,0,0);
	//cout<<n1<<n2<<endl;
	//assert(n1>=0);
	if(n1<0){
		assert(n2<0);
		lint p0=123456789012345678LL,p1=123456789012345678LL;
		rep(i,n){
			if(pat[i]==0) p0=min(p0,dp[i]);else p1=min(p1,dp[i]);
		}
		cout<<max(0LL,p0+p1-1LL)<<endl;
		return 0;
	}
	assert(n1>=0 && n2>=0 && n1!=n2);
	lint t=-1;
	rep(i,gr[n1].size()){
		if(gr[n1][i].fi==n2) t=gr[n1][i].se;
	}
	assert(t>=0);
	if((t+dp[n1]-dp[n2])%2!=0LL){
		cout<<0<<endl;return 0;
	}
	lint x=(t+dp[n1]-dp[n2])/2;
	memset(dp,-1,sizeof(dp));
	dfs2(n1,x);
	cout<<ok<<endl;
}