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");

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 col = new int[](n);
    col[] = -1;

    bool nibu = true;

    void dfs(int p, int v, int c) {
        col[v] = c;

        foreach (e ; adj[v]) {
            if (e.to == p) {
                continue;
            }
            if (col[e.to] != -1 && col[e.to] != (c ^ 1)) {
                nibu = false;
            }
            if (col[e.to] == -1) {
                dfs(v, e.to, c ^ 1);
            }
        }
    }

    dfs(-1, 0, 0);

    enum inf = 10L^^18;

    auto val = new long[](n);
    val[] = inf;
    val[0] = 1;

    void dfs2(int p, int v) {
        foreach (e ; adj[v]) {
            if (val[e.to] == inf) {
                val[e.to] = e.s - val[v];
                dfs2(v, e.to);
            }
        }
    }

    dfs2(-1, 0);

    foreach (i ; 0 .. m) {
        if (col[es[i].to] == col[es[i].from]) {
            if (val[es[i].to] + val[es[i].from] != es[i].s) {
                writeln(0);
                return;
            }
        }
    }

    if (nibu) {
        long lo = 1, hi = 10^^9;

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

        debug {
            writeln(hi, " ", lo);
        }

        long ans = hi - lo + 1;

        if (ans <= 0) {
            writeln(0);
        }
        else {
            writeln(ans);
        }
    }
    else {
        foreach (i ; 0 .. m) {
            if (col[es[i].to] != col[es[i].from]) {
                if ((val[es[i].to] + val[es[i].from] - es[i].s) % 2 != 0) {
                    writeln(0);
                    return;
                }
            }
        }

        writeln(1);
    }
}









































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);
        }
    }
}
