#include <cstdio>
#include <vector>
#include <utility>
#include <tuple>
#include <cstring>
#include <iostream>
using namespace std;

int N, M;
using Graph = vector< vector< pair<int, int> > >;

const long long int INF = 1LL << 60;
bool is_bipartite = true, exist_solution = true;
long long int color[100010], value[100010], start_val = -INF;

void dfs(Graph &G, int cur, int par, int verify) {
    for(auto e : G[cur]) {
        int to, val; tie(to, val) = e;
        if(to == par) continue;

        if(color[to] >= 0) {
            if(color[to] == color[cur]) {
                is_bipartite = false;

                if(!verify) {
                    long long int diff = (value[cur] + value[to]) - val;
                    if(abs(diff) % 2) {
                        exist_solution = false;
                    }
                    else {
                        long long int chg_val;
                        if(color[cur] == 1) {
                            chg_val = 1 + diff / 2 * (-1);
                        }
                        else {
                            chg_val = 1 + diff / 2;
                        }

                        if(chg_val <= 0) {
                            exist_solution = false;
                        }

                        if(start_val == -INF) {
                            start_val = chg_val;
                        }
                        else if(start_val != chg_val) {
                            exist_solution = false;
                        }
                    }
                }
                else {
                    if(value[cur] + value[to] != val) {
                        exist_solution = false;
                    }
                }
            }
            else {
                if(value[cur] + value[to] != val) {
                    exist_solution = false;
                }
            }
        }
        else {
            color[to] = 1 - color[cur];
            value[to] = val - value[cur];
            if(verify && value[to] <= 0) exist_solution = false;
            dfs(G, to, cur, verify);
        }
    }
}

int main() {
    scanf("%d%d", &N, &M);

    Graph G(N);
    for(int i=0; i<M; i++) {
        int u, v, s; scanf("%d%d%d", &u, &v, &s);
        u--; v--;
        G[u].push_back(make_pair(v, s));
        G[v].push_back(make_pair(u, s));
    }

    memset(color, -1, sizeof(color));
    value[0] = color[0] = 1;
    dfs(G, 0, -1, false);

    if(is_bipartite) {
        if(exist_solution) {
            // color[i] == 1 側について、どれだけ上げないと成立しないか
            // color[i] == 0 側について、どれだけ余裕があるか
            long long int m1 = 0, m2 = INF;
            for(int i=0; i<N; i++) {
                if(color[i] == 1) {
                    m1 = max(m1, -value[i] + 1);
                }
                else {
                    m2 = min(m2,  value[i] - 1);
                }
            }

            fprintf(stderr, "m1 = %lld, m2 = %lld\n", m1, m2);
            if(m2 < 0 || m1 > m2) {
                cout << 0 << endl;
            }
            else {
                cout << m2 - m1 + 1 << endl;
            }
        }
        else {
            cout << 0 << endl;
        }
    }
    else {
        fprintf(stderr, "start_val = %lld\n", start_val);
        if(exist_solution) {
            memset(color, -1, sizeof(color));
            value[0] = start_val;
            color[0] = 1;
            dfs(G, 0, -1, true);
        }

        cout << exist_solution << endl;
    }

    // debug
    for(int i=0; i<N; i++) {
        fprintf(stderr, "vertex %d: color = %lld, value = %lld\n", i, color[i], value[i]);
    }
    return 0;
}


Measured memory_bytes: 15104