#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, 0);
    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] && !self(self, nxt, -c))  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 {
            if(dist[cur] == inf){
                dist[cur] = num;
                for(auto& e : graph[cur]){
                    int nxt = e.first, cost = e.second;
                    self(self, nxt, cost - num);
                }
            }
            if(dist[cur] != 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 {
            if(dist[idx][cur] == inf){
                dist[idx][cur] = num;
                for(auto& e : graph[cur]){
                    int nxt = e.first, cost = e.second;
                    self(self, idx ^ 1, nxt, cost - num);
                }
            }
            if(dist[idx][cur] != num){
                cout << 0 << endl;
                exit(0);
            }
        };
        dfs(dfs);
        ll st = dist[1][0] / 2;
        for(int i = 0; i < 2; ++i){
            for(int j = 0; j < n; ++j){
                dist[i][j] = inf;
            }
        }
        dfs(dfs, 0, 0, st);
        for(int i = 0; i < 2; ++i){
            for(int j = 0; j < n; ++j){
                if(dist[i][j] == inf)   continue;
                if(dist[i][j] <= 0){
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
        cout << 1 << endl;
    }
}

Measured memory_bytes: 25172