import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

alias Edge = Tuple!(int, "from", int, "to", int, "s");
alias Pair = Tuple!(int, "sgn", int, "val");
enum inf = 2*10^^9 + 3;

void main() {
    int n, m;
    scan(n, m);

    auto adj = new Edge[][](n, 0);
    auto es = new Edge[](m);

    foreach (i ; 0 .. m) {
        int ui, vi, si;
        scan(ui, vi, si);
        ui--, vi--;
        adj[ui] ~= Edge(ui, vi, si);
        adj[vi] ~= Edge(vi, ui, si);
        es[i] = Edge(ui, vi, si);
    }

    auto t = new Pair[](n);
    t[] = Pair(0, 0);

    void dfs(int v, int s, int a) {
        t[v] = Pair(s, a);

        foreach (e ; adj[v]) {
            if (t[e.to].sgn == 0) {
                dfs(e.to, s*(-1), e.s - a);
            }
        }
    }

    dfs(0, 1, 0);

    int x = inf;
    bool one;

    foreach (i ; 0 .. m) {
        int u = es[i].from, v = es[i].to;
        int s = es[i].s;
        if (t[u].sgn != t[v].sgn) {
            if (t[u].val + t[v].val != s) {
                writeln(0);
                return;
            }
        }
        else if (t[u].sgn == 1) {
            int df = s - (t[u].val + t[v].val);
            if (df % 2 == 1 || df < 0) {
                writeln(0);
                return;
            }
            if (x == inf || x == df) {
                x = df;
                one = true;
            }
            else {
                writeln(0);
                return;
            }
        }
        else if (t[u].sgn == -1) {
            int df = s - (t[u].val + t[v].val);
            if (df % 2 == 1 || df > 0) {
                writeln(0);
                return;
            }
            if (x == inf || x == -df) {
                x = -df;
                one = true;
            }
            else {
                writeln(0);
                return;
            }
        }
    }

    if (one) {
        writeln(1);
        return;
    }

    long lo = 1, hi = inf;

    foreach (i ; 0 .. n) {
        if (t[i].sgn == 1) {
            lo = max(lo, -t[i].val + 1);
        }
        else {
            hi = min(hi, t[i].val - 1);
        }
    }

    debug {
        writefln("%d %d", lo, hi);
    }

    long ans = max(0, hi - lo + 1);
    writeln(ans);
}



































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
