#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

long long sabun[102000][2];
bool visited[102000][2];
bool impossible;
vector<pair<int, long long> > path[102000];


void visit(int from, int to, long long cost, int checker)
{
    //cout << from << endl;
    long long delta = cost - sabun[from][checker];
    checker = 1 - checker;
    if(visited[to][checker] == true){
        if(delta != sabun[to][checker]){
            impossible = true;
        }
        return;
    }
    sabun[to][checker] = delta;
    visited[to][checker] = true;
    for(int i = 0; i < path[to].size(); i++){
        visit(to, path[to][i].first, path[to][i].second, checker);
    }
}

int main() {
    impossible = false;
    //cout.precision(10);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        sabun[i][0] = 0;//a + hoge
        sabun[i][1] = 0;//-a + hoge
        visited[i][0] = false;
        visited[i][1] = false;
    }
    for(int i = 1; i <= m; i++){
        int u, v;
        long long s;
        cin >> u >> v >> s;
        pair<int, long long> in;
        in.first = u;
        in.second = s;
        path[v].push_back(in);
        in.first = v;
        path[u].push_back(in);
    }
    visit(1, 1, 0, 0);
    if(impossible){
        cout << 0 << endl;
        return 0;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= 1; j++){
            if(visited[i][j]){
                //cout << j << endl;
            }
        }
        //cout << sabun[i][0] << " " << sabun[i][1] << endl;
    }
    bool check = true;
    for(int i = 1; i <= n; i++){
        if(visited[i][0] && visited[i][1]){
            check = false;
        }
    }
    if(check){
        long long maxi = INF * INF;
        long long mini = 1;
        for(int i = 1; i <= n; i++){
            if(visited[i][0]){
                maxi = min(sabun[i][0] - 1, maxi);
            }
            mini = max(mini, -sabun[i][1] + 1);
        }
        long long ans = maxi - mini + 1;
        if(ans < 0){
            ans = 0;
        }
        cout << ans << endl;
        return 0;
    }
    vector<long long> answer;
    for(int i = 1; i <= n; i++){
        if(visited[i][0] && visited[i][1]){
            if((sabun[i][0] - sabun[i][1]) % 2 != 0){
                cout << 0 << endl;
                return 0;
            }
            long long ans = (sabun[i][0] - sabun[i][1]) / 2;
            if(ans <= 0){
                cout << 0 << endl;
                return 0;
            }
            answer.push_back(ans);
        }
    }
    for(int i = 0; i < answer.size(); i++){
        if(answer[i] != answer[0]){
            cout << 0 << endl;
            return 0;
        }
    }
        long long maxi = INF * INF;
        long long mini = 1;
        for(int i = 1; i <= n; i++){
            if(visited[i][0]){
                maxi = min(sabun[i][0] - 1, maxi);
            }
            mini = max(mini, -sabun[i][1] + 1);
        }
        if(maxi < answer[0]){
            cout << 0 << endl;
            return 0;
        }
        if(mini > answer[0]){
            cout << 0 << endl;
            return 0;
        }
    cout << 1 << endl;
    return 0;
}


Measured memory_bytes: 17788