#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

using pll = pair<int, ll>;

const int maxn = 100010;
V<pii> g[maxn];
int col[maxn];
pll dt[maxn];

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

    for (auto e : g[v]) {
        if (col[e.fi] == -1) {
            dt[e.fi] = mp(-dt[v].fi, e.se - dt[v].se);
            dfs(e.fi, c ^ 1);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    V<pair<pii, int>> edges;
    rep(i, m) {
        int u, v, s;
        cin >> u >> v >> s;
        --u;
        --v;
        g[u].eb(v, s);
        g[v].eb(u, s);
        edges.eb(mp(u, v), s);
    }

    memset(col, -1, sizeof(col));
    dt[0] = mp(1, 0);
    dfs(0, -1);
    rep(i, n) { debug(i, dt[i]); }

    for (auto e : edges) {
        int a, b;
        tie(a, b) = e.fi;
        if (dt[a].fi == dt[b].fi) {
            // odd
            int u = e.se - dt[a].se - dt[b].se;
            int d = dt[a].fi * 2;
            debug(u, d);
            if (u % d != 0) {
                puts("0");
                return 0;
            }

            int v = u / d;
            bool ok = 0;
            if (v > 0) {
                V<ll> val(n);
                ok = 1;
                rep(i, n) {
                    val[i] = dt[i].se + dt[i].fi * v;
                    if (val[i] <= 0) {
                        ok = 0;
                        break;
                    }
                }
                debug(val);
                for (auto es : edges) {
                    if (val[es.fi.fi] + val[es.fi.se] != es.se) {
                        ok = 0;
                    }
                }
            }
            cout << int(ok) << endl;
            return 0;
        } else {
            if (e.se != dt[a].se + dt[b].se) {
                puts("0");
                return 0;
            }
        }
    }

    ll mini = 1, maxi = TEN(18);

    rep(i, n) {
        if (dt[i].fi == 1) {
            chmax(mini, 1 - dt[i].se);
        } else {
            chmin(maxi, dt[i].se - 1);
        }
    }
    cout << max(maxi - mini + 1, 0ll) << endl;

    return 0;
}

Measured memory_bytes: 13492