#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
    int n, m;
    cin>>n>>m;

    vector<vector<int>> E(n);
    vector<vector<long long>> S(n);
    for (int i=0; i<m; i++)
    {
        int u, v;
        long long s;
        cin>>u>>v>>s;
        u--, v--;
        E[u].push_back(v);
        S[u].push_back(s);
        E[v].push_back(u);
        S[v].push_back(s);
    }

    vector<long long> V(n);
    vector<bool> X(n);
    vector<bool> F(n);
    int cv = -1;
    long long ca, cb;

    function<void(int,int,long long,bool)> BT = [&](int c, int p, long long v, bool x)
    {
        if (F[c] && X[c] != x)
        {
            cv = c;
            ca = V[c];
            cb = v;
        }
        if (F[c])
            return;
        F[c] = true;
        V[c] = v;
        X[c] = x;
        for (int i=0; i<(int)E[c].size(); i++)
        if (E[c][i] != p)
            BT(E[c][i], c, S[c][i]-v, !x);
    };
    BT(0, -1, 0LL, false);
    if (cv != -1)
    {
        if ((ca-cb)%2 != 0)
        {
            cout<<0<<endl;
            return 0;
        }

        F = vector<bool>(n);
        BT(cv, -1, (ca+cb)/2, false);
    }

    bool ok = true;
    bool bi = true;

    for (int i=0; i<n; i++)
    {
        for (int j=0; j<(int)E[i].size(); j++)
        {
            if (V[i]+V[E[i][j]] != S[i][j])
                ok = false;
            if (X[i] == X[E[i][j]])
                bi = false;
        }
    }

    if (!ok)
    {
        cout<<0<<endl;
        return 0;
    }
    if (!bi)
    {
        if (*min_element(V.begin(), V.end()) > 0)
            cout<<1<<endl;
        else
            cout<<0<<endl;
        return 0;
    }

    long long mn0 = 1000000000000000000LL;
    long long mn1 = 1000000000000000000LL;

    for (int i=0; i<n; i++)
    {
        if (X[i])
            mn0 = min(mn0, V[i]);
        else
            mn1 = min(mn1, V[i]);
    }

    long long ans = max(mn0+mn1-1, 0LL);
    cout<<ans<<endl;
}
