#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;


int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<pair<int,int>> edges;
    vector<long long> S(m);
    vector<vector<int>> graph(n);
    for (int i = 0; i < m; ++i) {
        int u, v;
        long long x;
        cin >> u >> v >> x;
        --u, --v;
        edges.emplace_back(u, v);
        S[i] = x;
        graph[u].push_back(i);
        graph[v].push_back(i);
    }

    int s = 0;

    const long long INF = 1LL << 60;
    vector<pair<long long, int>> F(n, make_pair(INF, -123));
    F[s] = make_pair(0, 1);

    queue<int> que;
    que.emplace(s);

    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (auto e : graph[u]) {
            int v = edges[e].first + edges[e].second - u;
            if (abs(F[v].second) > 1) {
                F[v].first = S[e] - F[u].first;
                F[v].second = -F[u].second;
                que.emplace(v);
            } else {
                long long a = S[e] - F[u].first;
                long long b = -F[u].second;
                if (b == F[v].second && a != F[v].first) {
                    cout << 0 << '\n';
                    return 0;
                }
            }
        }
    }

    auto check = [&](vector<long long>& X) {
        for (auto x : X) {
            if (x <= 0) {
                return false;
            }
        }

        for (int i = 0; i < m; ++i) {
            int u = edges[i].first, v = edges[i].second;
            if (X[u] + X[v] != S[i]) {
                return false;
            }
        }
        return true;
    };


    for (int i = 0; i < n; ++i) {
        assert(F[i].second == -1 || F[i].second == 1);
    }

    long long res = 0;
    bool done = false;
    for (int i = 0; i < m; ++i) {
        int u = edges[i].first, v = edges[i].second;
        if (F[u].second == F[v].second) {
            done = true;
            long long y = S[i] - F[u].first - F[v].first;
            long long x = y / (2 * F[u].second);
            // if (x <= 0 || x * 2 * F[u].second != y) {
            //     break;
            // }
            vector<long long> X(n, 0);
            for (int j = 0; j < n; ++j) {
                X[j] = F[j].first + F[j].second * 1LL * x;
            }

            if (check(X)) {
                res = 1;
            }

            break;
        }
    }

    if (!done) {
        long long L = 1;
        long long R = INF;
        for (int i = 0; i < n; ++i) {
            if (F[i].second < 0) {
                long long r = F[i].first - 1;
                R = min(R, r);
            } else {
                long long l = 1 - F[i].first;
                L = max(L, l);
            }
        }
        // cout << L << " " << R << endl;
        for (int i = 0; i < m; ++i) {
            int u = edges[i].first;
            int v = edges[i].second;
            long long y = S[i];
            for (auto x : {u, v}) {
                if (F[x].second < 0) {
                    long long l = F[x].first - (y - 1);
                    L = max(L, l);
                } else {
                    long long r = y - 1 - F[x].first;
                    R = min(R, r);
                }
            }
        }
        // cout << L << " " << R << endl;
        res = max(0LL, R - L + 1);
    }


    cout << res << '\n';

    return 0;
}

Measured memory_bytes: 10104