#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する

struct edge {
    int to;
    ll sum;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    vector<vector<edge> > G(n + 1);
    vector<pair<ll, ll> > siki(n + 1);
//    vector<vector<> > edge(n);

    rep(i, 0, m) {
        int u, v, s;
        cin >> u >> v >> s;
        G[u].push_back((edge) {v, s});
        G[v].push_back((edge) {u, s});
    }

    queue<int> que;
    que.push(1);
    siki[1] = {1, 0};

    while (!que.empty()) {
        int top = que.front();
        que.pop();
        for (int i = 0; i < G[top].size(); i++) {
            if (siki[G[top][i].to].first != 0) {
                continue;
            }
            pair<ll, ll> next;
            if (siki[top].first == 1) {
                next.first = -1;
            } else {
                next.first = 1;
            }
            next.second = G[top][i].sum - siki[top].second;
            siki[G[top][i].to] = next;
            que.push(G[top][i].to);
        }
    }

    int kaiko = 10;
    ll kai;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            pair<ll, ll> sikiA = siki[i];
            pair<ll, ll> sikiB = siki[G[i][j].to];
            ll sum = G[i][j].sum;
            if (sikiA.first != sikiB.first) {
                if (sikiA.second + sikiB.second != sum) {
                    cout << 0 << endl;
                    return 0;
                }
            } else {
                if (kaiko == 10) {
                    kaiko = 1;
                    if ((sikiA.second + sikiB.second - sum) % 2 == 0) {
                        kai = ((sum - (sikiA.second + sikiB.second)) / (sikiA.first + sikiB.first));
                    } else {
                        cout << 0 << endl;
                        return 0;
                    }
                } else {
                    if (kai != ((sum - (sikiA.second + sikiB.second)) / (sikiA.first + sikiB.first))) {
                        cout << 0 << endl;
                        return 0;
                    }
                }
            }
        }
    }

    if (kaiko == 1) {
        if (kai > 0) {
            bool flag = true;
            for(int i = 1; i <= n; i++){
                if(siki[i].first * kai + siki[i].second <= 0){
                    cout << 0 << endl;
                    return 0;
                }
            }
            cout << 1 << endl;
            return 0;
        } else {
            cout << 0 << endl;
        }
    } else {
        ll lower = 1, upper = INFl;
        for (int i = 1; i <= n; i++) {
            if (siki[i].first > 0) {
                lower = max(lower, -siki[i].second + 1);
            } else {
                upper = min(upper, siki[i].second - 1);
            }
        }
        if (upper >= lower) {
            cout << upper - lower + 1 << endl;
        } else {
            cout << 0 << endl;
        }
    }


    return 0;
}


Measured memory_bytes: 9600