#include <bits/stdc++.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>
#include <math.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define repf(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define repr(i, a, b) for (ll i = (ll)a; i > (ll)b; i--)
#define repv(x, arr) for (auto &x : arr)
#define all(v) (v).begin(), (v).end()
#define vec(name, num) vector<ll> name((num), 0);
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int infi = 2147483600;
const ll infl = 1e17;

typedef pair<pair<ll, ll>, ll> pp;

ll n, m;
vector<pll> edge[100005];
set<pp> edges;
ll cost[100005];
ll op[100005];

void dfs(ll now){
    for (auto p:edge[now]){
        ll nex = p.F, d = p.S;
        if (op[nex]==0){
            op[nex] = -1 * op[now];
            cost[nex] = d - cost[now];
            edges.erase(mp(mp(min(now, nex), max(now, nex)), d));
            dfs(nex);
        }
    }
}

int main()
{
    cin >> n >> m;
    rep(i,m){
        ll u, v, s;
        cin >> u >> v >> s;
        u--;
        v--;
        edge[u].push_back(mp(v, s));
        edge[v].push_back(mp(u, s));
        edges.insert(mp(mp(min(u,v), max(u,v)), s));
    }
    rep(i, n) op[i] = 0;
    cost[0] = 0;
    op[0] = 1;
    dfs(0);
    set<ll> x;
    for (auto e:edges){
        ll u = e.F.F, v = e.F.S, d = e.S;
        if (op[u]!=op[v]){
            if (cost[u]+cost[v]==d)
                continue;
            else {
                cout<<0<<endl;
                return 0;
            }
        }
        else if (op[u]==1){
            if ((d-cost[u]-cost[v])%2==1){
                cout << 0 << endl;
                return 0;
            }
            else {
                x.insert((d - cost[u] - cost[v]) / 2);
            }
        }
        else {
            if ((cost[u]+cost[v]-d)%2==1){
                cout << 0 << endl;
                return 0;
            }
            else {
                x.insert((cost[u] + cost[v] - d) / 2);
            }
        }
    }
    if (x.size() == 0)
    {
        ll MIN = 0, MAX = infl;
        rep(i, n)
        {
            if (op[i] == 1)
            {
                MIN = max(MIN, -cost[i]);
            }
            else
            {
                MAX = min(MAX, cost[i]);
            }
        }
        cout << max(0ll, MAX - MIN - 1) << endl;
        return 0;
        }
        else if (x.size() > 1)
        {
            cout << 0 << endl;
            return 0;
        }
        else if (*x.begin() <= 0)
        {
            cout << 0 << endl;
            return 0;
        }
        else
        {
            ll x0 = *x.begin();
            rep(i,n){
                if (cost[i]+op[i]*x0<=0){
                    cout << 0 << endl;
                    return 0;
                }
            }
            cout << 1 << endl;
        }
}

Measured memory_bytes: 26624