#include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)n;++i)
#define FOR(i, c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()


using namespace std;
#define rep(i, n) for(int i = 0 ; i < (int)(n) ; i++)
typedef long long ll;
typedef long double Double;

#ifndef INPUTS_DIR
#define INPUTS_DIR "./"
#endif

#define int long long
typedef int Weight;

int color[100010];


struct Edge {
    int src, dst;
    Weight weight;

    Edge(int src, int dst, Weight weight) :
            src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
           e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Graph g;

int v0cost2times = -1;


vector<int> dbg;

int smm[100010];

bool broken(int x, int c, int sum) {

    if (color[x] != -1) {
        if (c != color[x]) {
//            for (auto v : dbg) cout << v << "(" << smm[x] << ")";
//            cout << x << "(" << sum << ")" << " | " << sum - smm[x] << endl;
            v0cost2times = abs(smm[x] - sum);
            return true;
        }
        return false;
    }
    smm[x] = sum;

    dbg.push_back(x);
    color[x] = c;

    for (auto i : g[x]) {
        if (broken(i.dst, c ^ 1, i.weight - sum)) return true;
    }
    dbg.push_back(x);
//    cout << endl;
    return false;
}

int assignWeight[100010];
bool weightAssigned[100010];

void greedy(int x, int cst) {
    if (weightAssigned[x]) return;
    assignWeight[x] = cst;
    weightAssigned[x] = 1;
    for (auto i : g[x]) {
        greedy(i.dst, i.weight - cst);
    }
}

void solve(ll n, ll m, vector<ll> u, vector<ll> v, vector<ll> s) {
    g.resize(n);
    for (int i = 0; i < m; i++) {
        u[i]--;
        v[i]--;
        g[u[i]].push_back(Edge(u[i], v[i], s[i]));
        g[v[i]].push_back(Edge(v[i], u[i], s[i]));

    }
    memset(color, -1, sizeof(color));

    auto check = [&](int ww) {
        memset(weightAssigned, 0, sizeof(weightAssigned));
        greedy(0, ww);
        bool ok = 1;
        for (int i = 0; i < n; i++) {
            ok &= assignWeight[i] > 0;
        }
        for (int i = 0; i < m; i++) {
            ok &= assignWeight[u[i]] + assignWeight[v[i]] == s[i];
        }
        return ok;
    };

    if (broken(0, 0, 0)) {
        if (v0cost2times % 2 == 0) {
            v0cost2times /= 2;
            greedy(0, v0cost2times);
            bool ok = 1;
            for (int i = 0; i < n; i++) {
                ok &= assignWeight[i] > 0;
            }
            for (int i = 0; i < m; i++) {
                ok &= assignWeight[u[i]] + assignWeight[v[i]] == s[i];
            }
            cout << ok << endl;
        } else {
            cout << 0 << endl;
        }
    } else {
        greedy(0, 0);
//        for (int i = 0; i < n; i++) cout << assignWeight[i] << "<" << endl;
        int mx = 1e15;
        int mi = -1e15;
        for (int i = 0; i < n; i++) {
            if (color[i] == 0) {
                mi = max(mi, -assignWeight[i] + 1);
            } else {
                mx = min(mx, assignWeight[i] - 1);
            }
        }
        if (mi <= mx) {
            bool ok = check(mi);

            if (ok) {
                cout << mx - mi + 1 << endl;
            } else {
                cout << 0 << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
//    int another = 0;
//    for (int i = 0; i < 100; i++)
//        if (check(i)) another++, cout << "[" << i << "]" << endl;
//    cout << another << endl;


}


void main_(istream &cin) {
    ll n;
    ll m;
    cin >> n;
    cin >> m;
    vector<ll> u(m - 1 + 1);
    vector<ll> s(m - 1 + 1);
    vector<ll> v(m - 1 + 1);
    for (int i = 0; i <= m - 1; i++) {
        cin >> u[i];
        cin >> v[i];
        cin >> s[i];
    }
    solve(n, m, u, v, s);
}

signed main() {
#ifdef INTELLIJ
    ifstream ifs(string(INPUTS_DIR) + "in_1.txt");
    if (ifs.is_open()) {
        main_(ifs);
        ifs.close();
    } else {
        cerr << "Error: no input." << endl;
    }
#else
    ios::sync_with_stdio(false);
    cin.tie(0);
    main_(cin);
#endif
}





Measured memory_bytes: 24304