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 Rust (1.15.1) (judge verdict: Accepted)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
#[derive(Clone, Debug)]
pub struct WeightedUnionFind<G: Group> {
    group: G,
    parents: Vec<isize>,
    diff: Vec<G::T>,
}
impl<G: Group> WeightedUnionFind<G> {
    pub fn new(n: usize, group: G) -> Self {
        let parents = vec![-1; n];
        let diff = vec![group.mempty().clone(); n];
        WeightedUnionFind {
            group: group,
            parents: parents,
            diff: diff,
        }
    }
    pub fn find(&mut self, x: usize) -> usize {
        if self.parents[x] < 0 {
            x
        } else {
            let px = self.parents[x] as usize;
            let y = self.find(px);
            let w = self.group.mappend(&self.diff[x], &self.diff[px]);
            self.diff[x] = w;
            self.parents[x] = y as isize;
            y
        }
    }
    pub fn get_weight(&mut self, x: usize) -> G::T {
        self.find(x);
        self.diff[x].clone()
    }
    pub fn unite(&mut self, x: usize, y: usize, w: G::T) -> bool {
        let wx = self.get_weight(x);
        let wy = self.get_weight(y);
        let mut w = self.group.mappend(&w, &wx);
        w = self.group.mappend(&w, &self.group.invert(&wy));
        use std::mem::swap;
        let mut x = self.find(x);
        let mut y = self.find(y);
        if x == y {
            return false;
        }
        if self.parents[x] > self.parents[y] {
            swap(&mut x, &mut y);
            w = self.group.invert(&w);
        }
        self.parents[x] += self.parents[y];
        self.parents[y] = x as isize;
        self.diff[y] = w;
        true
    }
    pub fn size(&mut self, x: usize) -> usize {
        let x = self.find(x);
        (-self.parents[x]) as usize
    }
    pub fn is_same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
    pub fn get_difference(&mut self, x: usize, y: usize) -> Option<G::T> {
        if self.is_same(x, y) {
            Some(
                self.group
                    .mappend(&self.diff[y], &self.group.invert(&self.diff[x])),
            )
        } else {
            None
        }
    }
    pub fn members(&mut self, x: usize) -> Vec<usize> {
        let root = self.find(x);
        (0..self.parents.len())
            .into_iter()
            .filter(|i| self.find(*i) == root)
            .collect::<Vec<usize>>()
    }
    pub fn roots(&mut self) -> Vec<usize> {
        (0..self.parents.len())
            .into_iter()
            .filter(|i| self.parents[*i] < 0)
            .collect::<Vec<usize>>()
    }
    pub fn all_group_members(&mut self) -> std::collections::HashMap<usize, Vec<usize>> {
        let mut groups_map = std::collections::HashMap::new();
        for x in 0..self.parents.len() {
            let r = self.find(x);
            groups_map.entry(r).or_insert(vec![]).push(x);
        }
        groups_map
    }
}
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
pub trait Group: Monoid {
    fn invert(&self, x: &Self::T) -> Self::T;
}
#[derive(Clone, Debug)]
pub struct AnyGroup<T: Clone, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> {
    id: T,
    op: F,
    inv: G,
}
impl<T: Clone, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> AnyGroup<T, F, G> {
    pub fn new(id: T, op: F, inv: G) -> AnyGroup<T, F, G> {
        AnyGroup {
            id: id,
            op: op,
            inv: inv,
        }
    }
}
impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> Monoid for AnyGroup<T, F, G> {
    type T = T;
    fn mempty(&self) -> T {
        self.id.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        (self.op)(x, y)
    }
}
impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> Group for AnyGroup<T, F, G> {
    fn invert(&self, x: &T) -> T {
        (self.inv)(x)
    }
}
use std::cmp::{max, min};
fn main() {
    input! { n, m, uvs: [(usize1, usize1, i64); m] };
    let mut uf = WeightedUnionFind::new(n * 2, AnyGroup::new(0i64, |&x, &y| x + y, |&x| -x));
    for &(u, v, s) in &uvs {
        if let Some(w) = uf.get_difference(u, v + n) {
            if s != w {
                println!("{}", 0);
                return;
            }
        } else {
            uf.unite(u, v + n, s);
        }
        if let Some(w) = uf.get_difference(v, u + n) {
            if s != w {
                println!("{}", 0);
                return;
            }
        } else {
            uf.unite(v, u + n, s);
        }
    }
    let mut l = std::i64::MIN;
    let mut u = std::i64::MAX;
    for x in uf.members(0).into_iter() {
        let w = uf.get_weight(x);
        if x < n {
            l = max(l, w);
        } else {
            u = min(u, w);
        }
    }
    if uf.roots().len() == 1 {
        println!("{}", min(max(0, u - l - 1), 1));
    } else {
        println!("{}", max(0, u - l - 1));
    }
}