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: Time Limit Exceeded)

/** MIT License Copyright (c) 2018 Vasilyev Daniil **/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template<typename T> using v = vector<T>;
#define int long long
typedef long double ld;
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
#if 0  //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt"  : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin>>
#define put fout<<
#else
#define get cin>>
#define put cout<<
#endif
#define eol put endl
#define check(a) put #a << ": " << a << endl;
void read() {}     template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg)     ;read(args...) ;}
void print(){}     template<typename Arg,typename... Args> void print(Arg  arg,Args...  args){put (arg)<<" ";print(args...);}
void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg  arg,Args...  args){put (arg)<<" ";debug(args...);}
int getInt(){int a; get a; return a;}
//code goes here
const int N = 1e5 + 1;
v<vint> g[N];
int l[N], r[N];
int k[N], m[N]; //y = kx + m

int onlyOne = -2;
bool vis[N];
void dfs(int cur = 1, int prev = -1, int prevWeight = 0) {
    if (onlyOne != -2)
        return;

    if (prev == -1) {
        l[cur] = 1;
        r[cur] = 1e9;
        k[cur] = 1;
        m[cur] = 0;
    } else {
        int pk = -k[prev];
        int pm = prevWeight - m[prev];
        if (l[cur] != -1 && (pm != m[cur] || pk != k[cur])) {
            if (pk == k[cur] || (pm - m[cur]) % (k[cur] - pk))
                onlyOne = -1;
            else
                onlyOne = (pm - m[cur]) / (k[cur] - pk);
            return;
        } else if (l[cur] != -1)
            return;
        else {
            k[cur] = pk;
            m[cur] = pm;
            l[cur] = 1;
            r[cur] = 1e9;

            if (m[cur] <= 0 && k[cur] < 0) {
                onlyOne = -1;
                return;
            } else if (m[cur] <= 0 && k[cur] > 0)
                l[cur] = -m[cur] + 1;
            else if (m[cur] > 0 && k[cur] < 0)
                r[cur] = m[cur] - 1;
            else
                r[cur] = prevWeight - m[cur] - 1;
        }
    }

    for (vint i : g[cur])
        if (!vis[i[2]]) {
            vis[i[2]] = 1;
            dfs(i[0], cur, i[1]);
        }
}

int val[N];
bool valid = 1;
void dfs2(int cur = 1, int prev = -1, int prevWeight = 0) {
    if (!valid)
        return;
    if (prev != -1) {
        if (val[cur] != -inf && val[cur] != prevWeight - val[prev]) {
            valid = 0;
            return;
        }
        val[cur] = prevWeight - val[prev];
    }
    if (val[cur] <= 0) {
        valid = 0;
        return;
    }

    for (vint i : g[cur])
        if (!vis[i[2]]) {
            vis[i[2]] = 1;
            dfs2(i[0], cur, i[1]);
        }
}

void run() {
    int n, M;
    read(n, M);
    n++;
    rep(i, 0, M) {
        int v, u, s;
        read(v, u, s);
        g[v].pb({u, s, i});
        g[u].pb({v, s, i});
    }
    rep(i, 0, M)
        vis[i] = 0;
    rep(i, 1, n)
        l[i] = -1;
    dfs();

    if (onlyOne == -2) {
        int L = 1, R = 1e9;
        rep(i, 1, n)
            L = max(L, l[i]),
            R = min(R, r[i]);
        if (L > R)
            put 0;
        else
            put R - L + 1;
    } else {
        rep(i, 0, M)
            vis[i] = 0;
        rep(i, 1, n)
            val[i] = -inf;
        val[1] = onlyOne;
        if (onlyOne <= 0)
            put 0;
        else {
            dfs2();
            put valid;
        }
    }
}

int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 0;}