#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

long long n, m;
long long v[500000], u[500000], s[500000];
vector<int> graph[500000];
int go(int vertex, int i)
{
    return v[i] + u[i] - vertex;
}

long long addition[500000];
long long sign[500000];
long long color[500000];
vector<long long> req;

void dfs(int v)
{
    color[v] = 1;
    for (auto e : graph[v])
    {
        int u = go(v, e);
        if (color[u] == 0)
        {
            sign[u] = -sign[v];
            addition[u] = s[e] - addition[v];
            dfs(u);
        }
        else
        {
            if (sign[u] == sign[v])
            {
                if (abs(addition[u] + addition[v]) % 2 != abs(s[e]) % 2)
                {
                    cout << 0;
                    exit(0);
                }
                else
                {
                    if (sign[u] == 1)
                    {
                        long long val = (s[e] - addition[v] - addition[u]) / 2;
                        if (val <= 0)
                        {
                            cout << 0;
                            exit(0);
                        }
                        else
                        {
                            req.push_back(val);
                        }
                    }
                    else
                    {
                        long long val = (-s[e] + addition[v] + addition[u]) / 2;
                        if (val <= 0)
                        {
                            cout << 0;
                            exit(0);
                        }
                        else
                        {
                            req.push_back(val);
                        }
                    }
                }
            }
            else
            {
                if (addition[u] + addition[v] != s[e])
                {
                    cout << 0;
                    exit(0);
                }
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> v[i] >> u[i] >> s[i];
        graph[v[i]].push_back(i);
        graph[u[i]].push_back(i);
    }
    sign[1] = 1;
    dfs(1);
    sort(all(req));
    //cout << req.size() << endl;
    //for (auto e : req) cout << e << endl;
    if (req.size() && req[0] != req.back())
    {
        cout << 0;
        exit(0);
    }
    long long l = 1, r = 1e18;
    for (int i = 1; i <= n; i++)
    {
        if (sign[i] == 1)
        {
            l = max(l, - addition[i] + 1);
        }
        else
        {
            r = min(r, addition[i] - 1);
        }
    }
    if (l > r)
    {
        cout << 0;
        exit(0);
    }
    if (req.size())
    {
        cout << 1;
        exit(0);
    }
    cout << r - l + 1;
}


Measured memory_bytes: 36992