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 <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

const int MAX_N = 100000;
const int MAX_M = 100000;
const ll INF = 1e18;

struct edge {
  ll to, sum;
};

int n, m;
ll t = -1;
ll ans = INF;
vector<edge> G[MAX_N];
vi candis[MAX_N][2];
vector<bool> visited(MAX_N, false);

bool dfs(int now, int pre, ll val, int sign) {
  if (visited[now]) return true;
  visited[now] = true;

  int sign_d = (sign == -1) ? 0 : 1;
  for (int i = 0; i < G[now].size(); i++) {
    int next = G[now][i].to;
    if (next == pre) continue;
    if (candis[next][1-sign_d].size() == 1) {
      ll a = val + sign*G[now][i].sum;
      ll b = candis[next][1-sign_d][0];
      if (((a + b)&1) == 0) {
        t = sign*(a + b) / 2;
      }
      return false;
    }
    if (candis[next][sign_d].size() == 1) {
      if (candis[next][sign_d][0] != val) {
        // cout << now << ", " << next << endl;
        // cout << candis[next][sign_d][0] << ", " << val << endl;
        return false;
      }
    } else {
      candis[next][sign_d].pb(val + sign*G[now][i].sum);
    }
    if (!dfs(next, now, val + sign*G[now][i].sum, -sign)) {
      return false;
    }
  }
  return true;
}

vi check_values(MAX_N, -1);
vector<bool> check_visited(MAX_N, false);

bool check(int now, int pre, ll value) {
  if (check_visited[now]) return true;
  check_visited[now] = true;

  if (check_values[now] != -1 && check_values[now] != value) {
    return false;
  }
  check_values[now] = value;

  for (int i = 0; i < G[now].size(); i++) {
    int next = G[now][i].to;
    if (next == pre) continue;
    if (!check(next, now, G[now][i].sum - value)) {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v; u--; v--;
    ll s; cin >> s;
    edge tmp1 = {v, s};
    edge tmp2 = {u, s};
    G[u].pb(tmp1);
    G[v].pb(tmp2);
  }
  ll t_min = 1, t_max = INF;
  candis[0][0].pb(0);
  if (!dfs(0, -1, 0, -1)) {
    ans = 0;
    if (t != -1) {
      if (check(0, -1, t)) {
        ans = 1;
      }
    }
  } else {
    for (int i = 1; i < n; i++) {
      // cout << i+1 << endl;
      if (candis[i][0].size() == 1) {
        ll a = -candis[i][0][0];
        // cout << "a: " << a << endl;
        t_max = min(t_max, a - 1);
      } else {
        ll b = candis[i][1][0];
        // cout << "b: " << b << endl;
        t_min = max(t_min, -b + 1);
      }
      // cout << "t_min: " << t_min << endl;
      // cout << "t_max: " << t_max << endl;
    }
  }
  ans = min(ans, max(0LL, t_max - t_min + 1));
  cout << ans << endl;
  return 0;
}