#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream& operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream& operator<<(ostream &os, vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream& operator<<(ostream &os, set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream& operator<<(ostream &os, pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream& operator<<(ostream &os, map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

int N, M;
vector<int> to[100000];
vector<lint> cost, U, V;

constexpr lint INF = 1E17;

int main()
{
    cin >> N >> M;
    REP(e, M)
    {
        int u, v, s;
        cin >> u >> v >> s;
        to[u - 1].push_back(e);
        to[v - 1].push_back(e);
        cost.push_back(s);
        U.push_back(u - 1);
        V.push_back(v - 1);
    }

    vector<lint> sp(N, INF), sn(N, INF);
    sp[0] = 0;
    queue<int> pendingedges;
    for (auto  e : to[0]) pendingedges.push(e);
    while (!pendingedges.empty())
    {
        int e = pendingedges.front();
        pendingedges.pop();

        lint u = U[e], v = V[e], c = cost[e];
        REP(_, 2)
        {
            bool flg = false;
            if (sp[u] != INF && sn[v] == INF)
            {
                flg = true;
                sn[v] = c - sp[u];
            }
            if (sn[u] != INF && sp[v] == INF)
            {
                flg = true;
                sp[v] = c - sn[u];
            }
            if (sn[u] != INF && sp[v] != INF && sn[u] + sp[v] != c) cout << 0 << endl, exit(0);
            if (sn[v] != INF && sp[u] != INF && sn[v] + sp[u] != c) cout << 0 << endl, exit(0);

            if (flg) for (auto en : to[v]) pendingedges.push(en);
            swap(u, v);
        }
    }

    lint xmin = 1, xmax = 1E9;
    REP(v, N)
    {
        lint mincost = 1e9;
        for (auto e : to[v]) mincost = min(mincost, cost[e]);

        if (sp[v] != INF)
        {
            xmin = max(xmin, -sp[v] + 1);
            xmax = min(xmax, mincost - 1 - sp[v]);
        }
        if (sn[v] != INF)
        {
            xmin = max(xmin, sn[v] - mincost + 1);
            xmax = min(xmax, sn[v] - 1);
        }
        if (sp[v] != INF && sn[v] != INF)
        {
            if (abs(sp[v] + sn[v]) % 2)
            {
                cout << 0 << endl;
                return 0;
            }
            else
            {
                xmin = max(xmin, (-sp[v] + sn[v]) / 2);
                xmax = min(xmax, (-sp[v] + sn[v]) / 2);
            }
        }
    }
    cout << max(xmax - xmin + 1, 0LL) << endl;
}


Measured memory_bytes: 10456