//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        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, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($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) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

fn run() {
    input! {
        n: usize,
        m: usize,
        e: [(usize1, usize1, i64); m],
    }
    let mut g = vec![vec![]; n];
    for (a, b, s) in e {
        g[a].push((b, s - 2));
        g[b].push((a, s - 2));
    }
    let mut dp: Vec<Option<(bool, i64)>> = vec![None; n];
    dp[0] = Some((true, 0));
    let mut stack = vec![0];
    let mut fix = (false, 0);
    'outer: while let Some(v) = stack.pop() {
        let p = dp[v].unwrap();
        for &(u, s) in &g[v] {
            if let Some(q) = dp[u] {
                if q.0 == !p.0 {
                    if s - p.1 != q.1 {
                        println!("0");
                        return;
                    }
                } else {
                    fix.0 = true;
                    fix.1 = if p.0 {s - p.1 - q.1} else {p.1 + q.1 - s} / 2;
                    break;
                } 
            } else {
                dp[u] = Some((!p.0, s - p.1));
                stack.push(u);
            }
        }
    }
    if fix.0 {
        let mut dp = vec![None; n];
        dp[0] = Some(fix.1);
        let mut stack = vec![0];
        while let Some(v) = stack.pop() {
            let val = dp[v].unwrap();
            for &(u, s) in &g[v] {
                if let Some(d) = dp[u] {
                    if d != s - val {
                        println!("0");
                        return;
                    }
                } else {
                    dp[u] = Some(s - val);
                    stack.push(u);
                }
            }
        }
        for i in 0..n {
            if dp[i].unwrap() < 0 {
                println!("0");
                return;
            }
        }
        println!("1");
    } else {
        let mut l = 0;
        let mut r = 1_000_000_000 - 2;
        for v in 0..n {
            let p = dp[v].unwrap();
            if p.0 {
                if p.1 <= 0 {
                    l = std::cmp::max(l, -p.1);
                }
            } else {
                if p.1 >= 0 {
                    r = std::cmp::min(r, p.1);
                } else {
                    println!("0");
                }
            }
        }
        let ans = std::cmp::max(r - l + 1, 0);
        println!("{}", ans);
    }
}

fn main() {
    run();
}


Measured memory_bytes: 26868