#include <bits/stdc++.h>
using namespace std;
//#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(X) debug_func(X, #X)
#define DEBUG_ENDL endl
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(X) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "")
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
void debug_func_preffix(const string &s) { if(s.size()) cout << s << " = "; }
template <typename T>
void debug_func(const T &t, const string &s = "") {
    debug_func_preffix(s);
    cout << t << DEBUG_ENDL_S(s);
}
template <typename T, typename U>
void debug_func(const P<T, U> &p, const string &s = "") {
    debug_func_preffix(s);
    cout << "(";
    debug_func(p.first);
    cout << ", ";
    debug_func(p.second);
    cout << ")" << DEBUG_ENDL_S(s);
}
template <typename T>
void debug_func(const V<T> &v, const string &s = "") {
    for(ll i = 0; i < 10; i++) {
        string t = s + "[" + to_string(i) + "]";
        debug_func(v[i], t);
    }
}

const ll INF = 5e15;

struct Expr {
    ll x, num;
};

enum Status {
    OK,
    NG,
    X,
};

V<Expr> expr(1e5 + 10, Expr{1, 0});
V<bool> calced(1e5 + 10, false);
VV<PLL> edges(1e5 + 10);
bool x_error = false;
ll x_upper = INF, x_lower = 0;

pair<Status, ll> check(const Expr &a, const Expr &b, ll s) {
    ll xco = a.x + b.x;
    ll rval = s - a.num - b.num;
    if(xco == 0) return make_pair(rval == 0 ? OK : NG, 0);
    if(abs(rval) % abs(xco)) return make_pair(NG, 0);
    ll val = rval / xco;
    if(val <= 0) return make_pair(NG, 0);
    return make_pair(X, val);
}

void update_x_range(const Expr &e) {
    if(e.x == 0) return;
    ll v = (-e.num) / e.x;
    if(e.x > 0) chmax(x_lower, v);
    else        chmin(x_upper, v);
}

void dfs(ll now, ll pre) {
    for(const auto &e : edges[now]) {
        ll nxt, s;
        tie(nxt, s) = e;
        if(nxt == pre) continue;
        if(calced[nxt]) {
            auto status = check(expr[now], expr[nxt], s);
            switch (status.first) {
                case OK: 
                    break;
                case NG: 
                    x_error = true;
                    break;
                case X:
                    if(!(x_lower < status.second && status.second < x_upper)) {
                        x_error = true;
                    } else {
                        x_upper = status.second + 1;
                        x_lower = status.second - 1;
                    }
                    break;
            }
        } else {
            calced[nxt] = true;
            expr[nxt] = Expr{-expr[now].x, s - expr[now].num};
            update_x_range(expr[nxt]);
            dfs(nxt, now);
        }
    }
}

int main() {
    calced[0] = true;
    ll N, M;
    cin >> N >> M;
    for(ll i = 0; i < M; i++) {
        ll u, v, s;
        cin >> u >> v >> s;
        u--;
        v--;
        edges[u].push_back(PLL(v, s));
        edges[v].push_back(PLL(u, s));
    }
    dfs(0, -1);
    DEBUG(x_upper);
    DEBUG(x_lower);
    DEBUG(x_error);
    if(x_error) cout << 0;
    else cout << max<ll>(0, x_upper - x_lower - 1);
    cout << endl;
    return 0;
}


Measured memory_bytes: 15616