#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const ll INF = 10000000000000000;

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    vector<vector<Edge<T>>> g;
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};

struct Graph
{
    int n;
    vector<vector<int>> g;
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    WeightedGraph<ll> g(n);
    for(int i = 0; i < m; i++){
        int u, v;
        ll s;
        cin >> u >> v >> s;
        u--; v--;
        g.add_edge(u, v, s);
        g.add_edge(v, u, s);
    }
    ll d[100002], b[100002];
    for(int i = 0; i < n; i++) d[i] = b[i] = INF;
    queue<int> que;
    d[0] = 0;
    que.push(0);
    while(que.size()){
        int u = que.front();
        que.pop();
        for(Edge<ll> e : g.g[u]){
            int v = e.to;
            if(d[u] != INF){
                if(b[v] != INF){
                    if(b[v] != e.cost - d[u]){
                        cout << 0 << endl;
                        return 0;
                    }
                }
                else{
                    b[v] = e.cost - d[u];
                    que.push(v);
                }
            }
            if(b[u] != INF){
                if(d[v] != INF){
                    if(d[v] != e.cost - b[u]){
                        cout << 0 << endl;
                        return 0;
                    }
                }
                else{
                    d[v] = e.cost - b[u];
                    que.push(v);
                }
            }
        }
    }
    if(b[0] != INF){
        if(b[0] % 2){
            cout << 0 << endl;
            return 0;
        }
        ll l = b[0] / 2;
        for(int i = 0; i < n; i++) d[i] = b[i] = INF;
        while(que.size()) que.pop();
        d[0] = l;
        que.push(0);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(Edge<ll> e : g.g[u]){
                int v = e.to;
                if(d[u] != INF){
                    if(b[v] == INF){
                        b[v] = e.cost - d[u];
                        que.push(v);
                    }
                }
                if(b[u] != INF){
                    if(d[v] == INF){
                        d[v] = e.cost - b[u];
                        que.push(v);
                    }
                }
            }
        }
        for(int i = 0; i < n; i++)
            if(d[i] != b[i] || d[i] <= 0){
                cout << 0 << endl;
                return 0;
            }
        cout << 1 << endl;
        return 0;
    }
    ll l = 0;
    for(int i = 0; i < n; i++) l = min(l, d[i]);
    for(int i = 0; i < n; i++) d[i] = b[i] = INF;
    while(que.size()) que.pop();
    d[0] = -l + 1;
    que.push(0);
    while(que.size()){
        int u = que.front();
        que.pop();
        for(Edge<ll> e : g.g[u]){
            int v = e.to;
            if(d[u] != INF){
                if(b[v] == INF){
                    b[v] = e.cost - d[u];
                    que.push(v);
                }
            }
            if(b[u] != INF){
                if(d[v] == INF){
                    d[v] = e.cost - b[u];
                    que.push(v);
                }
            }
        }
    }
    l = INF;
    for(int i = 0; i < n; i++) l = min(l, b[i]);
    cout << max(0ll, l) << endl;
}

Measured memory_bytes: 9728