#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn dfs2(
    cur: usize,
    graph: &Vec<Vec<(usize, i64)>>,
    used: &mut Vec<bool>,
    nums: &mut Vec<i64>,
    gap: &mut HashSet<i64>,
) {
    used[cur] = true;
    assert!(nums[cur] != -std::i64::MAX);
    for &(to, sum) in graph[cur].iter() {
        if nums[to] == -std::i64::MAX {
            nums[to] = sum - nums[cur];
        } else {
            if nums[to] + nums[cur] != sum {
                gap.insert(nums[to] - (sum - nums[cur]));
            }
        }
        if !used[to] {
            dfs2(to, graph, used, nums, gap);
        }
    }
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);
    let mut graph = vec![vec![]; n];
    for i in 0..m {
        let v = read_vec::<i64>();
        let (u, v, m) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        graph[u].push((v, m));
        graph[v].push((u, m));
    }
    if let Some(color) = is_biparate(&graph) {
        let mut nums = vec![-std::i64::MAX; n];
        let mut gap = HashSet::new();
        let mut used = vec![false; n];
        nums[0] = 1;
        dfs2(0, &graph, &mut used, &mut nums, &mut gap);
        if !gap.is_empty() {
            println!("0");
            return;
        }
        // debug!(color);
        let mut diff_min = std::i64::MAX;
        let mut same_min = std::i64::MAX;
        for i in 0..n {
            if color[i] == color[0] {
                same_min = min(same_min, nums[i]);
            } else {
                diff_min = min(diff_min, nums[i]);
            }
        }
        // debug!(diff_min, same_min);
        let ans = max(diff_min + (same_min - 1), 0);
        println!("{}", ans);
    } else {
        let mut nums = vec![-std::i64::MAX; n];
        let mut gap = HashSet::new();
        let mut used = vec![false; n];
        nums[0] = 1;
        dfs2(0, &graph, &mut used, &mut nums, &mut gap);
        let gap = gap.into_iter().collect::<Vec<_>>();

        if gap.is_empty() {
            if nums.into_iter().min().unwrap() > 0 {
                println!("1");
            } else {
                println!("0");
            }
            return;
        }

        let mut nums = vec![-std::i64::MAX; n];
        let mut used = vec![false; n];
        nums[0] = gap[0].abs() / 2 + 1;
        let mut gap = HashSet::new();
        dfs2(0, &graph, &mut used, &mut nums, &mut gap);
        if nums.into_iter().min().unwrap() > 0 && gap.is_empty() {
            println!("1");
            return;
        }
        println!("0");
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
fn dfs(start: usize, graph: &Vec<Vec<(usize, i64)>>, cur_color: i32, color: &mut Vec<i32>) -> bool {
    if color[start] != 0 {
        return cur_color == color[start];
    }
    color[start] = cur_color;
    for &(to, _) in graph[start].iter() {
        if !dfs(to, &graph, -1 * cur_color, color) {
            return false;
        }
    }
    true
}

fn is_biparate(graph: &Vec<Vec<(usize, i64)>>) -> Option<Vec<i32>> {
    let mut color = vec![0; graph.len()];
    for i in 0..graph.len() {
        if color[i] == 0 {
            if !dfs(i, graph, 1, &mut color) {
                return None;
            }
        }
    }
    Some(color)
}


Measured memory_bytes: 52092