/**
*  _           _                 __                            _   _ _   _                                 _                    _                  _
* | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
* | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
* | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
* | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
* |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
*                                                  | |                                                                           | |   | |
*                                                  |_|                                                                           |_|   |_|
*
* https://github.com/hatoo/competitive-rust-snippets
*/
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
mod util {
    use std::fmt::Debug;
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn dfs(
    i: usize,
    x: i64,
    t: bool,
    es: &Vec<Vec<(usize, i64)>>,
    memo: &mut [(Option<i64>, Option<i64>)],
) -> bool {
    if t {
        if (memo[i].0).is_none() {
            memo[i].0 = Some(x);

            for &(to, s) in &es[i] {
                if !dfs(to, s - x, !t, es, memo) {
                    return false;
                }
            }
            true
        } else {
            return memo[i].0 == Some(x);
        }
    } else {
        if (memo[i].1).is_none() {
            memo[i].1 = Some(x);

            for &(to, s) in &es[i] {
                if !dfs(to, s - x, !t, es, memo) {
                    return false;
                }
            }
            true
        } else {
            return memo[i].1 == Some(x);
        }
    }
}

#[allow(dead_code)]
pub const INF: i64 = 1 << 60;

fn solve() {
    let (n, m) = get!(usize, usize);
    let uvs = get!(usize, usize, i64; m);

    let mut es = vec![Vec::new(); n];

    for (u, v, s) in uvs {
        es[u - 1].push((v - 1, s));
        es[v - 1].push((u - 1, s));
    }

    let mut memo = vec![(None, None); n];

    if !dfs(0, 0, true, &es, &mut memo) {
        println!("0");
        return;
    }

    // debug!(memo);

    let mut l = -INF;
    let mut r = INF;
    let mut c = None;
    for (x, y) in memo {
        match (x, y) {
            (Some(x), Some(y)) => {
                if (y - x) % 2 != 0 {
                    println!("0");
                    return;
                }
                let t = (y - x) / 2;
                if c.is_none() {
                    c = Some(t);
                } else {
                    if c != Some(t) {
                        println!("0");
                        return;
                    }
                }
                l = max(l, -x);
                r = min(r, y);
            }
            (Some(x), _) => {
                l = max(l, -x);
            }
            (_, Some(y)) => {
                r = min(r, y);
            }
            _ => (),
        }
    }

    // debug!(l, r, c);
    if let Some(c) = c {
        if l < c && c < r {
            println!("1");
        } else {
            println!("0");
        }
        return;
    }
    // debug!(l, r);
    let ans = r - l - 1;
    if ans <= 0 {
        println!("0");
    } else {
        println!("{}", ans);
    }
}


Measured memory_bytes: 30972