#include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <climits>

    using namespace std;

    struct Edge
    {
        int v, s;
    };

    //#define int long long

    const int INF = 1e9 + 8;

    bool bipartite = 1;
    vector<int> colors;
    vector< vector<Edge> > gr;

    int val1 = -1;
    vector< pair<int, int> > coes;
    //vector<int> vals;

    void wrong()
    {
        cout << 0 << endl;
        exit(0);
    }
    //
    //void dfsColors(int u)
    //{
    //    for (Edge e : gr[u]) {
    //        if (colors[e.v] == colors[u]) {
    //            bipartite = 0;
    //            return;
    //        }
    //        if (colors[e.v] == 3 - colors[u]) continue;
    //        colors[e.v] = 3 - colors[u];
    //        dfsColors(e.v);
    //    }
    //}

    void calcCoes(int u)
    {
        for (Edge e : gr[u]) {
            if (coes[e.v].first == INF) {
                coes[e.v].first = -coes[u].first;
                coes[e.v].second = e.s - coes[u].second;
    //            cerr << coes[e.v].first << "x + " << coes[e.v].second << endl;
                calcCoes(e.v);
            } else {
                int a1 = coes[e.v].first, b1 = coes[e.v].second;
                int a2 = -coes[u].first, b2 = e.s - coes[u].second;
    //            cerr << a1 << "x + " << b1 << endl;
    //            cerr << a2 << "x + " << b2 << endl;
                int a = a1 - a2, b = b1 - b2;
                if (a == 0) {
                    if (b == 0) continue;
                    else {
    					wrong();
                    }
                } else {
                    double newval = -1.0 * b / a;
                    if (newval != int(newval) || newval <= 0) { // check int
    //					cerr << newval << endl;
                        wrong();
                    }

                    // check equals
                    if (val1 < 0) val1 = newval;
                    else if (val1 != newval) {
    //					cerr << val1 << " != " << newval << endl;
                        wrong();
                    }
                }
            }
        }
    }

    signed main()
    {
    	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, m;
        cin >> n >> m;
        gr.resize(n+1);

        for (int i = 0; i < m; ++i) {
    		int u, v, s;
    		cin >> u >> v >> s;
            gr[u].push_back({v, s});
            gr[v].push_back({u, s});
        }

    //    colors.assign(n+1, 0);
    //    colors[1] = 1;
    //    dfsColors(1);

    	coes.assign(n+1, {INF, INF});
    	coes[1] = {1, 0};
        calcCoes(1);

    //    for (int i = 1; i <= n; ++i) {
    //        cout << i << ": " << coes[i].first << "x + " << coes[i].second << endl;
    //    }

        if (val1 >= 0) {
            for (int u = 2; u <= n; ++u) {
                if (coes[u].first * val1 + coes[u].second < 1) wrong();
            }
            cout << 1 << endl;
            return 0;
        }

        int minv = 1, maxv = INF;
        for (int u = 2; u <= n; ++u) {
            if (coes[u].first < 0) maxv = min(maxv, coes[u].second - 1);
            else minv = max(minv, 1 - coes[u].second);
        }

        if (minv > maxv) cout << 0 << endl;
        else cout << maxv - minv + 1 << endl;

    //
    //    if (!bipartite) {
    //        vals.assign(n+1, -1);
    //        coes[1] = {1, 0};
    //        calcCoes(1);
    //    } else {
    //
    //    }
    }


Measured memory_bytes: 12800