#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;

const long long INFL = 0x3f3f3f3f3f3f3f3fLL;

int main() {
        int n, m;
        scanf("%d%d", &n, &m);
        vector<vector<pair<int, long long>>> g(n);
        for (int i = 0; i < m; i ++) {
                int a, b;
                long long c;
                scanf("%d%d%lld", &a, &b, &c);
                a --, b --;
                g[a].push_back({b, c});
                g[b].push_back({a, c});
        }
        vector<long long> val(n, -INFL);
        vector<int> color(n, -1);
        bool ng = false;
        bool bi = true;
        long long tozan = -INFL;
        function<void (int, int, bool)> dfs = [&](int u, int prev, bool c) {
                color[u] = c;
                for (auto e : g[u]) if (e.first != prev) {
                        int v = e.first;
                        if (val[v] == -INFL) {
                                val[v] = e.second - val[u];
                                dfs(v, u, !c);
                        } else {
                                if (color[u] == color[v]) {
                                        bi = false;
                                        long long r;
                                        if (color[u] == 0) {
                                                r = e.second - (val[u] + val[v]);
                                        } else {
                                                r = (val[u] + val[v]) - e.second;
                                        }
                                        if (r < 0 || r % 2 == 1) {
                                                ng = true;
                                        }
                                        tozan = r / 2;
                                } else {
                                        if (val[u] + val[v] != e.second) {
                                                ng = true;
                                        }
                                }
                        }
                }
        };
        val[0] = 1;
        dfs(0, -1, 0);
        if (ng) {
                printf("%d\n", 0);
                return 0;
        }
        if (bi) {
                long long bmi = INFL, wmi = INFL;
                for (int i = 0; i < n; i ++) {
                        if (color[i] == 0) {
                                bmi = min(bmi, val[i]);
                        } else {
                                wmi = min(wmi, val[i]);
                        }
                }
                printf("%lld\n", max(0LL, bmi + wmi - 1));
                return 0;
        }
        val.assign(n, -INFL);
        val[0] = 1 + tozan;
        ng = false;
        function<void (int, int)> dfs2 = [&](int u, int prev) {
                for (auto e : g[u]) if (e.first != prev) {
                        int v = e.first;
                        if (val[v] == -INFL) {
                                val[v] = e.second - val[u];
                                if (val[v] <= 0) {
                                        ng = true;
                                }
                                dfs2(v, u);
                        } else {
                                if (val[v] + val[u] != e.second) {
                                        ng = true;
                                }
                        }
                }
        };
        dfs2(0, -1);
        if (ng) {
                printf("%d\n", 0);
        } else {
                printf("%d\n", 1);
        }
        return 0;
}



Measured memory_bytes: 18432