#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

// 参考URL : https://kmjp.hatenablog.jp/entry/2018/07/08/0900

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

    int n, m;
    cin >> n >> m;
    vector<vector<P>> graph(n);
    for(int i = 0; i < m; ++i){
        int u, v, s;
        cin >> u >> v >> s;
        --u, --v;
        graph[u].emplace_back(v, s);
        graph[v].emplace_back(u, s);
    }
    vector<int> color(n, -1);
    auto is_bipartite = [&](auto&& self, int cur = 0, int c = 1) -> bool {
        color[cur] = c;
        for(auto e : graph[cur]){
            int nxt = e.first;
            if(color[nxt] == c) return false;
            if(color[nxt] == -1 && !self(self, nxt, c ^ 1)) return false;
        }
        return true;
    };
    constexpr ll inf = 1e+17;
    if(is_bipartite(is_bipartite)){
        vector<ll> dist(n, inf);
        auto dfs = [&](auto&& self, int cur = 0, ll num = 0) -> void {
            dist[cur] = num;
            for(auto e : graph[cur]){
                int nxt = e.first, cost = e.second;
                if(dist[nxt] == inf)    self(self, nxt, cost - num);
                else if(dist[nxt] != cost - num){
                    cout << 0 << endl;
                    exit(0);
                }
            }
        };
        dfs(dfs);
        ll L = 1, R = inf;
        for(int v = 1; v < n; ++v){
            if(color[v] > 0)    chmax(L, 1 - dist[v]);
            else    chmin(R, dist[v] - 1);
        }
        cout << max(0LL, R - L + 1) << endl;
    }
    else{
        vector<vector<ll>> dist(2, vector<ll>(n, inf));
        auto dfs = [&](auto&& self, int idx = 0, int cur = 0, ll num = 0) -> void {
            dist[idx][cur] = num;
            idx ^= 1;
            for(auto e : graph[cur]){
                int nxt = e.first, cost = e.second;
                if(dist[idx][nxt] == inf)   self(self, idx, nxt, cost - num);
                else if(dist[idx][nxt] != cost - num){
                    cout << 0 << endl;
                    exit(0);
                }
            }
        };
        dfs(dfs);
        ll st = dist[1][0] / 2;
        for(int i = 0; i < n; ++i)  dist[0][i] = dist[1][i] = inf;
        dfs(dfs, 0, 0, st);
        for(int i = 0; i < n; ++i){
            if(min(dist[0][i], dist[1][i]) <= 0){
                cout << 0 << endl;
                return 0;
            }
        }
        cout << 1 << endl;
    }
}

Measured memory_bytes: 22424